keep-alive

tcp_keepalive_time

the interval between the last data packet sent (simple ACKs are not considered data) and the first keepalive probe; after the connection is marked to need keepalive, this counter is not used any further

tcp_keepalive_intvl

the interval between subsequential keepalive probes, regardless of what the connection has exchanged in the meantime

tcp_keepalive_probes

the number of unacknowledged probes to send before considering the connection dead and notifying the application layer

View variables:

cat /proc/sys/net/ipv4/tcp_keepalive_time

cat /proc/sys/net/ipv4/tcp_keepalive_intvl

cat /proc/sys/net/ipv4/tcp_keepalive_probes

Fast edit:

echo «900» > /proc/sys/net/ipv4/tcp_keepalive_time

echo «10» > /proc/sys/net/ipv4/tcp_keepalive_intvl

echo «15» > /proc/sys/net/ipv4/tcp_keepalive_probes

Forever edit:

nano /etc/sysctl.conf:

net.ipv4.tcp_keepalive_time=900

net.ipv4.tcp_keepalive_intvl=10

net.ipv4.tcp_keepalive_probes=15

 

C++:

setsockopt ( sock, SOL_SOCKET, SO_KEEPALIVE, 1, sizeof (int) );

setsockopt ( sock, SOL_TCP, TCP_KEEPCNT, 20, sizeof (int) );

setsockopt ( sock, SOL_TCP, TCP_KEEPIDLE, 180, sizeof (int) );

setsockopt ( sock, SOL_TCP, TCP_KEEPINTVL, 60, sizeof (int) );