如题

解决方案 »

  1.   

    据我所知要判断对方断线有两个方面,一是ONCLOSE(FD_CLOSE),还有就是自己还得隔一段时间去PING他。
      

  2.   

    CAsyncSocket::m_hSocket!=INVALID_SOCKET
      

  3.   

    CAsyncSocket::AsyncSelectSee Also
    CAsyncSocket Overview | Class Members | Hierarchy Chart | CAsyncSocket::GetLastError | WSAAsyncSelect
    Call this member function to request event notification for a socket.BOOL AsyncSelect(
       long lEvent = FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE 
    );
    Parameters
    lEvent 
    A bitmask which specifies a combination of network events in which the application is interested. 
    FD_READ   Want to receive notification of readiness for reading. 
    FD_WRITE   Want to receive notification when data is available to be read. 
    FD_OOB   Want to receive notification of the arrival of out-of-band data. 
    FD_ACCEPT   Want to receive notification of incoming connections. 
    FD_CONNECT   Want to receive notification of connection results. 
    FD_CLOSE   Want to receive notification when a socket has been closed by a peer. 
    Return Value
    Nonzero if the function is successful; otherwise 0, and a specific error code can be retrieved by calling GetLastError. The following errors apply to this member function: WSANOTINITIALISED   A successful AfxSocketInit must occur before using this API. 
    WSAENETDOWN   The Windows Sockets implementation detected that the network subsystem failed. 
    WSAEINVAL   Indicates that one of the specified parameters was invalid. 
    WSAEINPROGRESS   A blocking Windows Sockets operation is in progress. 
    Res
    This function is used to specify which MFC callback notification functions will be called for the socket. AsyncSelect automatically sets this socket to nonblocking mode. For more information, see the article Windows Sockets: Socket Notifications and Overview of Windows Sockets 2 and Windows Sockets Programming Considerations in the Platform SDK.
      

  4.   

    判断对方断线可设置SOCKET的“保持活动”
    BOOL fSetVal = TRUE;setsockopt(socket,SO_KEEPALIVE,&bSetVal,sizeof(BOOL),SOL_SOCKET);如果“保持活动”造成了连接的中断,便会向SOCKET正在进行的任何一个调用返回一个WSAENETERESET的错误代码。与“保持活动”设置相应的注册标键是KeepAliveInterval(保持活动间隔)和KeepAliveTime(保持活动时间)WIN95&WIN98
    HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\VxD\MSTCPWINNT &WIN2000
    HKEY_LOCAL_MACHINE\System\CurrentContrlSet\Services\TCPIP\Parameters"保持活动"间隔要在不短于2小时时间间隔进行,这个时间是由以上注册标键值决定的,但他会影响所有TCP的“保持活动”行为,而WIN2K增加了一个I/O控制命令SIO_KEEPALIVE_VALS,可由他指定每一个SOCKET;SOCK_DGRAM不支持这一选项;
      

  5.   

    这么多,我一个一个试一下。
    还有我的程序是在WinCE上的我只是想让他在Connect时返回