3

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?

SQB
  • 3,926
  • 2
  • 28
  • 49
Patrick Glandien
  • 7,791
  • 5
  • 41
  • 47

1 Answers1

3

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.

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171