select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
来源:互联网 发布:c语言 string比较 编辑:程序博客网 时间:2024/06/06 23:02
转自http://blog.sina.com.cn/s/blog_a43aa27401015kt9.html
用于学习
select函数用于在非阻塞中,当一个套接字或一组套接字有信号时通知你,系统提供select函数来实现多路复用输入/输出模型,原型:
int select(int maxfd,fd_set *rdset,fd_set *wrset,fd_set *exset,struct timeval *timeout);
步骤如下——
socket s;
.....
fd_set set;
while(1)
{
FD_ZERO(&set);//将你的套节字集合清空
FD_SET(s, &set);//加入你感兴趣的套节字到集合,这里是一个读数据的套节字s
select(0,&set,NULL,NULL,NULL);//检查套节字是否可读,
//很多情况下就是是否有数据(注意,只是说很多情况)
//这里select是否出错没有写
if(FD_ISSET(s, &set) //检查s是否在这个集合里面,
{ //select将更新这个集合,把其中不可读的套节字去掉
//只保留符合条件的套节字在这个集合里面
recv(s,...);
}
//do something here
}
基于上面的讨论,可以轻松得出select模型的特点:
使用select函数的过程一般是:
以下是一个测试单个文件描述字可读性的例子:
int isready(int fd)
{
int rc;
fd_set fds;
struct tim tv;
FD_ZERO(&fds);
FD_SET(fd,&fds);
tv.tv_sec = tv.tv_usec = 0;
rc = select(fd+1, &fds, NULL, NULL, &tv);
if (rc < 0) //error
return -1;
return FD_ISSET(fd,&fds) ? 1 : 0;
}
- select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- select函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- select函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- select函数以及 FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- unity连接sqlite数据库操作
- vncviewer远程登陆mac总是崩溃,提示Rect too large: 1282x900
- System.Web.Caching.Cache类 缓存
- JAVA-p257 T7.17
- Received lower prio advert, forcing new election (KEEP ALIVED)
- select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- POJ-1426 Find The Multiple
- 《解析C#类中的构造函数》
- 我的记事本项目之路(四)
- 重新定位svn地址的方法(windows和linux),svn switch(sw)的帮助信息
- GridView嵌套布局
- sql2008常用语句
- ListIterator和Iterator区别第十二篇
- xml约束技术之dtd