本文介绍了lwip stack netconn api保持连接“保持活动"状态.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用lwip堆栈来实现Modbus服务器,但是保持活动"功能不起作用.有人可以看我的问题吗?

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?

代码:

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); ,但这不会提供正确的解决方案.连接将保持打开状态,但我无法再次连接.

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?

推荐答案

LWIP_TCP_KEEPALIVE控件进行编译,以支持TCP keepalive,默认情况下,每个连接均关闭keepalive.

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

以上应用程序正在使用netconn API来管理其连接,并且没有netconn API可以启用SO_KEEPALIVE选项.为此,您需要使用LwIP的类似于BSD的套接字API和setsockopt()调用:

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

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

这篇关于lwip stack netconn api保持连接“保持活动"状态.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 07:09