分类: 网络与安全
2018-03-22 11:48:02
echo 120 > /proc/sys/net/ipv4/tcp_keepalive_time
echo 2 > /proc/sys/net/ipv4/tcp_keepalive_intvl
echo 1 > /proc/sys/net/ipv4/tcp_keepalive_probes
除了修改内核参数外,可以使用setsockopt修改socket参数,参考man 7 socket。
int nKeepAliveProbes = 1;
int nKeepAliveIntvl = 2;
int nKeepAliveTime = 120;
setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPCNT, (void*)&nKeepAliveProbes, sizeof(nKeepAliveProbes);
setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPINTVL, (void*)&nKeepAliveIntvl, sizeof(nKeepAliveIntvl);
setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE, (void*)&nKeepAliveTime, sizeof(nKeepAliveTime);