////the info of the server socket
    g_sockServerAddr[i].sin_family = AF_INET;
    g_sockServerAddr[i].sin_port=htons(nport);
    g_sockServerAddr[i].sin_addr.s_addr=inet_addr(szServerIp);then connect

解决方案 »

  1.   

    to  Kevin_qing() ,youyi(youyiY) :我少copy了一句设置端口号,重新贴代码:g_nServerPort[0]=sFirstPort;
    g_nServerPort[1]=sSecondPort;int i=0;
    for(i=0;i<2;i++)////prepare to setup the 2 TCP links
    {
        g_ClientSocket[i] = socket(AF_INET, SOCK_STREAM, 0);
        if(g_ClientSocket[i]==INVALID_SOCKET)
        {
    MessageBox( NULL,"create client socket error", NULL,MB_OK);
    return FALSE;
        }
        ////the info of the server socket
        g_sockServerAddr[i].sin_family = AF_INET;
        ulIP = inet_addr(sServerIP);
        if ( INADDR_NONE != ulIP )
    g_sockServerAddr[i].sin_addr.S_un.S_addr = ulIP;
        else 
        {
    MessageBox( NULL, "server IP error", NULL, MB_OK);
    return FALSE;
         }      ////端口号
         g_sockServerAddr[i].sin_port = htons(g_nServerPort[i]);     int nErrorCode = WSAAsyncSelect(g_ClientSocket[i],hWnd,WM_RCV1+i*10, FD_READ);
         if (nErrorCode == SOCKET_ERROR) 
         {
    MessageBox( NULL, "asyncselect error", NULL, MB_OK);
    return FALSE;
         }
    }int nRet;
    nRet=connect(g_ClientSocket[0],(LPSOCKADDR)&g_sockServerAddr[0],sizeof(g_sockServerAddr[0]));
    if (nResponse!=0)
    {
           nErrorCode=WSAGetLastError();
           str.Format("错误号%d",nErrorCode);
           MessageBox( NULL,"connect to the server error! "+str, NULL, MB_OK);
          return -1;
    }
    bConnected[0] = TRUE;
    服务器端能够acccept,connect()失败后取到的错误码是10035:引用指针错误。
      

  2.   

    nRet=connect(g_ClientSocket[0],(struct sockaddr *)&g_sockServerAddr[0],sizeof(g_sockServerAddr[0]));
      

  3.   

    你用的是AsyncSelect IO 模型 所以
    connect是异步的,你可以用WSAGetLastError看看
    如果是WSAEWOULDBLOCK 并不表示已经失败了,具体应该在事件消息中判断是否连接成功!
      

  4.   

    MSDN上这样写到If the application is using WSAAsyncSelect to indicate interest in connection events, then the application will receive an FD_CONNECT notification indicating that the connect operation is complete (successfully or not).
      

  5.   

    WSAEWOULDBLOCK 
    (10035) = WSAEWOULDBLOCK
    Resource temporarily unavailable. 
    This error is returned from operations on nonblocking sockets that cannot be completed immediately, for example recv when no data is queued to be read from the socket. It is a nonfatal error, and the operation should be retried later. It is normal for WSAEWOULDBLOCK to be reported as the result from calling connect on a nonblocking SOCK_STREAM socket, since some time must elapse for the connection to be established. 
      

  6.   

    在程序的最开始的地方加入:
    WORD wVersionRequested;
    WSADATA wsaData;
    wVersionRequested = MAKEWORD( 2, 2 ); 
    WSAStartup( wVersionRequested, &wsaData );我说对了就给分!
      

  7.   

    to qiufuwang(呆呆瓜) :这一步我已经做过了to ychener(贫血) , florist2000(善良的石头) :你们的意见我感觉很有道理,我马上试一下。谢谢大家
      

  8.   

    调用WSAAsyncSelect后,socket就自动改成异步的了,前面有两位朋友已经指出问题所在。
    异步的socket要靠消息驱动,connect调用后返回的99%都是马上返回WSAEWOULDBLOCK,
    当系统连接成功后,系统会向你的监听消息的窗口发送连接成功的FD_CONNECT消息, 
    当有数据来到时,会收到FD_READ消息,你要在消息处理函数里读出数据当可以发送数据时(连接成功后或所有要发送的数据已经发送完后),收到FD_WRITE消息,你要发送的数据此时可以发送。当对方关闭连接时,收到FD_CLOSE消息,此时你也应该关闭连接,释放相关资源。
      

  9.   

    我的程序片段:
    {
       WORD wVersionRequested;
       WSADATA wsaData;
       wVersionRequested = MAKEWORD( 2, 2 ); 
       WSAStartup( wVersionRequested, &wsaData );
      .................  WSAAsyncSelect(pClientSock->m_hSocket,GetSafeHwnd),WM_CLIENTSOCKEVENT,FD_CONNECT|FD_READ|FD_CLOSE);
      if(SOCKET_ERROR==pClientSock->Connect(m_ip,SERVER_PORT))
      {
         return false; 
      }
    }afx_msg LONG CMySocket::OnClientSockEvent(UINT wParam, LONG lParam)
    {
    CAsyncSocket *psRecv;
    int          i=0;
    int          hlP=0;
    switch(LOWORD(lParam))
    {
    case FD_CONNECT:
    hlP=HIWORD(lParam);
    if(hlP>0)
    {
    switch(hlP)
    {
    case WSAEAFNOSUPPORT:
    AfxMessageBox("地址不对");
    break;   
    case WSAECONNREFUSED:
    AfxMessageBox("连接被拒绝");
    break;
    case WSAETIMEDOUT:
    AfxMessageBox("连接超时");
    break;
    case WSAENETUNREACH:
    AfxMessageBox("地址不对");
    break;
    case WSAEINVAL:
    case WSAEISCONN:
    AfxMessageBox("该套接字正被使用");
    break;
    default:
    AfxMessageBox("发生错误,未能连接上");
    break;
    }
    psRecv = CAsyncSocket::FromHandle((SOCKET)wParam);
    if(psRecv==NULL) break; }
    psRecv->Close();
    delete psRecv;
    psRecv=NULL;
    break;
    }
    psRecv = CAsyncSocket::FromHandle((SOCKET)wParam);
    if(psRecv==NULL) break;
    psRecv->Send(m_ClientSendBuff,SOCK_BUFFSIZE);
    break;
    case FD_READ:
                 .......
         }
    }
      

  10.   

    to  taolei(实在无聊) :我必须等到收到FD_WRITE消息后才能发送数据吗?to qiufuwang(呆呆瓜) :谢谢你贴出了代码,我回记着给你的分的。
      

  11.   

    一定要等到FD_WRITE消息收到后才能发送数据,否则一定会失败。
    但可不用一定在FD_WRITE消息处理函数里发送数据,收到FD_WRITE之后可以做个标记,如:BOOL canwrite=TRUE;然后在其他地方当canwrite==TRUE时发送数据,当你发送数据失败,并且返回WSAEWOULDBLOCK后,canwrite=FALSE;等下次FD_WRITE到来之后,才能再发送。我的方法:
    自己准备一个buffer,要发送的数据先放到buffer里,如果canwrite==TRUE,就立即发送,直到buffer里的数据都成功发送;如果没发送完时返回WSAEWOULDBLOCK,就canwrite=FALSE;返回先。
    当FD_WRITE到来时,canwrite=TRUE,然后再重复上面发送数据的过程。注意:如果发送数据都成功,没有返回WSAEWOULDBLOCK,那么,系统将不会再发送FD_WRITE消息。