日期:2014-05-16 浏览次数:21117 次
int keepalive = 1; // 开启keepalive属性
int keepidle = 5; // 如该连接在5秒内没有任何数据往来,则进行探测
int keepinterval = 1; // 探测时发包的时间间隔为1 秒
int keepcount = 5; // 探测尝试的次数.如果第1次探测包就收到响应了,则后4次的不再发
socklen_t optlen = sizeof(socklen_t);
if (setsockopt(m_Socket, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepalive, optlen) < 0 ||
setsockopt(m_Socket, SOL_TCP, TCP_KEEPIDLE, (void *)&keepidle, optlen) < 0 ||
setsockopt(m_Socket, SOL_TCP, TCP_KEEPINTVL, (void *)&keepinterval, optlen) < 0 ||
setsockopt(m_Socket, SOL_TCP, TCP_KEEPCNT, (void *)&keepcount, optlen) < 0)
{
ClosePort();
return false;
}