Conara
Conara

Reputation: 13

lwip stack netconn api keep connection "keep-alive"

I'm currently working with the lwip stack to implement a modbus server, but the "keep-alive" function doesn't work. Can someone look to my problem?

code:

static void prvweb_ParseHTMLRequest( struct netconn *pxNetCon )
{
struct netbuf *pxRxBuffer;
portCHAR *pcRxString;
unsigned portSHORT usLength;
static unsigned portLONG ulPageHits = 0;

    while(netconn_recv( pxNetCon, &pxRxBuffer) != ERR_OK)
    {
        vTaskDelay( webSHORT_DELAY );
    }
    if( pxRxBuffer != NULL )
    {
        /* Where is the data? */
        netbuf_data( pxRxBuffer, ( void * ) &pcRxString, &usLength );

        if(( NULL != pcRxString               )
        && ( !strncmp( pcRxString, "GET", 3 ) ))
        {
            /********************************* 
                    Generate HTML page 
            *********************************/

            /* Write out the dynamically generated page. */
            netconn_write( pxNetCon, cDynamicPage, (u16_t) strlen( cDynamicPage ), NETCONN_COPY );
        }
        netbuf_delete( pxRxBuffer );
    }

    netconn_close( pxNetCon );
    netconn_delete( pxNetCon );
}

I changed the following settings:

#ifndef LWIP_TCP_KEEPALIVE
#define LWIP_TCP_KEEPALIVE              1
#endif



#ifndef  TCP_KEEPIDLE_DEFAULT
#define  TCP_KEEPIDLE_DEFAULT     7200000UL /* Default KEEPALIVE timer in milliseconds */
#endif

#ifndef  TCP_KEEPINTVL_DEFAULT
#define  TCP_KEEPINTVL_DEFAULT    75000UL   /* Default Time between KEEPALIVE probes in milliseconds */
#endif

#ifndef  TCP_KEEPCNT_DEFAULT
#define  TCP_KEEPCNT_DEFAULT      9U        /* Default Counter for KEEPALIVE probes */
#endif

Are there other things I must do in my code? If i tried this the server will end the connection after transmit the HTML page. I tried to delete netconn_close( pxNetCon ); and/or netconn_delete( pxNetCon ); ,but this will not give the right solution. The connection will stay open, but I cannot connect again.

So are there other settings I didn't use? Or are there modification in the code needed?

Upvotes: 0

Views: 9626

Answers (2)

user13755530
user13755530

Reputation: 11

How to enable Keep-Alive when Raw API

  1. in lwipopts.h
#define LWIP_TCP_KEEPALIVE  1 // enable "kepp-alive"
#define TCP_KEEPIDLE_DEFAULT    1000 // keep_idle : dont' send keep-alive until keep_idle after connecting
#define TCP_KEEPCNT_DEFAULT     9U // keep_cnt : increase when no response after sending keep-alive every keep_intvl
  1. when call tcp_connect(pcb, ...)
pcb->keep_intvl = 1000; // send "keep-alive" every 1000ms
  1. in loop()...
if(pcb_client->keep_cnt==pcb_client->keep_cnt_sent)
{
    tcp_client_connection_close(pcb_client, client_s);
}

this settings make timeout 10s after server unplugged

Upvotes: 1

Joel Cunningham
Joel Cunningham

Reputation: 671

LWIP_TCP_KEEPALIVE controls compiling in support for TCP keepalives and by default each connection has keepalives off.

The above application is using the netconn API for managing it's connection and there is no netconn API to enable the SO_KEEPALIVE option. In order to do this, you'll need to be using LwIP's BSD-like sockets API and the setsockopt() call:

int optval = 1; setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval));

Upvotes: 5

Related Questions