Search code examples
clinuxudphandlepolling

POLL handling with UDP


I've been writing (with almighty C) a server wich listens in UDP. I've got 2 sockets listening on the same port (IPv4 and IPv6) and im using poll() to monitor both sockets.

So, when listening, what is the correct way to handle poll revents erros ? Is possible to receive a POLLHUP? If its possible how it must be handled? Same goes for POLLERR

Also, its possible to receive a POLLRDBAND or POLLPRI? If so then what does it means and how it must be handled?

Thanks in advance.

PS: All questions are UDP-related


Solution

  • POLLHUP indicates that the socket has been disconnected. Because your program is listening on a UDP socket, you shouldn't get this error condition.

    You might get a POLLERR, however, and should be prepared to handle it. The POSIX.1-2008 standard says

    POLLERR
    An error has occurred on the device or stream. This flag is only valid in
    the revents bitmask; it shall be ignored in the events member.
    

    and it's possible that the error is due to a hardware failure. You should destroy the socket and attempt to re-create it.

    Unlike TCP, UDP doesn't have a mechanism for marking a packet as urgent or out-of-band, so you should't get a POLLRDBAND or POLLPRI. Also, both of these conditions are optional obsolescent extensions -- so they shouldn't be used according to the POSIX.1-2008 standard:

    Obsolescent
    The functionality described may be removed in a future version of this 
    volume of POSIX.1-2008. Strictly Conforming POSIX Applications and Strictly 
    Conforming XSI Applications shall not use obsolescent features.
    
    XSI STREAMS
    The functionality described is optional. The functionality described is also 
    an extension to the ISO C standard.