可参考链接select函数详解-CSDN博客
select读模板:
int retval;
fd_set rfds;
struct timeval tv;
FD_ZERO(&rfds);
FD_SET(m_fd, &rfds);
tv.tv_sec = s32MilliSec / 1000; // s32millisec 为5000时,为五秒
tv.tv_usec = (s32MilliSec % 1000) * 1000;
retval = select(m_fd + 1, &rfds, NULL, NULL, &tv);
if (retval == -1) {
perror("select()");
} else if (retval && FD_ISSET(m_fd, &rfds)) {
int32 s32ReadLen = SafeRead(m_fd, pu8Buf, s32BufLen);
if (s32ReadLen <= 0) {
XSleep(100);
return -1;
} else {
return s32ReadLen;
}
} else //time out
{
}