You must know the tcp_keepalive settings and tcp_keepalive settings.
1. parameter settings
View related parameters
Sysctl-a | grep tcp_keepalive
Net. ipv4.tcp _ keepalive_intvl = 30
Net. ipv4.tcp _ keepalive_probes = 2
Net. ipv4.tcp _ keepalive_time = 160
Set related parameters
Sysctl-w net. ipv4.tcp _ keepalive_time = 7500
You can also directly open/etc/sysctl. conf
Add net. ipv4.tcp _ keepalive_time = 7500, save and exit
Make parameters take effect
Sysctl-p
2. parameter description
/Proc/sys/net/ipv4/tcp_keepalive_time
The frequency of keepalive messages sent by TCP when keepalive is in use. The default value is 2 hours.
/Proc/sys/net/ipv4/tcp_keepalive_intvl
The frequency of re-sending the probe when the probe is not confirmed. The default value is 75 seconds.
/Proc/sys/net/ipv4/tcp_keepalive_probes
The number of TCP keepalive test packets sent before the connection is determined to be invalid. The default value is 9. After this value is multiplied by tcp_keepalive_intvl, it determines how long a connection can not respond after it sends keepalive.
Tcp_keepalive_time: INTEGER
The default value is 7200 (2 hours)
When keepalive is enabled, the frequency at which TCP sends the keepalive message. (Due to factors such as network attacks, this attack is very frequent. Some cu friends once mentioned that if a connection is established on both sides, if no data or rst/fin messages are sent, will the duration be 2 hours or empty connection attacks? Tcp_keepalive_time is used to prevent this situation. I personally changed the value to 1800 seconds when performing the nat service)
Tcp_keepalive_probes: INTEGER
The default value is 9.
TCP sends a keepalive test to determine the number of times the connection has been disconnected. (Note: It is sent only when the SO_KEEPALIVE socket option is enabled. The number of requests does not need to be modified by default. Of course, this value can be shortened as appropriate. It is more appropriate to set it to 5)
Tcp_keepalive_intvl: INTEGER
The default value is 75.
The frequency of probe message sending. Multiply by tcp_keepalive_probes to get the time for the connection that has not responded since the start of the probe. The default value is 75 seconds, indicating that connections without activity will be dropped after about 11 minutes. (For common applications, this value is too large and can be changed as needed. Especially for web servers, this value needs to be changed to a smaller value. 15 is a suitable value)
$/Proc/sys/net/ipv4/tcp_keepalive_time
$/Proc/sys/net/ipv4/tcp_keepalive_intvl
$/Proc/sys/net/ipv4/tcp_keepalive_probes
The three parameters are related to TCP KeepAlive. The default value is:
Tcp_keepalive_time = 7200 seconds (2 hours)
Tcp_keepalive_probes = 9
Tcp_keepalive_intvl = 75 seconds
This means that if a TCP connection takes two hours after idle, the kernel initiates probe. if the probe fails for 9 times (75 seconds each time), the kernel will give up completely and the connection is deemed invalid. obviously, the above value is too large for the server. adjustable:
/Proc/sys/net/ipv4/tcp_keepalive_time 1800
/Proc/sys/net/ipv4/tcp_keepalive_intvl 30
/Proc/sys/net/ipv4/tcp_keepalive_probes 3
Tcp_keepalive_intvl: The frequency of message sending.
Tcp_keepalive_probes: TCP sends a keepalive test to determine the number of times the connection has been disconnected.
Tcp_keepalive_time: the frequency at which TCP sends keepalive messages when keepalive is enabled.
Linux code
[Cpp]View plaincopyprint?
- # Include <sys/types. h>
- # Include <sys/socket. h>
- # Include <netinet/tcp. h>
- Int keepAlive = 1; // enable the keepalive attribute
- Int keepIdle = 1800; // if the connection does not have any data exchange within 1800 seconds, the test is performed.
- Int keepInterval = 3; // The packet interval during probe is 3 seconds.
- Int keepCount = 2; // Number of probe attempts. If the response is received for the first probe packet, the last few attempts will not be sent again.
- Setsockopt (client_fd, SOL_SOCKET, SO_KEEPALIVE, (void *) & keepAlive, sizeof (keepAlive ));
- Setsockopt (client_fd, SOL_TCP, TCP_KEEPIDLE, (void *) & keepIdle, sizeof (keepIdle ));
- Setsockopt (client_fd, SOL_TCP, TCP_KEEPINTVL, (void *) & keepInterval, sizeof (keepInterval ));
- Setsockopt (client_fd, SOL_TCP, TCP_KEEPCNT, (void *) & keepCount, sizeof (keepCount ));