点击(此处)折叠或打开
int SocketConnected(int sock){ if (sock <= 0) return 0; struct tcp_info info; int len = sizeof(info); getsockopt(sock, IPPROTO_TCP, TCP_INFO, &info, (socklen_t *) & len); if ((info.tcpi_state == TCP_ESTABLISHED)) { //myprintf("socket connected\n"); return 1; } else { //myprintf("socket disconnected\n"); return 0; }} tcp_info和TCP_ESTABLISHED在linux/tcp.h 包含点击(此处)折叠或打开
#include <linux/types.h>#include <asm/byteorder.h>#include <linux/config.h>#include <linux/skbuff.h>#include <linux/ip.h>#include <net/sock.h>http: //www.cse.scu.edu/~dclark/am_256_graph_theory/linux_2_6_stack/globals.html#index_tint SocketConnected(int sock){ if (sock <= 0) return 0; struct tcp_info info; int len = sizeof(info); getsockopt(sock, IPPROTO_TCP, TCP_INFO, &info, (socklen_t *) & len); if ((info.tcpi_state == TCP_ESTABLISHED)) {//myprintf("socket connected\n"); return 1; } else {//myprintf("socket disconnected\n"); return 0; }} 目前主要有三种方法来实现用户掉线检测:SO_KEEPALIVE ,SIO_KEEPALIVE_VALS 和Heart-Beat线程。 下面我就上面的三种方法来做一下介绍。 (1)SO_KEEPALIVE 机制 这是socket库提供的功能,设置接口是setsockopt API: BOOL bSet=TRUE; setsockopt(hSocket,SOL_SOCKET,SO_KEEPALIVE,(const char*)&bSet,sizeof(BOOL)); 根据MSDN的文档,如果为socket设置了KEEPALIVE选项,TCP/IP栈在检测到对方掉线后, 任何在该socket上进行的调用(发送/接受调用)就会立刻返回,错误号是WSAENETRESET ; 同时,此后的任何在该socket句柄的调用会立刻失败,并返回WSAENOTCONN错误。 该机制的缺点也很明显: 默认设置是空闲2小时才发送一个“保持存活探测分节”,不能保证实时检测! 当然也可以修改时间间隔参数,但是会影响到所有打开此选项的套接口! 关联了完成端口的socket可能会忽略掉该套接字选项。 (2)SIO_KEEPALIVE_VALS 机制 设置接口是WSAIoctl API:点击(此处)折叠或打开
DWORD dwError = 0L ;tcp_keepalive sKA_Settings = {0}, sReturned = {0} ;sKA_Settings.onoff = 1 ;sKA_Settings.keepalivetime = 5500 ; // Keep Alive in 5.5 sec.sKA_Settings.keepaliveinterval = 3000 ; // Resend if No-Replyif (WSAIoctl(skNewConnection, SIO_KEEPALIVE_VALS, &sKA_Settings, sizeof(sKA_Settings), &sReturned, sizeof(sReturned), &dwBytes, NULL, NULL) != 0){ dwError = WSAGetLastError() ;} 实现时需要添加tcp_keepalive and SIO_KEEPALIVE_VALS的定义文件MSTCPiP.h 该选项不同于SO_KEEPALIVE 机制的就是它是针对单个连接的,对系统其他的套接 口并不影响。 针对完成端口的socket,设置了SIO_KEEPALIVE_VALS后,激活包由TCP STACK来负责。 当网络连接断开后,TCP STACK并不主动告诉上层的应用程序,但是当下一次RECV或者SEND操作 进行后,马上就会返回错误告诉上层这个连接已经断开了.如果检测到断开的时候,在这个连接 上有正在PENDING的IO操作,则马上会失败返回. 该机制的缺点: 不通用啦。MS的API只能用于Windows拉。不过,优雅一些^_^. (3)Heart-Beat线程 没说的。自己写一个后台线程,实现Heart-Beat包,客户端受到该包后,立刻返回相应的反馈 包。 该方法的好处是通用,但缺点就是会改变现有的通讯协议!点击(此处)折叠或打开
/* Net check Make sure you have not used OUT OF BAND DATA AND YOU CAN use OOB */int netcheck(int fd){ int buf_size = 1024; char buf[buf_size]; //clear OOB DATA recv(fd, buf, buf_size); if (send(fd, (void *)"\0", 1, MSG_OOB) < 0) { fprintf(stderr, "Connection[%d] send OOB failed, %s", fd, strerror(errno)); return -1; } return 0;}//复制代码 /* Setting SO_TCP KEEPALIVE */ //int keep_alive = 1;//设定KeepAlive //int keep_idle = 1;//开始首次KeepAlive探测前的TCP空闭时间 //int keep_interval = 1;//两次KeepAlive探测间的时间间隔 //int keep_count = 3;//判定断开前的KeepAlive探测次数void set_keepalive(int fd, int keep_alive, int keep_idle, int keep_interval, int keep_count){ int opt = 1; if (keep_alive) { if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&keep_alive, sizeof(keep_alive)) == -1) { fprintf(stderr, "setsockopt SOL_SOCKET::SO_KEEPALIVE failed, %s\n", strerror(errno)); } if (setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, (void *)&keep_idle, sizeof(keep_idle)) == -1) { fprintf(stderr, "setsockopt SOL_TCP::TCP_KEEPIDLE failed, %s\n", strerror(errno)); } if (setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, (void *)&keep_interval, sizeof(keep_interval)) == -1) { fprintf(stderr, "setsockopt SOL_tcp::TCP_KEEPINTVL failed, %s\n", strerror(errno)); } if (setsockopt(fd, SOL_TCP, TCP_KEEPCNT, (void *)&keep_count, sizeof(keep_count)) == -1) { fprintf(stderr, "setsockopt SOL_TCP::TCP_KEEPCNT failed, %s\n", strerror(errno)); } }}