简体   繁体   English

lwip stack netconn api 保持连接“保持活动”

[英]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.我目前正在使用 lwip 堆栈来实现 modbus 服务器,但“保持活动”功能不起作用。 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.如果我试过这个,服务器将在传输 HTML 页面后结束连接。 I tried to delete netconn_close( pxNetCon );我试图删除 netconn_close( pxNetCon ); and/or netconn_delete( pxNetCon );和/或 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 controls compiling in support for TCP keepalives and by default each connection has keepalives off. LWIP_TCP_KEEPALIVE 控制编译以支持 TCP 保活,默认情况下每个连接都关闭保活。

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.上面的应用程序使用 netconn API 来管理它的连接,并且没有 netconn API 来启用 SO_KEEPALIVE 选项。 In order to do this, you'll need to be using LwIP's BSD-like sockets API and the setsockopt() call:为此,您需要使用 LwIP 的类 BSD 套接字 API 和 setsockopt() 调用:

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

How to enable Keep-Alive when Raw API如何在 Raw API 时启用 Keep-Alive

  1. in lwipopts.h在 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, ...)当调用 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此设置使服务器拔出后超时 10 秒

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM