Can someone provide me an example of how to use select() to see if a client has closed the connection on a socket?
FYI. I\'m using linux.
Thanks!
The below snippet first checks if the socket is marked readable (which it is when closed) and then if there's actually anything to be read.
#include
#include
#include
#include
bool isclosed(int sock) {
fd_set rfd;
FD_ZERO(&rfd);
FD_SET(sock, &rfd);
timeval tv = { 0 };
select(sock+1, &rfd, 0, 0, &tv);
if (!FD_ISSET(sock, &rfd))
return false;
int n = 0;
ioctl(sock, FIONREAD, &n);
return n == 0;
}