Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SO_ERROR value after successful socket operation

Tags:

posix

sockets

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?

like image 682
Joel Cunningham Avatar asked Oct 27 '25 02:10

Joel Cunningham


1 Answers

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)

like image 122
Joel Cunningham Avatar answered Oct 28 '25 19:10

Joel Cunningham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!