Home > Mobile >  recv call returns 0 vs POLLHUP event in C poll
recv call returns 0 vs POLLHUP event in C poll

Time:06-19

(In C socket programming, using poll to manage the fd's and its events)
With a data stream socket returned from an accept() call, what's the difference between the event POLLHUP and getting 0 when calling recv(fd, ...) ? They both seem to indicate the connection was closed from the other end, but I wanted to know if there is any technical difference between the two.

CodePudding user response:

POLLHUP in the revents of a struct pollfd tells you if the other end of a pipe or socket has been closed. That means you can't write to the descriptor (If it's open for writing) without getting a SIGPIPE, but there might still be data pending waiting to be read (If it's open for reading). Once all remaining bytes have been read, then functions like recv(2) and read(2) will return 0.

Both POLLIN and POLLHUP can thus be set at the same time, but not POLLOUT and POLLHUP - those two are mutually exclusive.

  • Related