解决方案 »

  1.   

    你可以使用 TCP 的 keep alive ,通过 setsockopt 设置间隔值。这有很多socket例子,你也可以看:
    http://download.csdn.net/detail/geoff08zhang/4571358
      

  2.   

    亲爱的菇凉,惦记你的人又来啦~~,以下文章来自CodeProject,是否中意呢?/*Sometimes, the connect time-out can take too much time when the target is unavailable. To resolve this issue, we can use non-blocking socket mode to select
    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;
    }斗胆问一句: 姑娘年方几何? 有无情郎?
      

  3.   

    host是连接的客户端IP地址,port是客户端的端口,timeout是超时设置,如果timeout时间内客户端依旧无法找到,服务端就断开连接。这是我的理解,原文在这里,请移芳步~ 
    http://www.codeproject.com/Tips/168704/How-to-set-a-socket-connection-timeout
      

  4.   

    你用的是select 模式,另外,你还可以用一个线程来判断
      

  5.   

    下面是前人总结的所有方法,你先试试看,哪个好用。
    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秒之内进行一次局部刷新,而不是等所有连接判断完后再全部刷新。如果你采用的是后面这种方法,延时大也是有可能的哦~~~ 
      

  6.   

    你不就是想知道在线的装置数么?
    装置侧的通信规约你有么?
    你就定期的问一个发一个数据然后看echo
    如果没有返回数据说明这台装置离线了
    我们做电力监控的时候基本都用这种方法判断
      

  7.   

    检测是否掉线的线程的实现如下:发送心跳包的线程:(每1秒发送1次)while(1)
    {
        sendHeartPkg();//收到信号包的下位机应答该心跳包,(该心跳包最好广播发送)
        sleep(1000);
    }
    检查是否掉线的线程:while(1)
    {
        checkOffline();//检查是否有应答(如3次检查都没有应答,则表示设备掉线,前提设备如果没有掉线,3秒内必定有应答)
        sleep(1000);
    }这种方式3秒内就可以检测出设备是否掉线,看你的需要来改变检测时间
    当然了你也可以用定时器实现,检测和发送心跳包可以放同一个线程