TCP Refusing FIN

The two TCP peers:

Client: 192.168.21.5,  Win = 64240 "laptop"   port 49833
Server: 192.168.21.10, Win =  2920 "FreeRTOS" port  6000

Actory is right, there is only one FIN packet in the PCAP file. You can find it by setting the filter “tcp.flags.fin==true”. And you can inspect the stream by filtering “tcp.stream eq 2”.

The FIN packet is sent while 192.168.21.10 is still sending lots of data.

packet IP
10271 x.5  FIN packet
10272 x.10 TCP data 1460 bytes
10273 x.10 TCP data 1460 bytes
10274 x.5  RST pack

My intuition:

Laptop is a bit impatient. The peer on the laptop calls shutdown and doesn’t wait for the results of the shutdown. In stead it closes the socket.

Now when x.5 receives data, it doesn’t have a socket to received it. The socket has been closed.

A shutdown needs time. After calling shutdown() you must treat the socket as “connected” and continue to receive data. The shutdown is ready as soon as:

  • Either recv() returns an error code
  • A timeout has occurred

You should do the equivalent of the following on the laptop side:

static void shutdown_socket( Socket_t sck )
{
    TickType_t start_time;

    FreeRTOS_shutdown( sck, FREERTOS_SHUT_RDWR );
    start_time = xTaskGetTickCount( );

    do {
        int rc = FreeRTOS_recv( sck, buffer, sizeof buffer, 0 );
        if( ( rc < 0 ) &&
            ( rc != -pdFREERTOS_ERRNO_EAGAIN ) &&
            ( rc != -pdFREERTOS_ERRNO_EINTR ) )
        {
            /* A fatal error is reported, we can stop waiting. */
            break;
        }
        if( rc > 0 )
        {
          /* Use the received data. */
        }
    } while( ( xTaskGetTickCount( ) - start_time ) < pdMS_TO_TICKS( 1000 ) );

    FreeRTOS_closesocket( sck );
}

Did you program the client your self? Using Winsock or Linux?

1 Like