I'm trying to detect whenever client has been disconnected with select() function. The problem is that, I don't quite much understood how select() does work. I'm using following code, can you tell me what I'm doing wrong and/or how to detect if client is disconnected? I'm using non-blocking sockets.
int Network::bytesAvailable()
{
long bytes = 0;
if(ioctl(this->sockfd, FIONREAD, &bytes) < 0)
{
printf("ERROR: Network:bytesAvailable: ioctl() call failed.\n");
return -1;
}
return bytes;
}
NetworkStatus Network::status()
{
struct timeval tv;
fd_set fd;
int result = 0;
tv.tv_sec = 5;
tv.tv_usec = 0;
FD_ZERO(&fd);
FD_SET(this->sockfd, &fd);
result = select(this->sockfd + 1, &fd, 0, 0, &tv);
if(result && !this->bytesAvailable())
{
return -1; // disconnected, I'm guessing this is definitely WRONG.
}
else if(result > 0 && FD_ISSET(this->sockfd, &fd))
{
return 1; // bytes available.
}
else if(!result)
{
return 0; // timeout
}
return -1; // select() call failed.
}
keepalive
andselect()
we use for multiplexing. Is it not correct?