我的CAsyncSocket应用中,客户端使用Connect()函数已经连接成功,但返回值却为0,不是非零值,为什么?

解决方案 »

  1.   

    我想可能0表示连接成功,非0表示有错误吧
    MSDN上是不是有点前后矛盾?其实这个你也大可不必理会的。
      

  2.   

    Connect()函数返回-1才表示连接失败。
      

  3.   

    Connect()函数的返回值在异步通讯里没有什么价值(个人观点)你调用了Connect()函数后,系统会自动触发消息FD_CONNECTCAsyncSocket::OnConnect( int nErrorCode );virtual void OnConnect( int nErrorCode );
    nErrorCodeThe most recent error on a socket. The following error codes apply to the OnConnect member function: 0              The function executed successfully.
    WSAEADDRINUSE  The specified address is already in use如果nErrorCode == 0 就说明连接上了,可以receive, send
      

  4.   

    调用WSAGetLastError()看看
    异步调用一般会返回WSAWOULDBLOCK.
    表示不能立即完成一个异步操作。
      

  5.   

    GetLastError()返回值为0,好像用成员变量m_bConnected可以判断的。
    是否因为是异步操作,Connect()发消息后即返回,不管是否连接成功。有些奇怪的是,Connect()返回非零为连接成功;而OnConnect()的错误码为0表示连接成功。
      

  6.   

    For connections, you simply call Connect, which will return when the connection is completed (either successfully or in error). How connection notifications are handled is an MFC implementation detail.