#include "netinet/tcp.h"
int keepAlive = 1; //設(shè)定KeepAlive
int keepIdle = 30; //開始首次KeepAlive探測前的TCP空閉時間
int keepInterval = 30; //兩次KeepAlive探測間的時間間隔
int keepCount = 3; //判定斷開前的KeepAlive探測次數(shù)
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;
}