KeepAlive and Heartbeat packs in TCP connection probes. Keywords: tcp keepalive, Heartbeat, keep alive
KeepAlive and Heartbeat packs in TCP connection probes. Keywords: tcp keepalive, Heartbeat, keep aliveCategory: Linux2013-11-29 17:46 3855 People read comments (1) Favorites Report Linux TCP Heartbeat KeepAlive
1. The necessity of the TCP's keeping alive
1 many firewalls, etc. for the idle socket automatic shutdown
2 for abnormal disconnect, the server is not able to detect. In order to reclaim resources, a detection mechanism must be provided.
2. Factors leading to TCP disconnection
If the network is normal, the socket also closes gracefully with the close operation, so everything is perfect. However, in many cases, such as network cable failure, the client side of the sudden power outage or crash, and so on, these conditions server does not normally detect the disconnection of the connection.
3. Two ways of keeping alive:
1) application level of the heartbeat mechanism
Custom Heartbeat message headers. The general client sends unsolicited, the server responds after receiving (also can not respond). Not in detail here.
PS: Someone from the functional point of view of the software listed a third way, that is, through the Third-party software to detect, determine the validity of the connection. This is a very restrictive approach and is not part of the software's internal functionality implementation. No discussion is made.
2 The TCP protocol self-preserving function
Turn on the keep-alive function. Specific properties can also be set through the API.
4. The advantages and disadvantages of two kinds of methods
The TCP protocol has its own function, which is simple to use and reduces the complexity of the application layer code. Speculation will also save traffic, because generally the application layer of data transfer to the protocol layer will be added to the end of the packet. The detection packets provided by the TCP protocol will theoretically be more subtle (with fewer bytes to accomplish more goals) and consume less traffic.
By applying the heartbeat of the application layer implemented by itself, it is possible to define a message type for the heartbeat message. Is the application of the normal message packet, but the special point of the package, specifically used to check the live only, usually relatively small, probably only the message header can be, unless the need for additional information.
The benefits of the application layer heartbeat I personally understand that there are two points:
One is more flexible, because the protocol layer of heartbeat can only provide the most pure check live function, but the application layer can control itself at will, including the Protocol may provide a second level, but you want to do any of the milliseconds (although practically no such time level heartbeat), the packet can even carry additional information, These are the places of flexibility.
The second is universal, the application layer of heartbeat is not dependent on protocol. If one day do not use TCP to change to UDP, the protocol layer does not provide a heartbeat mechanism, but your application layer of heartbeat is still common, may only need to make a little change can continue to use.
The bad place for the application layer heartbeat is also obvious, increase the development effort, because the application specific network framework, may also increase the complexity of the code structure. Then according to the above speculation, the application layer heartbeat flow consumption is still greater, after all, this is essentially a normal packet.
5. What kind of heartbeat do you choose?
The 4th section has been elaborated, so if you can determine the possibility of your replacement agreement is very small, and only need to check live function, then use the protocol with the absolute OK, the use of simple and efficient. Some conceited people always like to use their own, to replace the mature protocol brought things, instead of the system core to provide things, in fact, often your application layer to achieve things, are more clumsy. On the Internet read some of the keep-alive of the agreement is not reliable, are also more utopian and take for granted, have not come up with any factual arguments or experimental data. We have a point of view, Welcome to Exchange Kazakhstan ~
6. How Unix-like platforms use keep-alive
KeepAlive default is off, because although the traffic is very small, after all, is overhead. The user is therefore required to manually open. There are two ways to turn it on.
1 in the code for each socket for individual settings, the use of flexible.
In addition to the keepalive switch, there are Keepidle, Keepinterval, Keepcount 3 properties, simple to use, as follows: [CPP] View plain copyintkeepAlive = 1; Open the KeepAlive property. Default value: 0 (OFF)intKeepidle = 60; If there is no data interaction within 60 seconds, the probe is performed. Default value: 7200 (s)intKeepinterval = 5; Probe packets are sent at intervals of 5 seconds. Default value: (s)intKeepcount = 2; The number of probe retries. All timeouts determine that the connection is invalid. Default value: 9 (Times) setsockopt (S, Sol_socket, So_keepalive,void*) &keepalive,sizeof(keepAlive)); SetSockOpt (S, sol_tcp, Tcp_keepidle,void*) &keepidle,sizeof(Keepidle)); SetSockOpt (S, sol_tcp, TCP_KEEPINTVL,void*) &keepinterval,sizeof(Keepinterval)); SetSockOpt (S, sol_tcp, tcp_keepcnt,void*) &keepcount,sizeof(Keepcount));
Need #include <netinet/tcp.h> when using, otherwise 3 macros such as SOL_TCP and Tcp_keepidle cannot be found.
PS: Can not help spit slot, the Internet a large number of not responsible for the reprint, the same search results, many people did not have any verification. It's not a small thing to look for a file like this. Most of the words in the post are not available.
2 Modify the configuration file, the entire system for all sockets valid.
We can use the cat command to see these default values in the system.
#cat/proc/sys/net/ipv4/tcp_keepalive_time 7200
#cat/PROC/SYS/NET/IPV4/TCP_KEEPALIVE_INTVL 75
#cat/proc/sys/net/ipv4/tcp_keepalive_probes 9
Modify them:
#echo >/proc/sys/net/ipv4/tcp_keepalive_time
#echo 5 >/PROC/SYS/NET/IPV4/TCP_KEEPALIVE_INTVL
#echo 3 >/proc/sys/net/ipv4/tcp_keepalive_probes
Link Recommendation:
The keepalive mechanism of heartbeat heartbeat packet and TCP protocol
TCP Keepalive HOWTO