lwip stack netconn api keep connection“keep-alive”

我目前正在使用lwip堆栈来实现modbus服务器,但“keep-alive”function不起作用。 有人可以看看我的问题吗?

码:

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 ); } 

我更改了以下设置:

 #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 

我的代码中还有其他必须做的事吗? 如果我试过这个,服务器将在传输HTML页面后结束连接。 我试图删除netconn_close(pxNetCon); 和/或netconn_delete(pxNetCon); ,但这不会给出正确的解决方案。 连接将保持打开状态,但我无法再次连接。

那么我还没有使用其他设置吗? 或者是否需要对代码进行修改?

LWIP_TCP_KEEPALIVE控制编译以支持TCP keepalive,默认情况下,每个连接都有keepalive关闭。

上面的应用程序使用netconn API来管理它的连接,并且没有netconn API来启用SO_KEEPALIVE选项。 为此,您需要使用LwIP的BSD类套接字API和setsockopt()调用:

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