- 论坛徽章:
- 0
|
回复 #2 yangsf5 的帖子
真是不好意思 ,我上班的地方上网不方便,哎 真是 !见谅!
我的 部分代码
//keepalive 事件间隔
int keepAlive = 1; // 开启keepalive属性
int keepIdle = 18; // 如该连接在18秒内没有任何数据往来,则进行探测
int keepInterval = 5; // 探测时发包的时间间隔为5 秒
int keepCount = 3; // 探测尝试的次数.如果第1次探测包就收到响应了,则后2次的不再发.
if(setsockopt(m_Accept_sd, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepAlive, sizeof(keepAlive)) != 0)
{
printf("error code = %d,error string = %s \n", errno, strerror(errno));
return FALSE;
}
if(setsockopt(m_Accept_sd, SOL_TCP, TCP_KEEPIDLE, (void*)&keepIdle, sizeof(keepIdle)) != 0)
{
printf("error code = %d,error string = %s \n", errno, strerror(errno));
return FALSE;
}
if(setsockopt(m_Accept_sd, SOL_TCP, TCP_KEEPINTVL, (void *)&keepInterval, sizeof(keepInterval)) != 0)
{
printf("error code = %d,error string = %s \n", errno, strerror(errno));
return FALSE;
}
if(setsockopt(m_Accept_sd, SOL_TCP, TCP_KEEPCNT, (void *)&keepCount, sizeof(keepCount)) != 0)
{
printf("error code = %d,error string = %s \n", errno, strerror(errno));
return FALSE;
}
这样设置就可以设置 keepalive 了
[ 本帖最后由 liujq110 于 2009-5-14 10:14 编辑 ] |
|