CAsyncSocket m_ClientSock;  
         m_ClientSock.Create();
res = m_ClientSock.Connect("127.0.0.1", 1001);
if (res == 0)
{
str.Format("%d", GetLastError());
AfxMessageBox("连接失败"+ str);
return;
}连接失败的错误代码是10035,我不知道哪里出错了?

解决方案 »

  1.   

    socket的建立需要一些时间,所以返回10035错误是正常的,当发生10035错误的时候,过一会重试就可以了,可以用下面的方法:
    CSocket m_ClientSock;
    m_ClientSock.Create();
    int res = m_ClientSock.Connect("127.0.0.1", 1001);
    while(res == 10035)
    {
    res = m_ClientSock.Connect("127.0.0.1", 1001);
    }
      

  2.   

    异步套接字连接和接收发送操作都是异步的,会立即返回。
    你这个是正常的。10035表示无法立即完成一个非阻止性套接字操作。
    用同步套接字类CSocket不会出现这个问题。
      

  3.   

    CAsyncSocket  m_ClientSock;
    m_ClientSock.Create();
    int res = m_ClientSock.Connect("127.0.0.1", 1001);
    while(res == 0 && GetLastError() == 10035)
    {
    res = m_ClientSock.Connect("127.0.0.1", 1001);
    }
      

  4.   

    CAsyncSocket  m_ClientSock;
    m_ClientSock.Create();
    int res = m_ClientSock.Connect("127.0.0.1", 1001);
    while(res == 0 && GetLastError() == 10035)
    {
             sleep(1000);  //这样会不会好些?
    res = m_ClientSock.Connect("127.0.0.1", 1001);
    }
      

  5.   

    很奇怪,res = 0,却还可以通信,这是为什么?
      

  6.   

    你用了异步,是非阻塞的,连接完成与否windows会有消息通知你,等消息就可以了
      

  7.   

    MSDN Library CD 上的文档
    mk:@MSITStore:F:\MSDN\MSDN2000\MSDN\WinSock.chm::/devdoc/good/pdnds/sock2/errors_0oaa.htm
    WSAEWOULDBLOCK 
    (10035) 
    Resource temporarily unavailable. 
    This error is returned from operations on non-blocking sockets that cannot be completed immediately, for example recv when no data is queued to be read from the socket. It is a non-fatal error, and the operation should be retried later. It is normal for WSAEWOULDBLOCK to be reported as the result from calling connect on a non-blocking SOCK_STREAM socket, since some time must elapse for the connection to be established. mk:@MSITStore:F:\MSDN\MSDN2000\MSDN\mswnsk98.chm::/html/vbevterrorevent.htmsckInvalidArgument 10014 所请求的地址是广播地址,但未设置标记。 
    sckWouldBlock 10035 套接字不成块,而指定操作将使之成块。 
    sckInProgress 10036 制造块的 Winsock 操作在进行之中。