Connection Refused to Simulink Real Time Target Port

5 views (last 30 days)
I am trying to use sockets (socketfuncs.h) on a Simulink Real-Time R2017b target in a C S-function.
I got a client socket to run and send some data to a server.
In the next stept I want to open a listening socket (server) and accept incoming connections and read data, if there is any. The server socket is running as non blocking. In the mdlOutput() function I use the accept() function to repeatedly look for incoming connections.
When I run this server and try to connect with a tcp client (telnet for example), I get a connection refused error. TCP error 10061. I tried different Ports in the range of 20000-33000, 80. The firewall is disabled.
I wanted to check which ports are open on the SLRT target, but I could not figure out how to do it.
SOCKET ClientSocket = 0;
SOCKET ServerSocket = 0;
bool ConnReady = false;
uint_T localPort = 32000;
uint_T targetPort = 20000;
char_T targetAddress[16] = "";
static void mdlOutputs(SimStruct *S, int_T tid)
char message[2500];
char server_reply[2000];
int recv_size = 0;
SOCKET _acceptSocket;
// Prepare data to send
sprintf(message, "Hallo! Ich bin ein SLRT Tcp/Ip Client.\n");
//Send some data
if( send(ClientSocket, message , strlen(message) , 0) < 0)
printf("Send failed\n");
// SERVER: Check for incoming data
if (!ConnReady)
_acceptSocket = accept(ServerSocket, NULL, NULL);
int bytesSent;
int bytesRecv = SOCKET_ERROR;
char recvbuf[200] = "";
char sendbuf[200] = "";
if (_acceptSocket != INVALID_SOCKET)
ConnReady = true;
ServerSocket = _acceptSocket;
if (ConnReady)
bytesRecv = recv(ServerSocket, recvbuf, 200, 0);
if (bytesRecv == SOCKET_ERROR)
printf("Server: recv() error %ld.\n", getlasterror());
printf("Bytes received: %ld, Received data is: \"%s\"\n", bytesRecv, recvbuf);
printf("accept failed: %d.", getlasterror());
Also, there seems to be an offset in the errors reported from the socketfuncs.h functions. For example accept() returns 112. Though 112 is not defined in socketfuncs.h, I can see that
#define EDESTADDREQ 12 /* destination address is required / #define RTIP_ERRNO 100 / RTIP error number offset */
So it is safe to assume that I always have to substract RTIP_ERRNO from getlasterrror when using socketfuncs?
Thanks alot.

Answers (1)

Jon Lobo
Jon Lobo on 3 Jul 2018
It's not recommended to do this through an S-Function. I recommend using the TCP blocks here:

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!