Connection Refused to Simulink Real Time Target Port
5 views (last 30 days)
Show older comments
S. Rösener on 25 May 2018
Commented: Diego Kuratli on 2 Nov 2020
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.
/*** GLOBAL VARIABLES ***/
SOCKET ClientSocket = 0;
SOCKET ServerSocket = 0;
bool ConnReady = false;
uint_T localPort = 32000;
uint_T targetPort = 20000;
char_T targetAddress = "192.168.8.13";
static void mdlOutputs(SimStruct *S, int_T tid)
int recv_size = 0;
// 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)
// SERVER: Check for incoming data
_acceptSocket = accept(ServerSocket, NULL, NULL);
int bytesRecv = SOCKET_ERROR;
char recvbuf = "";
char sendbuf = "";
if (_acceptSocket != INVALID_SOCKET)
ConnReady = true;
ServerSocket = _acceptSocket;
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?
Jon Lobo on 3 Jul 2018
It's not recommended to do this through an S-Function. I recommend using the TCP blocks here: https://www.mathworks.com/help/xpc/tcp.html
Diego Kuratli on 2 Nov 2020
R2020a and previous: https://www.mathworks.com/help/releases/R2020a/xpc/tcp.html
R2020b and newer: https://www.mathworks.com/help/slrealtime/tcp.html
Find more on Development Computer Setup in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!