我现在是客户端在循环从服务端读数据,如何检测到服务端断开,自动关闭该socket

解决方案 »

  1.   

    用Select "FD_CLOSE"事件,服务器关闭时会触发这个事件
      

  2.   

    如果server正常关闭,客户端会收到fd_close事件
    若非正常关闭,只能靠其它方式,比如发送不成功,根据返回的状态值判断
      

  3.   

    recv 返回值==0,则服务器关闭了
      

  4.   

    一般是心跳检测如果是TCP连接,可以看这个函数
    GetTcpTable
    The GetTcpTable function retrieves the TCP connection table.DWORD GetTcpTable (
    PMIB_TCPTABLE pTcpTable // buffer for the connection table
    PDWORD pdwSize // size of the buffer
    BOOL bOrder // sort the table?);
    Parameters
    pTcpTable 
    Pointer to a buffer that, on successful return, contains the TCP connection table as a MIB_TCPTABLE structure. 
    pdwSize 
    Specifies the size of the buffer pointed to by the pTcpTable parameter. If the buffer is not large enough to hold the returned connection table, the function sets this parameter equal to the required buffer size. 
    bOrder 
    Specifies whether the connection table should be sorted. If this parameter is TRUE, the table is sorted in order of: 
    Local IP address Local port Remote IP address Remote port Return Values
    If the function succeeds, the return value is NO_ERROR.If the function fails, use FormatMessage to obtain the message string for the returned error.
      

  5.   

    相应close事件,或定时发包测试
      

  6.   

    读之前,select一下,心跳虽好,比较麻烦,socket意外断开一般不会返回什么错误代码