The Keepalive mechanism of the HeartBeat Heartbeat and TCP Protocol is not specified

Many application layer protocols have a HeartBeat mechanism, usually the client sends to the server every short period of time Send a data packet to inform the server that it is still online and transmit some data that may be necessary. The typical protocol for using heartbeat packets is IM, such as QQ/MSN/Fetion protocols.

Students who have studied TCP/IP should know that the two aspects of the transmission layer The main protocols are UDP and TCP. UDP is connectionless and packet-oriented, while TCP is a connection-oriented, stream-oriented protocol.

So it’s very easy to understand that clients using UDP protocol (such as early “OICQ”, I heard that OICQ.com has been squashed over the past two days, which is an ancient memory) It needs to send heartbeat packets to the server regularly to tell the server to be online.

However, MSN and current QQ often use TCP connection. Although the underlying TCP/IP provides an optional KeepAlive (ACK-ACK packet) mechanism, they also implement higher-level heartbeat packets. It seems to be a waste of traffic and CPU, a bit inexplicable.

Checked specifically, TCP’s KeepAlive mechanism is like this, first of all It seems that it is not turned on by default. You need to use setsockopt to set SOL_SOCKET.SO_KEEPALIVE to 1 to turn it on, and you can set three parameters tcp_keepalive_time/tcp_keepalive_probes/tcp_keepalive_intvl, which respectively indicate how long the connection has been idle to start sending keepalive ack packets and how many ack packets When the other party is dead if you don’t reply, how long is the interval between the two ack packets? The default value under Ubuntu Server 10.04 I tested is 7200 seconds (2 hours, or it hurts!), 9 times, 75 seconds . Then there is a timeout window for the connection. If there is no communication between the connections, this time window will gradually decrease. When it decreases to zero, the TCP protocol will send an empty data packet with an ACK flag to the other party ( KeepAlive probe). After the other party receives the ACK packet, if the connection is normal, it should reply with an ACK; if there is an error in the connection (for example, the other party restarts and the connection status is lost), it should reply with an RST; if the other party does not reply, The server sends an ACK every intvl time. If consecutive probes packets are ignored, the connection is disconnected.

Here is a very detailed introduction article: http: //tldp.org/HOWTO/html_single/TCP-Keepalive-HOWTO , including the introduction and related of KeepAlive Kernel parameters, C programming interface, and how to enable KeepAlive mechanism for existing applications (with or without modification of the source code) are worth reading in detail.

Section 2.4 of this article says “Preventing disconnection due to network inactivity”, to prevent the connection interruption caused by the inactive network connection (no data packets for a long time). What is said is that many network devices, especially NAT routers, cannot be Keep all connections on it, so when necessary, some inactive connections will be selected in the connection pool and kicked off. The typical approach is LRU, which will drop the connection that has no data for the longest time. By using TCP’s KeepAlive mechanism (modify the time parameter), the connection can generate some ack packets every short time to reduce the risk of being dropped. Of course, the cost is additional network and CPU burden.

As mentioned earlier, many IM protocols implement their own heartbeat mechanism, and It is not directly dependent on the underlying mechanism, and I don’t know what the real reason is.

In my opinion, some simple protocols use the underlying mechanism directly Yes, it is completely transparent to the upper layer, which reduces the difficulty of development and does not need to manage the status of the connection. Those protocols that implement the heartbeat mechanism by themselves should expect to transmit some data while sending heartbeat packets, so that the server can learn more about the status. For example, some clients like to collect user information… Anyway, it is necessary to send a package, it is better to stuff some data, otherwise the header of the package is wasted…

This is probably the case. If someone knows the real reason, please feel free to enlighten me.


@2012-04-21
< br style="font-family:Tahoma,Arial; font-size:14px; line-height:19.4599990844727px"> ps By consulting a colleague who has done IM, the reference answer should be that the heartbeat mechanism implemented by myself is universal and can ignore the underlying UDP or TCP protocol. If only the TCP protocol is used, then it is sufficient to directly use the KeepAlive mechanism.


Please indicate the source for reprinting http://www.felix021.com/blog/read.php?2076 , if reprinted text, please indicate the original source, thank you:)
RSS subscription address: http://www.felix021.com/blog/feed.php .

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2572 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.