- 论坛徽章:
- 0
|
SetKeepLive的代码如下。我刚才试过,把这行代码注释掉,问题仍然存在。- int Network::SetKeepLive(int sockfd,int idle,int intvl,int cnt)
- {
- int opt = 1;
- if (setsockopt(sockfd,SOL_SOCKET,SO_KEEPALIVE,reinterpret_cast<char*>(&opt),sizeof(opt)) < 0)
- {
- return RET_ERROR;
- }
- #ifdef __linux__
- if (setsockopt(sockfd,IPPROTO_TCP,TCP_KEEPIDLE, &idle, sizeof(idle)) < 0 ||
- setsockopt(sockfd,IPPROTO_TCP,TCP_KEEPINTVL,&intvl,sizeof(intvl))< 0 ||
- setsockopt(sockfd,IPPROTO_TCP,TCP_KEEPCNT, &cnt, sizeof(cnt)) < 0)
- {
- return RET_ERROR;
- }
- #elif _WIN32
- unsigned long dw;
- tcp_keepalive live,liveout;
- live.onoff = 1;
- live.keepalivetime = idle * 1000;
- live.keepaliveinterval = intvl * 1000;
- if(WSAIoctl(sockfd,SIO_KEEPALIVE_VALS,&live,sizeof(live),&liveout,sizeof(liveout),&dw,NULL,NULL) < 0)
- {
- return RET_ERROR;
- }
- #else
- if (setsockopt(sockfd,IPPROTO_TCP,TCP_KEEPIDLE, &idle, sizeof(idle)) < 0 ||
- setsockopt(sockfd,IPPROTO_TCP,TCP_KEEPINTVL,&intvl,sizeof(intvl))< 0 ||
- setsockopt(sockfd,IPPROTO_TCP,TCP_KEEPCNT, &cnt, sizeof(cnt)) < 0)
- {
- return RET_ERROR;
- }
- #endif
- return RET_SUCCESS;
- }
复制代码 |
|