FreeRTOS+TCP: How to use the FREERTOS_SO_REUSE_LISTEN_SOCKET option

heinbali01 wrote on Tuesday, April 24, 2018:

Right!

    for (;;) {
        Socket_t xSocket = FreeRTOS_accept( xServerSocket, &remote_addr, &l );
        if (xSocket == FREERTOS_INVALID_SOCKET) {
            continue;
        }
        for( ;; )
        {
            int rc = FreeRTOS_recv(xSocket, buf, 1, 0);
            if( ( rc < 0 ) && ( rc != -pdFREERTOS_ERRNO_EWOULDBLOCK ) )
            {
                break;
            }
        }
        FreeRTOS_closesocket(xSocket);
    }

In the above example, xServerSocket continues to exist.

But if you allow only 1 client :

    FreeRTOS_listen( xServerSocket, 1 )

you will still see the problem occurring. I’m afraid that you have to use at least 3, although you only help one client at a time.

Within Linux, after close(socket), the kernel will take over the socket and make sure that it’s connection is closed (because shutdown() was called).

While testing, I saw in a PCAP that at some point there were 3 concurrent connections: 2 were in a closing status, one was just starting.