Conara
Conara

Reputation: 13

lwip multiple respond sockets (and one listensocket)

I am currently developing with the BSD-like socket API. I asked another relevant question before: lwip stack netconn api keep connection "keep-alive"

I use now the following structure:

One listening thread which opens a new thread if a connection is established:

portTASK_FUNCTION( vModbusServer, pvParameters ) {
int lSocket;
struct sockaddr_in sLocalAddr;

lSocket = lwip_socket(AF_INET, SOCK_STREAM, 0);

if (lSocket < 0) return; // check error

memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr));
sLocalAddr.sin_family = AF_INET;
sLocalAddr.sin_len = sizeof(sLocalAddr);
sLocalAddr.sin_addr.s_addr = htonl(INADDR_ANY);
sLocalAddr.sin_port = MODBUS_PORT;

if (lwip_bind(lSocket, (struct sockaddr *)&sLocalAddr, sizeof(sLocalAddr)) < 0) {
    lwip_close(lSocket);
    return;
}

if ( lwip_listen(lSocket, 20) != 0 ) {
    lwip_close(lSocket);
    return;
}

while (1) {
    vTaskDelay(1);
    int clientfd;
    struct sockaddr_in client_addr;
    int addrlen=sizeof(client_addr);

    clientfd = lwip_accept(lSocket, (struct sockaddr*)&client_addr, (socklen_t)&addrlen);
    
    if (clientfd>0) {
        sys_thread_new( "MODBUSResponder", vModbusResponder, ( void * ) &clientfd,
               lwipMODBUS_SERVER_STACK_SIZE,
               lwipMODBUS_SERVER_PRIORITY );
    }
}
lwip_close(lSocket); }

This thread will be created (respond thread):

vModbusResponder( void *pvParameters ) {
char buffer[12];
int nbytes;
int *temp = (int*) pvParameters;
int clientfd = *temp;

do {
    nbytes=lwip_recv(clientfd, buffer, sizeof(buffer),0);
    if (nbytes>0) { //no error

    }
}  while (nbytes>0);
lwip_close(clientfd);
vTaskDelete(NULL); }

This works perfect for two connections (two respond threads) for example two PC's which connect with the embedded device, but if I try to connect with a third PC than it disconnects the first connection. I don't understand why it happens. The connection is break down from the embedded device (lwip) with a RST. If I connect a fourth PC the second connection disconnects.

I tried the following things:

edit this line to:

define SYS_THREAD_MAX 30

Can somebody give some tips? Do I something wrong?

Upvotes: 0

Views: 5219

Answers (1)

Conara
Conara

Reputation: 13

I found a solution: I didn't edit the following configurations:

#define MEMP_NUM_TCP_PCB        2 // I edited this to 5
#define MEMP_NUM_NETBUF         3 // I edited this to 5
#define MEMP_NUM_NETCONN        4 // I edited this to 5

Now I can set up 5 simultaneously TCP connections. And if I try to open a 6th connection then it wil send a RST.

@Joel Cunningham:

What do you mean with "but the way you are passing clientfd isn't safe. There is no guarantee that your forked thread will run and deference the pvParameter before execution returns back to the original thread (which will re-assign clientfd on the next call to accept())" That the RTOS will switch back to the listening socket before it opens a TCP connection? What is the best way to prevent this from happening? Give it a higher priority than the listening socket?

Upvotes: 1

Related Questions