Question

According to MSDN you have to create a non-blocking socket like this:

unsigned nonblocking = 1;
ioctlsocket(s, FIONBIO, &nonblocking);

and use it in the write-fdset for select() after that. To check if the connection was successful, you have to see if the socket is writeable. However, the MSDN-article does not describe how to check for errors.

How can I see if connect() did not succeed, and if that is the case, why it did not succeed?

Was it helpful?

Solution

You check socket error with getsockopt(). Here's a snippet from Stevens (granted it's Unix, but winsock should have something similar):


if ( FD_ISSET( sockfd, &rset ) || FD_ISSET( sockfd, &wset )) {
    len = sizeof(error);
    if ( getsockopt( sockfd, SOL_SOCKET, SO_ERROR, &error, &len ) < 0 )
        return -1;
} else {
    /* error */
}

Now error gives you the error number, if any.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top