You must know the tcp_keepalive settings and tcp_keepalive settings.

Source: Internet
Author: User

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?
  1. # Include <sys/types. h>
  2. # Include <sys/socket. h>
  3. # Include <netinet/tcp. h>
  4. Int keepAlive = 1; // enable the keepalive attribute
  5. Int keepIdle = 1800; // if the connection does not have any data exchange within 1800 seconds, the test is performed.
  6. Int keepInterval = 3; // The packet interval during probe is 3 seconds.
  7. 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.
  8. Setsockopt (client_fd, SOL_SOCKET, SO_KEEPALIVE, (void *) & keepAlive, sizeof (keepAlive ));
  9. Setsockopt (client_fd, SOL_TCP, TCP_KEEPIDLE, (void *) & keepIdle, sizeof (keepIdle ));
  10. Setsockopt (client_fd, SOL_TCP, TCP_KEEPINTVL, (void *) & keepInterval, sizeof (keepInterval ));
  11. Setsockopt (client_fd, SOL_TCP, TCP_KEEPCNT, (void *) & keepCount, sizeof (keepCount ));

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.