完全实现linux下SOCKET连接断开判断
2014-01-14 08:20
555 查看
原文在这里:http://bbs.chinaunix.net/thread-930153-1-1.html
link: http://libsbase.googlecode.com/svn/trunk/0.0.2/netcheck-0.0.3.tar.gz
/* 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)
4000
{
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));
}
}
}
link: http://libsbase.googlecode.com/svn/trunk/0.0.2/netcheck-0.0.3.tar.gz
/* 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)
4000
{
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));
}
}
}
相关文章推荐
- 完全实现linux下SOCKET连接断开判断
- soket编程相关(二)如何判断连接状态断开,Linux:C/Socket多路复用select(),阻塞
- 面试中的soket编程相关(一)(如何判断连接状态断开,Linux:C/Socket多路复用select(),阻塞
- 如何判断连接状态断开,Linux:C/Socket多路复用select(),阻塞
- (笔记)Linux服务器中判断客户端socket断开连接的方法
- 判断Socketl连接是否断开
- java中判断socket网络远端是否断开连接
- linux socket断开网络连接程序终止
- linux socket的多线程编成来实现多个客户端的连接
- linux下socket编程实现一个服务器连接多个客户端
- C# socket服务端判断 客户端已经断开连接的一个小办法
- java中判断socket网络远端是否断开连接
- socket.sendUrgentData判断网络远端是否断开连接
- 判断Socket断开连接
- Linux下Socket连接超时的一种实现方法(转载)
- socket 如何判断远端服务器的连接状态?连接断开,需重连
- AsyncSocket写的客户端Socket,实现当服务器断开连接时弹窗提示
- 安卓 java有效判断Socket连接断开和安卓TCP测试APP(客户+服务端一体)
- Linux检测TCP连接断开的一种简单实现方法http://blog.csdn.net/wtewrw/article/details/6547380
- Linux下Socket连接超时的一种实现方法