当前位置: 技术问答>linux和unix
TCP断线后,如何检测?
来源: 互联网 发布时间:2015-09-22
本文导语: 运行环境:linux 语言:c++ TCP连接断线后, 1.正常断线,如何检测? 2.意外掉线,如何检测? 我不希望通过发送数据来检测TCP连接! 谢谢! | 怎样及时检测出非正常断开...
运行环境:linux
语言:c++
TCP连接断线后,
1.正常断线,如何检测?
2.意外掉线,如何检测?
我不希望通过发送数据来检测TCP连接!
谢谢!
语言:c++
TCP连接断线后,
1.正常断线,如何检测?
2.意外掉线,如何检测?
我不希望通过发送数据来检测TCP连接!
谢谢!
|
怎样及时检测出非正常断开的TCP连接
此处的”非正常断开”指TCP连接不是以优雅的方式断开,如网线故障等物理链路的原因,还有突然主机断电等原因
有两种方法可以检测:1.TCP连接双方定时发握手消息 2.利用TCP协议栈中的KeepAlive探测
第二种方法简单可靠,只需对TCP连接两个Socket设定KeepAlive探测,所以本文只讲第二种方法在Linux,Window2000下的实现(在其它的平台上没有作进一步的测试)
Windows 2000平台下
//定义结构及宏
struct TCP_KEEPALIVE {
u_long onoff;
u_long keepalivetime;
u_long keepaliveinterval;
} ;
#define SIO_KEEPALIVE_VALS _WSAIOW(IOC_VENDOR,4)
//KeepAlive实现
TCP_KEEPALIVE inKeepAlive = {0}; //输入参数
unsigned long ulInLen = sizeof(TCP_KEEPALIVE);
TCP_KEEPALIVE outKeepAlive = {0}; //输出参数
unsigned long ulOutLen = sizeof(TCP_KEEPALIVE);
unsigned long ulBytesReturn = 0;
//设置socket的keep alive为5秒,并且发送次数为3次
inKeepAlive.onoff = 1;
inKeepAlive.keepaliveinterval = 5000; //两次KeepAlive探测间的时间间隔
inKeepAlive.keepalivetime = 5000; //开始首次KeepAlive探测前的TCP空闭时间
if (WSAIoctl((unsigned int)s, SIO_KEEPALIVE_VALS,
(LPVOID)&inKeepAlive, ulInLen,
(LPVOID)&outKeepAlive, ulOutLen,
&ulBytesReturn, NULL, NULL) == SOCKET_ERROR)
{
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("(%P|%t) WSAIoctl failed. error code(%d)!n"),WSAGetLastError()));
}
Linux平台下
#include
……
////KeepAlive实现
//下面代码要求有ACE,如果没有包含ACE,则请把用到的ACE函数改成linux相应的接口
int keepAlive = 1;//设定KeepAlive
int keepIdle = 5;//开始首次KeepAlive探测前的TCP空闭时间
int keepInterval = 5;//两次KeepAlive探测间的时间间隔
int keepCount = 3;//判定断开前的KeepAlive探测次数
if(setsockopt(s,SOL_SOCKET,SO_KEEPALIVE,(void*)&keepAlive,sizeof(keepAlive)) == -1)
{
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("(%P|%t) setsockopt SO_KEEPALIVE error!n")));
}
if(setsockopt(s,SOL_TCP,TCP_KEEPIDLE,(void *)&keepIdle,sizeof(keepIdle)) == -1)
{
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("(%P|%t) setsockopt TCP_KEEPIDLE error!n")));
}
if(setsockopt(s,SOL_TCP,TCP_KEEPINTVL,(void *)&keepInterval,sizeof(keepInterval)) == -1)
{
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("(%P|%t) setsockopt TCP_KEEPINTVL error!n")));
}
if(setsockopt(s,SOL_TCP,TCP_KEEPCNT,(void *)&keepCount,sizeof(keepCount)) == -1)
{
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("(%P|%t) setsockopt TCP_KEEPCNT error!n")));
}
测试中表明,当Linux服务器设定了KeepAlive,Windows客户端可以不设KeepAlive
引用请注明作者:在水一鱼(powercc@21cn.com)
此处的”非正常断开”指TCP连接不是以优雅的方式断开,如网线故障等物理链路的原因,还有突然主机断电等原因
有两种方法可以检测:1.TCP连接双方定时发握手消息 2.利用TCP协议栈中的KeepAlive探测
第二种方法简单可靠,只需对TCP连接两个Socket设定KeepAlive探测,所以本文只讲第二种方法在Linux,Window2000下的实现(在其它的平台上没有作进一步的测试)
Windows 2000平台下
//定义结构及宏
struct TCP_KEEPALIVE {
u_long onoff;
u_long keepalivetime;
u_long keepaliveinterval;
} ;
#define SIO_KEEPALIVE_VALS _WSAIOW(IOC_VENDOR,4)
//KeepAlive实现
TCP_KEEPALIVE inKeepAlive = {0}; //输入参数
unsigned long ulInLen = sizeof(TCP_KEEPALIVE);
TCP_KEEPALIVE outKeepAlive = {0}; //输出参数
unsigned long ulOutLen = sizeof(TCP_KEEPALIVE);
unsigned long ulBytesReturn = 0;
//设置socket的keep alive为5秒,并且发送次数为3次
inKeepAlive.onoff = 1;
inKeepAlive.keepaliveinterval = 5000; //两次KeepAlive探测间的时间间隔
inKeepAlive.keepalivetime = 5000; //开始首次KeepAlive探测前的TCP空闭时间
if (WSAIoctl((unsigned int)s, SIO_KEEPALIVE_VALS,
(LPVOID)&inKeepAlive, ulInLen,
(LPVOID)&outKeepAlive, ulOutLen,
&ulBytesReturn, NULL, NULL) == SOCKET_ERROR)
{
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("(%P|%t) WSAIoctl failed. error code(%d)!n"),WSAGetLastError()));
}
Linux平台下
#include
……
////KeepAlive实现
//下面代码要求有ACE,如果没有包含ACE,则请把用到的ACE函数改成linux相应的接口
int keepAlive = 1;//设定KeepAlive
int keepIdle = 5;//开始首次KeepAlive探测前的TCP空闭时间
int keepInterval = 5;//两次KeepAlive探测间的时间间隔
int keepCount = 3;//判定断开前的KeepAlive探测次数
if(setsockopt(s,SOL_SOCKET,SO_KEEPALIVE,(void*)&keepAlive,sizeof(keepAlive)) == -1)
{
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("(%P|%t) setsockopt SO_KEEPALIVE error!n")));
}
if(setsockopt(s,SOL_TCP,TCP_KEEPIDLE,(void *)&keepIdle,sizeof(keepIdle)) == -1)
{
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("(%P|%t) setsockopt TCP_KEEPIDLE error!n")));
}
if(setsockopt(s,SOL_TCP,TCP_KEEPINTVL,(void *)&keepInterval,sizeof(keepInterval)) == -1)
{
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("(%P|%t) setsockopt TCP_KEEPINTVL error!n")));
}
if(setsockopt(s,SOL_TCP,TCP_KEEPCNT,(void *)&keepCount,sizeof(keepCount)) == -1)
{
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("(%P|%t) setsockopt TCP_KEEPCNT error!n")));
}
测试中表明,当Linux服务器设定了KeepAlive,Windows客户端可以不设KeepAlive
引用请注明作者:在水一鱼(powercc@21cn.com)
|
假设是server端判断client的断开:
1)client 关闭socket, 这是正常的情况,server段recv返回0,server就关闭socket就可以了。
2)网络断开,需要keepalive的在线维持,tcp默认的的keepalive时间为7200秒,时间很长;
也可以在tcp数据流中keepalive,自己在tcp流中定时在线维持包,不过这样要求tcp流可以拆
成包;或者在OOB中发生keepalive包,定时发生1Byte的keepalive包。
3)客户端程序异常退出,系统应该会关闭所有的文件描述符,释放资源。
1)client 关闭socket, 这是正常的情况,server段recv返回0,server就关闭socket就可以了。
2)网络断开,需要keepalive的在线维持,tcp默认的的keepalive时间为7200秒,时间很长;
也可以在tcp数据流中keepalive,自己在tcp流中定时在线维持包,不过这样要求tcp流可以拆
成包;或者在OOB中发生keepalive包,定时发生1Byte的keepalive包。
3)客户端程序异常退出,系统应该会关闭所有的文件描述符,释放资源。
|
通常情况下都是采用heartbeat来持续检测连接的,如果不希望采用这种方式的话,也即在没有任何数据发送和接收的情况下,tcp栈需要大概一个小时的时间才能发现连接已经不存在。
|
如果有 连接不停 接收他的数据 自然就会从接收函数返回 有标志标明是数据 还是连接断开
异常断开 会有超时 系统协议栈会尝试多次握手 在收不到ack情况下 会给上层接收或是监测如select发送信息 告知连接异常断开
异常断开 会有超时 系统协议栈会尝试多次握手 在收不到ack情况下 会给上层接收或是监测如select发送信息 告知连接异常断开