php tcp keepcnt,关于LWIP的LWIP_TCP_KEEPALIVE功能,用过的麻烦进来看一下!~~

Please finish lwIP TCP workshop  before trying this one

Keepalive is an important feature in TCP connection to make sure we don't lose the link. For more info regarding keepalive in TCP, please visit Wikipedia.

1.Use the codes in  lwIP TCP workshop to create a TCP echo server first

2.In lwipopts.h, add "#define LWIP_TCP_KEEPALIVE 1"

3.

In tcp.h, modify the timing parameters according to your application. We chose the following for shorter time.

1.

TCPKEEPINTVL_DEFAULT to 3000UL (default was 7200000UL)

2.

TCP_KEEPCNT_DEFAULT to 1000UL (default was 75000UL)

3.

TCP_KEEPCNT_DEDAULT to 3U (default was 9U)

4.

Bring lwip_socket definition from sockets.c to sockets.h so that we can use it in the TCP thread

5.

Add the follow lines after lwip_socket:

struct lwip_socket *sock=get_socket(lSocket);

struct netconn* myconn = sock->conn;

myconn->pcb.tcp->so_options |= SOF_KEEPALIVE;

6.

Due to a bug in lwIP, the following patch is necessary until next release of lwIP, refer to https://savannah.nongnu.org/bugs/?25629:

1.

In src/core/tcp_out.c:tcp_keepalive(), locate the following lines

tcphdr->ackno = htonl(pcb->rcv_nxt);

TCPH_FLAGS_SET(tcphdr, 0);

tcphdr->wnd = htons(pcb->rcv_ann_wnd);

tcphdr->urgp = 0;

2.

Replace with:

tcphdr->ackno = htonl(pcb->rcv_nxt);

TCPH_FLAGS_SET(tcphdr, TCP_ACK);

tcphdr->wnd = htons(pcb->rcv_ann_wnd + 1);

tcphdr->urgp = 0;

7.

Add a call to tcp_tmr() every 250ms

8.

Build and run

9.

Telnet to it

10.

To verify keepalive,  run Wireshark. If you don't type anything for 3 seconds (defined by TCPKEEPINTVL_DEFAULT), keepalive packet be sent out once a second (defined by TCP_KEEPCNT_DEFAULT) will by lwIP, and the PC will ACK the keepalive packet.

11.

If you remove the ethernet cable, thus the lwIP will not receive the ACK from the PC. After 3 tries (defined by TCP_KEEPCNT_DEDAULT), lwIP will close the connection

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值