Quote from Dan Bernstein :
Situation: you install a non-blocking socket and do connect (), which returns -1 / EINPROGRESS or -1 / EWOULDBLOCK. You select () a socket for recording. This returns as soon as the connection succeeds or fails. (Exception: in some older versions of Ultrix select () would not have noticed an error before a 75 second timeout.)
Question: What do you do after select () returns writeability? Did the connection fail? If so, how did this happen?
If the connection fails, the reason is hidden inside what is called so_error in the socket. Modern systems allow you to see so_error with getsockopt (, SO_ERROR,) ...
He further discusses the fact that getsockopt(,,SO_ERROR,,) is a modern invention that does not work on older systems and how to get an error code on such systems. But you probably should not worry about this if you are programming a Unix / Linux system released over the past 15 years.
The Linux SO_ERROR page for connect describes the same use of SO_ERROR .
So, if you are performing asynchronous socket operations, you may need to use SO_ERROR . Otherwise, just use errno .
source share