Connect()是不是在服务器端调用Accept()后才会返回TRUE?
小弟初学CSocket,服务器端Accept()好像调用成功了,可是客户端Connect()没有返回TRUE,很迷茫.....PS:这个问题应该很简单吧,5分算了...

解决方案 »

  1.   

    connect() 返回值并非布尔型,当返回-1时,即SOCKET_ERROR,为连接失败。
    具体查下msdn吧,记不清了。
      

  2.   

    分不是重要的。
    connect(...)
    Return Values
    If no error occurs, connect returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError.On a blocking socket, the return value indicates success or failure of the connection attempt.With a nonblocking socket, the connection attempt cannot be completed immediately. In this case, connect will return SOCKET_ERROR, and WSAGetLastError will return WSAEWOULDBLOCK.如果阻塞模式,connect返回0表明成功,返回非0则connect失败;
    如果非阻塞模式,connect是异步的,当前的返回值SOCKET_ERROR并不代表connect的结果,你需要在异步事件里面判断connect是否成功,或者用select来判断连接是否成功。
      

  3.   

    MSDN上是这样写的
    Return Value
    Nonzero if the function is successful; otherwise 0, and a specific error code can be retrieved by calling GetLastError. If this indicates an error code of WSAEWOULDBLOCK, and your application is using the overridable callbacks, your application will receive an OnConnect message when the connect operation is complete. 
    为什么不一样...
    那服务器端的Accept()是用来干什么的 ?
      

  4.   

    MSDN 2001/MSDN 2003.
    accept server side: accept connect request from client sides.
      

  5.   

    具体你可以看MSDN啊,上面也说了,客户端分为阻塞、非阻塞2种模式得到connect的结果。
    也就是服务端accept以后,你的客户端会在阻塞模式下connect返回0,非阻塞模式下客户端可能会收到异步的链接成功时间或者select得到结果。
      

  6.   

    accept后,accept函数将自动创建一个新的socket用来与client(g_hSockTK)连接会话,如创建失败,会返回SOCKET_ERROR,成功为0。client端的connect结果同上
    而原socket继续保持侦听。// 该函数堵塞等待客户的连接
    g_hSockTK = accept(g_hSocket, NULL, NULL);
    if (g_hSockTK == SOCKET_ERROR)
    {
    TRACE("Accept Error: %d\n",(error = WSAGetLastError()));
    return 1;
    }