下面是connect的MSDN文档。由划线部分可知,当socket设置为非阻塞模式时,connect发起后会立即返回,然而,“三次握手”却依然进行,此时,connect返回SOCKET_ERROR,WSAGetLastError()返回WSAEWOULDBLOCK。如果需要判断connect是否成功,可以使用select()函数,当connect成功时,socket文件描述符写就绪;当connect失败时,socket文件描述符异常就绪。
Return Value
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. In this case, there are three possible scenarios:
• Use the select function to determine the completion of the connection request by checking to see if
Return Value
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. In this case, there are three possible scenarios:
• Use the select function to determine the completion of the connection request by checking to see if