How to check the connection with the client

I am working on network programming using epoll. I have a list of connections and every client on the list. I can detect a user disconnect by reading 0 if the user logs off normally. However, if the user somehow unexpectedly disconnected, then no one knows about it until he tries to send data to the user.

I don't think epoll provides a good way to handle this. So I think I have to deal with it on my own. I will be very grateful if you guys can provide me with something like examples or links related to this problem.

+3
networking tcp epoll
Jun 22 2018-11-11T00:
source share
2 answers

epoll_wait will return EPOLLHUP or EPOLLERR for the socket if the other side disconnects. EPOLLHUP and EPOLLERR are installed automatically, but you can also install a new EPOLLRDHUP, which explicitly reports peer disconnection.

Also, if you use send with the MSG_NOSIGNAL flag, it will set EPIPE to closed connections.

int resp = send (sock, buf, buflen, MSG_NOSIGNAL);

if (resp == -1 & & errno == EPIPE) {/ * the other side is gone * /}

Much better than getting a signal.

+5
Jun 22 '11 at 17:44
source share

How about TCP Keepalives: http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html . See "Checking Dead Peers." A later section on the same site provides an example code: http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/programming.html .

0
Jun 22 '11 at 3:00
source share



All Articles