int keepAlive = 1; // 开启 keepalive 属性. 缺省值: 0(关闭)
int keepIdle = 60; // 如果在 60 秒内没有任何数据交互,则进行探测. 缺省值:7200(s)
int keepInterval = 5; // 探测时发探测包的时间间隔为 5 秒. 缺省值:75(s)
int keepCount = 2; // 探测重试的次数. 全部超时则认定连接失效..缺省值:9(次)
setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void*)&keepAlive, sizeof(keepAlive));
setsockopt(s, SOL_TCP, TCP_KEEPIDLE, (void*)&keepIdle, sizeof(keepIdle));
setsockopt(s, SOL_TCP, TCP_KEEPINTVL, (void*)&keepInterval, sizeof(keepInterval));
setsockopt(s, SOL_TCP, TCP_KEEPCNT, (void*)&keepCount, sizeof(keepCount));
这个是 server 端还是 client 端设置,还是都设置?
1
jimzhong 2018-01-19 02:54:56 +08:00
|
2
Panic 2018-01-19 08:25:21 +08:00
保活一般都是应用自己加的,基本没人用 tcp 层面的 KEEPALIVE
|
3
thomaswang OP @Panic 能说说自己怎么加吗,服务端(对每个连接)、客户端都要设置计时,到时间就发个包给对放 ?
|
4
Panic 2018-01-20 10:14:55 +08:00
|