Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training




Loading

FreeRTOS+TCP : Why do sockets stay in the status eCLOSE_WAIT ?

Posted by heinbali01 on April 2, 2016

A user of FreeRTOS+TCP asked the following question:

Look at the output of FreeRTOS_netstat(). It was recorded on a K63F machine:

~~~~ Prot Port IP-Remote : Port R/T Status Alive tmout Child TCP 3123 0.0.0.0 : 0 0/0 eTCPLISTEN 0 0 0/2 TCP 80 0.0.0.0 : 0 0/0 eTCPLISTEN 0 0 3/3 TCP 80 192.168.180.111 : 55940 1/0 eCLOSEWAIT 0 19973 TCP 80 192.168.180.111 : 55942 1/0 eCLOSEWAIT 0 19975 TCP 80 192.168.180.111 : 55944 1/0 eCLOSEWAIT 0 20000 FreeRTOSnetstat: 5 sockets 2 < 3 < 3 buffers free ~~~~

192.168.180.111 is the PC running several wget instances in parallel, all connecting to K63F. Port 80 has a listen limit of 3 clients. After 1000-4000 seconds no new HTTP connections can be initiated, since there are no free "slots".

I don't understand why those closed sockets (all wgets are killed on the PC) in eCLOSE_WAIT state remain there; they should disappear, shouldn't they? There's a 'tmout' field, which reaches zero, but then the countdown start from 20000 again.

What could be the reason for these persistent, non-closing sockets?

From WiKiPedia:

CLOSE-WAIT
    (both server and client) represents waiting for a connection
    termination request from the local user.

A socket in the status "eCLOSE_WAIT" is waiting for you! It has done all it could and it may be closed by calling FreeRTOS_closesocket().

How can you prevent this?

1) Check the return code of all API's :

~~~~ // Same for FreeRTOSsend() xRc = FreeRTOSrecv(s, b, sizeof b, 0); if( ( xRc < 0 ) && ( xRc != -pdFREERTOSERRNOEWOULDBLOCK ) ) { FreeRTOS_closesocket( s ); s = NULL; return -1; } ~~~~

Note: if a socket's connection is closed but it still has RX data, FreeRTOS_recv() will return positive values until all data have been read.

2) After your socket is connected, you may call FreeRTOS_issocketconnected() to see if it is still connected. If it returns false you can close the socket.

3) You can call FreeRTOS_connstatus() to check the status, but that is only recommended for debugging.

4) Always try to close sockets gracefully. Also under other operating systems that communicate with your embedded systems. If your program decides to close a TCP connection, follow this procedure (symbolically):

~~~~ FreeRTOSshutdown(s, 0); /* A FIN will be sent. Wait up to a few seconds for the other party to ACK our FIN. If the other party still had data to sent, it will do so. Keep on calling FreeRTOSrecv() for this socket until it returns a negative value (not equal to -EWOULDBLOCK) */

for( ;; )
{
    /* Only do this if reading from 's' will block. */
    xRc = FreeRTOS_recv(s, buf, sizeof buf, 0);
    if( xRc > 0 )
    {
        /* Use the received data in 'buf'. */
    }
    else if( ( xRc < 0 ) && ( xRc != -pdFREERTOS_ERRNO_EWOULDBLOCK ) )
    {
        FreeRTOS_closesocket( s );
        s = NULL;
        break;
    }
}

~~~~

The flag for shutdown() (RD, WR, and RDWR) has not been implemented in +TCP.

PS about FreeRTOS_issocketconnected():

For an active connection, using FreeRTOS_connect(), you can call FreeRTOS_issocketconnected() as well. It will return false until the socket gets connected. Once it is connected, it will return true until the connection is closed.

For passive connections (using FreeRTOS_accept()), the socket will start in a connected state.


FreeRTOS+TCP : Why do sockets stay in the status eCLOSE_WAIT ?

Posted by tselmeci on April 5, 2016

Hello Hein!

(I was the user with the issues above)

I've applied some changes (mostly the use of FreeRTOS_issocketconnected) and since then I can't observe zombie sockets any longer. But I'm facing to other problems...

Here's my function to close TCP sockets:

~~~~ int mysocketclose(Sockett sock) { /* wait while TX buffer flushes */ while (FreeRTOSoutstanding(sock) > 0 && FreeRTOSissocketconnected(sock) == pdTRUE) vTaskDelay(pdMSTO_TICKS(20));

/* signalling that we want to gracefully break the connection up */
FreeRTOS_shutdown(sock, FREERTOS_SHUT_RDWR);
/* get rid of any accumulated data */
while ((ret = FreeRTOS_recv(sock, buff, sizeof(buff), 0)) >= 0) {
    if (ret == 0)
        vTaskDelay(pdMS_TO_TICKS(5));
}
if (ret == -pdFREERTOS_ERRNO_EINVAL)
    return RC_OK;
if (ret == -pdFREERTOS_ERRNO_ENOTCONN || ret == -pdFREERTOS_ERRNO_ENOMEM)
    FreeRTOS_closesocket(sock);
else {
    printf("%s: ret = %d --> it may crash", __FUNCTION__, ret);
    FreeRTOS_closesocket(sock);
}
return RC_OK;

} ~~~~ Most of the time it works great. But sometimes the return value stored in 'ret' becomes -536818635 and the machine freezes. Maybe there's a memory write issue somewhere else in my program, but I'd be curious about your opinion...


[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]




Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.


Free TCP/IP and file system demos for the RTOS


Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists