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