1.简介
在实际项目开发中,TCP连接关闭主要有两种情况:
- 1、连接正常关闭,调用close() 、shutdown()连接优雅关闭,send与recv立马返回错误;
- 2、连接的对端异常关闭,比如拔掉网线网络断掉,突然断电。
面对上述问题,通常都要检测对端是否处于连接中,主要有两种方法:
- 1、编写心跳包程序,简单的说就是自己的程序加入一条线程,定时向对端发送数据包,查看是否有ACK,根据ACK的返回情况来管理连接。此方法比较通用,一般使用业务层心跳处理,灵活可控,但改变了现有的协议;
- 2、使用TCP的keepalive机制,UNIX网络编程不推荐使用SO_KEEPALIVE来做心跳检测。
2.keepalive机制
对于一个已经建立的tcp连接,如果在keepalive_time时间内双方没有任何的数据包传输,则开启keepalive功能的一端将发送 keepalive数据包。若没有收到应答,则每隔keepalive_intvl时间再发送该数据包,发送keepalive_probes次。一直没有 收到应答,则发送rst包关闭连接。若收到应答,则将计时器清零。
int keepalive = 1;
int keepidle = 60;
int keepinterval = 5;
int keepcount = 3;
setsockopt(rs, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepalive , sizeof(keepalive ));
setsockopt(rs, SOL_TCP, TCP_KEEPIDLE, (void*)&keepidle , sizeof(keepidle ));
setsockopt(rs, SOL_TCP, TCP_KEEPINTVL, (void *)&keepinterval , sizeof(keepinterval ));
setsockopt(rs, SOL_TCP, TCP_KEEPCNT, (void *)&keepcount , sizeof(keepcount ));
3.抓包分析
说明:该抓包对应第四部分的代码
4.部分代码
static int NetWorkServerInit(void)
{
char *SERVERIP = DEFAULT_CONFIG_SERVER_IP;
ListenSockFd = socket(AF_INET, SOCK_STREAM, 0);
if(ListenSockFd < 0)
{
cks_err("Net work create socket fail!");
return NET_ERR;
}
int opt = 1;
int idel = 5;
int interl = 10;
int cnt = 3;
setsockopt(ListenSockFd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
setsockopt(ListenSockFd, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt));
setsockopt(ListenSockFd, SOL_TCP, TCP_KEEPIDLE, &idel, sizeof(idel));
setsockopt(ListenSockFd, SOL_TCP, TCP_KEEPINTVL, &interl, sizeof(interl));
setsockopt(ListenSockFd, SOL_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt));
struct sockaddr_in local;
local.sin_port = htons((int)DEFAULT_CONFIG_LISTEN_PORT);
local.sin_family = AF_INET;
local.sin_addr.s_addr = inet_addr(SERVERIP);
if(bind(ListenSockFd, (struct sockaddr *)&local, sizeof(local)) < 0)
{
cks_err("Net work bind fail!");
return NET_ERR;
}
if(listen(ListenSockFd, 5) < 0)
{
cks_err("Net work listen fail!");
return NET_ERR;
}
return NET_OK;
}
5.加入讨论
|