1.select 函数原形
int select (int nfds,
fd_set FAR * readfds,
fd_set FAR * writefds,
fd_set FAR * exceptfds,
const struct timeval FAR * timeout
);
说明: 最后1个参数timeout用于决定select最多等待I/O操作完成多久的时间。如果timeout为NULL,那么select调用会无限制的锁定或停顿下去,直到至少有1个描述符符合指定的条件后结束。timeval 结构定义如下:
struct timeval
{
long tv_sec;
long tv_usec;
}
2.相关宏操作
FD_CLR(s,*set) : 从set中删除套接子s.
FD_ISSET(s,*set): 检查s是否set集合中的一元,是->返回TRUE
FD_SET(s,*set): s加入到set
FD_ZERO(*set): 清空
3.使用方法
SOCKET s;
fd_set fdread;
int ret;
//Create a socket ,and accept a connection
//...............
//Manage I/O
while(TRUE)
{
//clear set before calling select
FD_ZERO(&fdread);
FD_SET(s,&fdread);
if((ret = select(0,&fdread,NULL,NULL,NULL) == SOCKET_ERROR)
{
//ERROR;
}
if(FD_ISSET(s,&fdread))
{
//read s
}
}