[From QnA]TCP Keep Alive Option


Could I use TCP Keep Alive in WizFi210?

Wizfi220 limited AP 모드에서.. 클라이언트 연결끊겼을때 문제..

To set the keep alive function you need to use below command.

Regarding the SETSOCKOPT, please refer to below information.

  • CID: is the socket identifier received after opening a connection.
  • Type: is the type of the option to be set
    (SOCKET: 65535, IP : 0, TCP: 6)
  • Parameter: The Option name to be set. Accepts hex values.
    (TCP_MAXRT : 10(Hex), TCP_KEEPALIVE: 4001(Hex), SO_KEEPALIVE: 8(Hex), TCP_KEEPALIVE_CNT: 4005(Hex))
  • Value: The value to be set. This in seconds (Ex: 30 ? 30 seconds)
  • Length: The length of the value in bytes (Ex: in above case it is 4, basically it tells the type of the value is integer, Short or Char) (Integer : 4, Short : 2, Char :1)
    This command returns the standard command response (paragraph heading Commands for Command Processing Mode in this chapter) to the serial interface.

To enable the TCP Keepalive is:

[quote]AT+SETSOCKOPT= 0,65535,8,1,4 : Enable SO_KEEPALIVE option at base socket level. Without enabling this TCP_KEEPALIVE will not work.
AT+SETSOCKOPT= 0,6,4001,600,4 : Enable TCP_KEEPALIVE option at TCP level with timeout as 600 seconds.
[/quote]Note: The default keepalive count is 8 so the minimum keepalive timeout is 8*75=600 seconds. To reduce the keepalive timeout further, set the Keepalive count first to an appropriate value and set the keepalive timeout.

[quote]Ex: To set the keep alive timeout to 75 seconds:
AT+SETSOCKOPT =0,6,4005,1,4 : Configure TCP Keep Alive Probe Sending count at just 1.
AT+SETSOCKOPT= 0,6,4001,75,4 : Enable TCP_KEEPALIVE option at TCP level with 75 seconds as Keep Alive timeout.[/quote]


Regarding the TCP Keepalive, please refer to the below.

2.1. What is TCP keepalive?

The keepalive concept is very simple: when you set up a TCP connection, you associate a set of timers. Some of these timers deal with the keepalive procedure. When the keepalive timer reaches zero, you send your peer a keepalive probe packet with no data in it and the ACK flag turned on. You can do this because of the TCP/IP specifications, as a sort of duplicate ACK, and the remote endpoint will have no arguments, as TCP is a stream-oriented protocol. On the other hand, you will receive a reply from the remote host (which doesn’t need to support keepalive at all, just TCP/IP), with no data and the ACK set.

If you receive a reply to your keepalive probe, you can assert that the connection is still up and running without worrying about the user-level implementation. In fact, TCP permits you to handle a stream, not packets, and so a zero-length data packet is not dangerous for the user program.

This procedure is useful because if the other peers lose their connection (for example by rebooting) you will notice that the connection is broken, even if you don’t have traffic on it. If the keepalive probes are not replied to by your peer, you can assert that the connection cannot be considered valid and then take the correct action.

2.2. Why use TCP keepalive?

You can live quite happily without keepalive, so if you’re reading this, you may be trying to understand if keepalive is a possible solution for your problems. Either that or you’ve really got nothing more interesting to do instead, and that’s okay too.

Keepalive is non-invasive, and in most cases, if you’re in doubt, you can turn it on without the risk of doing something wrong. But do remember that it generates extra network traffic, which can have an impact on routers and firewalls.