0

I use epoll linux server for multiplexing. I noticed that the request to close the connection occurs in two ways:

  1. the EPOLLHUP event fires
  2. recv returns 0

It’s not entirely clear to me what the client should do so that we get EPOLLHUP on the server, and it’s also not clear what the client should do so that we get 0 on the server with recv.

I just need to close the connection in the right way, but I don't know how.

Joseph Conrad
  • 43
  • 1
  • 6
  • `close()`? Maybe. – Jesper Juhl Jul 28 '22 at 16:54
  • 1
    I think we can get 0 from recv and EPOLLHUP in different situations. This is right? – Joseph Conrad Jul 28 '22 at 16:55
  • https://stackoverflow.com/questions/52976152/tcp-when-is-epollhup-generated says this is generated only if the socket has been shut down in both directions. My suggestion: just treat EPOLLHUP the same as EPOLLIN - when you call recv you'll get 0 – user253751 Jul 28 '22 at 16:58
  • @user253751 Is it correct not to use EPOLLHUP, but just wait for recv to return 0? Is it possible that when the client closes the connection, I don't get EPOLLIN where recv returns 0, but I do get EPOLLHUP? Thank you – Joseph Conrad Jul 28 '22 at 17:05
  • https://stackoverflow.com/questions/71387404/semantically-why-does-epoll-wait-sends-me-epollin-after-read-end-had-been-shut says you will get EPOLLIN if recv will return 0 – user253751 Jul 28 '22 at 17:10
  • @user253751 Thank you so much! Yes, I am aware of this. But I'm wondering if it's possible for the connection to be closed by the client, but in that situation, shouldn't recv be expected to return 0? – Joseph Conrad Jul 28 '22 at 17:15
  • 1
    On graceful shutdown you get 0 in recv and EPOLLIN. On non-graceful shutdown I *think* you get an error from the next send or recv, but I'm not sure if you get EPOLLIN or EPOLLHUP or something else – user253751 Jul 28 '22 at 17:17
  • 2
    `EPOLLHUP` happens when you try to send to a socket and the remote machine responds with `RST`. `recv()` returns `0` when you read and the remote machine has sent a `FIN`. – Barmar Jul 28 '22 at 17:22

0 Answers0