UltimaSerial AVR32 lwIP: How to enable keepalive 

Data logger


Windaq add-ons
Windaq Add-ons




Ultimaserial XChart




Ultimaserial Classroom

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

Find out how to upgrade to the latest lwIP



 Last update: 07/31/15