问题
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?
回答1:
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));
来源:https://stackoverflow.com/questions/29492995/lwip-stack-netconn-api-keep-connection-keep-alive