套接字编程中,select:bad file descriptor

来源:互联网 发布:ubuntu pyqt4安装 编辑:程序博客网 时间:2024/05/16 16:05

第一次select的是server的socket,accept一个连接的fd之后加入select,再select就出错了...

An invalid file descriptor was given in one of the sets. (Perhaps a file descriptor that was already closed, or one on which an error has occurred.) 

原因是原来是不小心把套接字关了



另外看到有这样的说法:

错误提示信息是:Bad File Descriptor

原因在于,上面的写法有误!

int ret=SOCKET_ERROR; fd_set FDRead={0}; struct timeval timeout={0};   FD_ZERO(&FDRead);  FD_SET(sock, &FDRead);        timeout.tv_sec = EPOLL_SERVER_TIME_OUT;        timeout.tv_usec = EPOLL_SERVER_TIME_OUT_MILLSECONDS;  ret = select(sock+1, &FDRead, 0, 0, &timeout);  if(ret == SOCKET_ERROR)  {   break;  }


int ret=SOCKET_ERROR; fd_set FDRead={0}; struct timeval timeout={0};   FD_ZERO(&FDRead);  FD_SET(sock, &FDRead);        timeout.tv_sec = EPOLL_SERVER_TIME_OUT;        timeout.tv_usec = EPOLL_SERVER_TIME_OUT_MILLSECONDS;  ret = select(sock+1, &FDRead, 0, 0, &timeout);  if(ret == SOCKET_ERROR)  {   break;  }

不应该捕获无关的套接字事件!