I have a simple HTTP/WebSocket server/client application with the following setup:
cfd
from client, it sets cfd
to be non-blocking, and sets its keepalive like this int flags =1;
if (setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (void *)&flags, sizeof(flags))) { perror("ERROR: setsocketopt(), SO_KEEPALIVE"); exit(0); };
cfd
(non-blocking)netstat
to watch the state of the connection between the server and the client. I saw it remains in "ESTABLISHED" state for around 960 seconds.Now my question is where the 960 seconds could come from. I thought it is controlled by
net.ipv4.tcp_keepalive_time
, HOWEVER, no matter how I changed its value by sudo sysctl -w net.ipv4.tcp_keepalive_time=XXX
, the TCP socket remains in ESTABLISHED state for 960 seconds, instead of the value of net.ipv4.tcp_keepalive_time
I believe you are looking for net.ipv4.tcp_retries2
which controls how many times Linux will continue to retry before giving up and closing the connection. It defaults to 15 and given that the maximum retransmission timeout is 60 seconds, your 960 second observed time is in line with what is expected.
If you reduce tcp_retries2, it will retransmit fewer times and close the connection faster.
You may also be interested in looking at some of the answers to this StackOverflow question