#include "netinet/tcp.h"
int keepAlive = 1; //设定KeepAlive
int keepIdle = 30; //开始首次KeepAlive探测前的TCP空闭时间
int keepInterval = 30; //两次KeepAlive探测间的时间间隔
int keepCount = 3; //判定断开前的KeepAlive探测次数
if(setsockopt(sockfd,SOL_SOCKET,SO_KEEPALIVE,(void*)&keepAlive,sizeof(keepAlive)) == -1)
{
printf("Socket Option setting(SO_KEEPALIVE) fail !\n");
return FALSE;
}
if(setsockopt(sockfd,SOL_TCP,TCP_KEEPIDLE,(void *)&keepIdle,sizeof(keepIdle)) == -1)
{
printf("Socket Option setting(SO_KEEPALIVE) fail !\n");
return FALSE;
}
if(setsockopt(sockfd,SOL_TCP,TCP_KEEPINTVL,(void *)&keepInterval,sizeof(keepInterval)) == -1)
{
printf("Socket Option setting(TCP_KEEPIDLE) fail !\n");
return FALSE;
}
if(setsockopt(sockfd,SOL_TCP,TCP_KEEPCNT,(void *)&keepCount,sizeof(keepCount)) == -1)
{
printf("Socket Option setting(TCP_KEEPCNT) fail !\n");
return FALSE;
}