dermesser / libsocket

The ultimate socket library for C and C++, supporting TCP, UDP and Unix sockets (DGRAM and STREAM) on Linux, FreeBSD, Solaris. See also my uvco library for a refreshed version!
https://borgac.net/~lbo/doc/libsocket/
Other
797 stars 195 forks source link

Detect/check if inet_stream is closed #57

Open nemo9955 opened 5 years ago

nemo9955 commented 5 years ago

I want to be notified or at least check periodically if a libsocket::inet_stream created by a libsocket::inet_stream_server::accept() is closed. I tried fcntl but it does not seem to work.

int r;
r = fcntl(cli_sock->getfd(), F_GETFL);
if (r == -1)
    is_ok = false;
dermesser commented 5 years ago

I don't think your fcntl call would work, as a socket is not a file. In general, once a call to recv(2) returns 0, you can expect the socket to be closed/the connection to have been terminated.

If you really need to detect a closed stream without reading anything from the socket, you can use epoll: the EPOLLRDHUP event flag should indicate the situation you're looking for.