簡體   English   中英

lwip stack netconn api 保持連接“保持活動”

[英]lwip stack netconn api keep connection “keep-alive”

我目前正在使用 lwip 堆棧來實現 modbus 服務器,但“保持活動”功能不起作用。 有人可以看看我的問題嗎?

代碼:

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 保活,默認情況下每個連接都關閉保活。

上面的應用程序使用 netconn API 來管理它的連接,並且沒有 netconn API 來啟用 SO_KEEPALIVE 選項。 為此,您需要使用 LwIP 的類 BSD 套接字 API 和 setsockopt() 調用:

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

如何在 Raw API 時啟用 Keep-Alive

  1. 在 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. 當調用 tcp_connect(pcb, ...)
pcb->keep_intvl = 1000; // send "keep-alive" every 1000ms
  1. 在循環()...
if(pcb_client->keep_cnt==pcb_client->keep_cnt_sent)
{
    tcp_client_connection_close(pcb_client, client_s);
}

此設置使服務器拔出后超時 10 秒

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM