#include
#include
#include
#include
#include
#include
Your hypothesis is correct. The Linux udp(7) man page describes the situation thus:
All fatal errors will be passed to the user as an error return even when the socket is not connected. This includes asynchronous errors received from the network. You may get an error for an earlier packet that was sent on the same socket.
This behavior differs from many other BSD socket implementations which don't pass any errors unless the socket is connected. Linux's behavior is mandated by RFC 1122.When the
IP_RECVERRoption is enabled all errors are stored in the socket error queue and can be received byrecvmsg(2)with theMSG_ERRQUEUEflag set.