Search code examples
socketsposix

SO_ERROR value after successful socket operation


I'm curious about the behavior of the SO_ERROR socket option used with getsockopt() after a successful socket operation

The Open Group specification:

SO_ERROR Reports information about error status and clears it. This option shall store an int value.

Usually I see SO_ERROR used after a socket operation returns -1, but what happens if the previous socket operation succeeded (thus not returning -1). Does the getsockopt() call fail? Does it return 0 as the int value?


Solution

  • I've learned more about SO_ERROR in Unix Networking Programmig Vol 1, it's become clear to me. SO_ERROR is used to report asynchronous errors that are the result of events within the network stack and not synchronous errors that are the result of a library call (send/recv/connect). Synchronous results are reported via errno.

    Calling getsockopt() with SO_ERROR after a library call returns -1 is incorrect from the POSIX implementation.

    Learning of the non-blocking connect result via select is an example of discovering when the asynchronous result is ready (which can then be retrieved via SO_ERROR)