codereviewanskquestions
codereviewanskquestions

Reputation: 13998

epoll best way to detect user disconnection

I am working on a network programming using epoll. I was wondering the best way to detect user disconnection. Right now, I am using select with timeout to see if there is a signal in the receive buffer and there is no signal for a certain amount of time then it closes the socket connection.

I think there might be something else more suitable for epoll instead of using select.

Thanks in advance..

Upvotes: 4

Views: 4292

Answers (2)

Fredde-Fisk
Fredde-Fisk

Reputation: 131

You can still use epoll, and use the timeout argument in epoll_wait to detect a timeout.

If you have a TCP session and want to detect when the remote peer closes the connection you register to receive the EPOLLRDHUP event, or you can detect it by getting an errno == EAGAIN when receiving the EPOLLIN event and trying to read (non blocking) from the closed socket.

Upvotes: 5

nos
nos

Reputation: 229058

I think there might be something else more suitable for epoll instead of using select.

No, there is not. If you need to discover an inactive or dead client, you have to do it yourself. (e.g. send them some form of heartbeat messages and see if they respond/error within a timeout, or do as you're already doing and time them out after a perioid of inactivity.)

Upvotes: 1

Related Questions