我目前正在使用 lwip 堆栈来实现 modbus 服务器,但是“keep-alive”功能不起作用。有人可以看看我的问题吗?
代码:
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);,但这不会给出正确的解决方案。连接将保持打开状态,但我无法再次连接。
那么还有其他我没有使用的设置吗?还是需要对代码进行修改?