Monitoring of TCP disconnection in CentOS

Source: Internet
Author: User
TCP is normally disconnected, and both parties (both the server and the client) can know. However, unusual disconnection occurs. for example, if the network cable is directly Unplugged, the following two methods can be used to detect the network in a short time. I. Heartbeat packet mechanism the Heartbeat packet mechanism is a common mechanism in online game design. From the user level, send your own packets to determine the connection status of the other party. It can be used flexibly as needed. For example, a minimum number is sent in 20 seconds.

TCP is normally disconnected, and both parties (both the server and the client) can know. However, unusual disconnection occurs. for example, if the network cable is directly Unplugged, the following two methods can be used to detect the network in a short time.

I, Heartbeat packet mechanism

Heartbeat packet mechanism is a common mechanism in online game design. From the user level, send your own packets to determine the connection status of the other party. It can be used flexibly as needed. For example, a minimum packet is sent in 20 seconds (or some other data can be carried according to the actual situation ). If no response is sent, the other party is determined to be offline.

II, Using the tcp_keepalive mechanism

The TCP mechanism is used to monitor the tcp connection status at the system level by setting system parameters. The following are the methods in linux:

1. First, check the system tcp_keepalive parameters (centos and Ubuntu)

# Sysctl-a | grep tcp_keepalive

Shown as follows:

Net. ipv4.tcp _ keepalive_intvl = 75
Net. ipv4.tcp _ keepalive_probes = 9
Net. ipv4.tcp _ keepalive_time = 7200

The three parameters are described as follows:

Tcp_keepalive_time parameter:

The frequency of sending keepalive messages over TCP. the default value is 7200 seconds, that is, two hours.

Keepalive_intvl parameters:

The frequency of re-sending the probe when the probe is not confirmed. The default value is 75 seconds.

Keepalive_probes parameter:

The number of TCP keepalive test packets sent before the connection is determined to be invalid. The default value is 9.

--------------------------------------------------------------------

2. modify the tcp_keepalive parameters of the system.

After knowing the definitions and functions of the above three system parameters, let's modify these three parameters:

Modify the/etc/sysctl. conf file:

# Vi/etc/sysctl. conf

Add the following three lines to the open file:

Net. ipv4.tcp _ keepalive_intvl = 1
Net. ipv4.tcp _ keepalive_probes = 1
Net. ipv4.tcp _ keepalive_time = 5

Save the file.

--------------------------------------------------------------------

3. restart the server.

# Reboot

Note: or temporarily enable the system settings to take effect: # sysctl-p

 

--------------------------------------------------------------------

4. modify the program code.

Note: Configuring only the kernel parameters is not enough. you must set socket options during programming. the call function is
Socket. setsockopt (level, optname, value)
Example:
If the level is SOL_SOCKET, the option is SO_KEEPALIVE and the value is Boolean.

Socket. setsockopt (socket. SOL_SOCKET, socket. SO_KEEPALIVE, True)

 

In addition, you can set TCP options (the set level is SOL_TCP instead of SOL_SOCKET) to overwrite the global settings of the system.
TCP_KEEPCNT corresponds to tcp_keepalive_probes
TCP_KEEPIDLE corresponds to tcp_keepalive_time
TCP_KEEPINTVL corresponds to tcp_keepalive_intvl

 

--------------------------------------------------------------------

References:

Http://blog.sina.com.cn/s/blog_a2d4803001013hrk.html

Http://blog.chinaunix.net/uid-23849526-id-274740.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.