0

(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.

Shawn
  • 47,241
  • 3
  • 26
  • 60
carce-bo
  • 480
  • 2
  • 10

1 Answers1

0

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.

Shawn
  • 47,241
  • 3
  • 26
  • 60
  • I see. So I should add to events POLLIN and POLLHUP and when POLLHUP is registered, call recv if theres data left until I get a 0 return value ? I was previously just handling POLLIN with recv = 0 as a closed connection. – carce-bo Jun 18 '22 at 12:05
  • @carce-bo `POLLHUP` has no meaning when in `events`; it's explicitly ignored, just like `POLLERR`. But basically yes. – Shawn Jun 18 '22 at 12:06