Connection Refused to Simulink Real Time Target Port

조회 수: 17 (최근 30일)
S. Rösener
S. Rösener 2018년 5월 25일
댓글: Diego Kuratli 2020년 11월 2일
Hi,
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[16] = "192.168.8.13";
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());
else
{
printf("Bytes received: %ld, Received data is: \"%s\"\n", bytesRecv, recvbuf);
}
}
else
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.

답변 (1개)

Jon Lobo
Jon Lobo 2018년 7월 3일
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

카테고리

Help CenterFile Exchange에서 Development Computer Setup에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by