解决方案 »
- jmail 读取126邮件乱码问题,急急急急急!!!
- 基于snmp++的异步编程怎么写?能给个例子吗?
- vc中窗口的问题(界面)
- 如何使用GetRegionData()函数,获取区域边界上各点的数据CPoint,如何将LPRGNDATA数据转换为CPoint?
- 高分求教!!如何在Radio Button的BN_CLICKED事件中调用对话框
- 请问如何查找一个NT服务
- 怎么学VC有点“狗咬刺猬”的味道,谁能帮我?指导一下。谢谢
- 100分求教:API小问题,怎样在窗口的不同位子每隔2秒显示一幅位图,最好给个例子。送上150分!
- 被刘建宏左右的中央电视台很让人失望!!!!!!
- 求教:如何串行化一个链表(链表里面存储的是对象的指针)?
- 高手帮忙看看编译H264Enc.dll问题
- Richedit中插入Bitmap,如何释放内存
http://download.csdn.net/detail/geoff08zhang/4571358
the timeout. */
bool connect(char *host,int port, int timeout)
{
TIMEVAL Timeout;
Timeout.tv_sec = timeout;
Timeout.tv_usec = 0;
struct sockaddr_in address; /* the libc network address data structure */
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
address.sin_addr.s_addr = inet_addr(host); /* assign the address */
address.sin_port = htons(port); /* translate int2port num */
address.sin_family = AF_INET;
//set the socket in non-blocking
unsigned long iMode = 1;
int iResult = ioctlsocket(sock, FIONBIO, &iMode);
if (iResult != NO_ERROR)
{
printf("ioctlsocket failed with error: %ld\n", iResult);
}
if(connect(sock,(struct sockaddr *)&address,sizeof(address))==false)
{
return false;
}
// restart the socket mode
iMode = 0;
iResult = ioctlsocket(sock, FIONBIO, &iMode);
if (iResult != NO_ERROR)
{
printf("ioctlsocket failed with error: %ld\n", iResult);
}
fd_set Write, Err;
FD_ZERO(&Write);
FD_ZERO(&Err);
FD_SET(sock, &Write);
FD_SET(sock, &Err);
// check if the socket is ready
select(0,NULL,&Write,&Err,&Timeout);
if(FD_ISSET(sock, &Write))
{
return true;
}
return false;
}斗胆问一句: 姑娘年方几何? 有无情郎?
http://www.codeproject.com/Tips/168704/How-to-set-a-socket-connection-timeout
1、Add a keepalive message to the application protocol framing (an empty message).
2、Add a keepalive message to the actual application protocol (a "null" message).
3、Explicit timer assuming the worst.
4、Manipulate the TCP/IP keepalive packet settings.
翻译如下:
1. 间隔一段时间,发送TCP KeepAlive数据包,无任何数据,判断返回值。客户端程序无需改动
2. 间隔一段时间,发送TCP KeepAlive数据包,加一个null标志位,客户端程序需要改动,如果客户端还是alive的话,数据直接丢弃,然后返回一个应答给服务器端.
3. 史上最愚蠢的方法,在规定的间隔内,没有收到应答数据包就认为连接不可用,容易误报,但值得一试。
4. 一个折衷的方案,手动设置TCP KeepAlive包设置,对windows有版本要求。
原文链接:http://www.codeproject.com/Articles/37490/Detection-of-Half-Open-Dropped-TCP-IP-Socket-Conne
此外,按常理,你的界面应该也是在1~2秒之内进行一次局部刷新,而不是等所有连接判断完后再全部刷新。如果你采用的是后面这种方法,延时大也是有可能的哦~~~
装置侧的通信规约你有么?
你就定期的问一个发一个数据然后看echo
如果没有返回数据说明这台装置离线了
我们做电力监控的时候基本都用这种方法判断
{
sendHeartPkg();//收到信号包的下位机应答该心跳包,(该心跳包最好广播发送)
sleep(1000);
}
检查是否掉线的线程:while(1)
{
checkOffline();//检查是否有应答(如3次检查都没有应答,则表示设备掉线,前提设备如果没有掉线,3秒内必定有应答)
sleep(1000);
}这种方式3秒内就可以检测出设备是否掉线,看你的需要来改变检测时间
当然了你也可以用定时器实现,检测和发送心跳包可以放同一个线程