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: \"
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?