For getting socket syscall (like recv
) error, which is better (at performance level) ?
- Use the plain old
errno
- Or use
SO_ERROR
asgetsockopt()
optname ?
I think errno
(defined to __error()
on my system) is faster because it's not a system call. Am I right ?
The advantages of SO_ERROR are : automatic error reset after getting, and we are sure that the error only concerns our socket. It's safer.
Which one do you think is better ? Is there a really difference of performance between the two ?
Quoting Dan Bernstein:
He goes on to discuss the fact that
getsockopt(,,SO_ERROR,,)
is a modern invention that doesn't work on old systems, and how to get the error code on such systems. But you probably don't need to worry about that if you're programming for a Unix/Linux system released in the last 15 years.The Linux man page for
connect
describes the same usage ofSO_ERROR
.So, if you're performing asynchronous operations on sockets, you may need to use
SO_ERROR
. In any other case, just useerrno
.