我试图检测客户端何时与select()函数断开连接.问题是,我不太了解select()是如何工作的.我正在使用以下代码,你能告诉我我做错了什么和/或如何检测客户端是否断开连接?
我正在使用非阻塞套接字.
我正在使用非阻塞套接字.
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,&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. }