Home >Industrial IoT>Industrial IoT
Detailed explanation of TCP keep-alive mechanism

TCP/IP protocol is currently the most commonly used protocol in computer communication networks, and it has also been integrated into all aspects of life, whether it is the http/https protocol used to browse the web, the MQTT/MQTTS protocol used by Internet of Things devices, and the ftp protocol used to download files. , Modbus TCP protocol used in industrial Ethernet and many other application layer protocols are based on TCP/IP protocol. While transmitting data, the TCP/IP protocol also provides the following functions:

1. Slow start: network environment congestion detection

2. Retransmission mechanism: ensuring data integrity and connection reliability

3. Sliding window: flow control, reducing network environment pressure and avoiding data packet loss

4. Keep-alive: Link anomaly detection

TCP keep-alive.jpg

Introduction to TCP protocol

TCP (Transmission Control Protocol) is a connection-oriented, reliable, byte stream-based transport layer communication protocol. It provides a full-duplex, connection-oriented, reliable byte stream service, and is the most important and complex protocol in the TCP/IP protocol suite

TCP&IP.jpg

Main features of TCP

Connection-oriented: Before communication, you need to shake hands three times to establish a connection, and wave four times after communication to release the connection.

Reliable: Ensure the reliability of data transmission by solving message out-of-order/loss, timeout retransmission, congestion control, sliding window, checksum and other methods through sequence numbers.

Byte stream: There are no fixed message boundaries, and data is transmitted in the form of byte stream in TCP.

Full duplex: Both ends of the communication can send data to each other at any time, either the client or the server.

TCP assumes that it can obtain simple, possibly unreliable, datagram services from lower-level protocols. In principle, TCP should be able to operate on top of a variety of communications systems, from hardwired connections to packet-switched or circuit-switched networks.

TCP Keep-alive application scenarios

Generally, the connection types between TCP clients and servers can be divided into:

1. Short link: After the client connects to the server, it starts interacting with the server, requesting resources, reporting data, etc. After the interaction is completed, it disconnects from the server, such as HTTP protocol, etc.

2. Long connection: After the client connects to the server, the data may not be transferred immediately, but the connection status will always be maintained, and both parties generally will not actively disconnect, such as the MQTT protocol, etc.

It should be noted that neither long connection nor short connection is specified by the TCP protocol itself. TCP only provides the application layer with methods and resource management for establishing and disconnecting connections.

 

It can be imagined that when the client and the server are in a long connection state, if the server suddenly loses power, the server will not be able to notify the client of the abnormal situation, and the client will not be able to detect the server abnormality. Only when the client sends data to the server, the client can know about the server exception due to the timeout mechanism. And the data is naturally discarded. Moreover, if the abnormal connection cannot be released, it will also lead to the consumption and waste of system resources. Therefore, under a long connection, you can enable TCP's Keep-alive mechanism to avoid abnormal connections caused by unexpected power outages, crashes, crashes, restarts, or unreasonable disconnection of the intermediate routing network.

TCP Keep-alive mechanism

Related parameters

SO_KEEPALIVE: Whether to enable keep alive

TCP_KEEPIDLEStart keeplives after this period

TCP_KEEPINTVLInterval between keepalives

TCP_KEEPCNTNumber of keepalives before death


SO_KEEPALIVE: Keep-alive can be bidirectional, that is, the client can actively send it to the server, or the server can actively send it to the client. After SO_KEEPALIVE is enabled, the keep-alive mechanism is enabled.


TCP_KEEPIDLE: When the client and server do not exchange data for the idle time of TCP_KEEPIDLE, TCP will send a detection packet to the other party.

 

TCP_KEEPINTVL: If the last detection packet did not receive a response, TCP_KEEPINTVL will be used as the next detection packet interval.

 

TCP_KEEPCNT: When no response is received after sending the TCP_KEEPCNT number of consecutive probe packets, the local computer will release the current connection resources and notify the application layer that the connection is disconnected.

TCP Keep-alive instance

Normal detection packet

TCP keep-alive-1.png

Offline process

TCP keep-alive-2.png

Next, test KeepAlive disconnection: After establishing a normal TCP connection, unplug the network cable: Capture the packet again as shown in the figure below:

TCP keep-alive-3.png

Related Article: