Linux socket之四:使用POLL机制处理多连接
2014-04-29 15:02
435 查看
转载自:http://blog.csdn.net/zhandoushi1982/article/details/7738424
使用select函数可以处理socket多连接的问题(select的用法参见:/article/1948373.html),使用POLL也可以实现同样的功能,且调用方式更加简单。原型是:
[cpp] view
plaincopy
struct pollfd {
int fd; //文件描述符
short events; //要求查询的事件掩码
short revents; //返回的事件掩码
};
int poll(struct pollfd *ufds, unsigned int nfds, int timeout);
poll函数使用pollfd类型的结构来监控一组文件句柄,ufds是要监控的文件句柄集合,nfds是监控的文件句柄数量,timeout是等待的毫秒数,这段时间内无论I/O是否准备好,poll都会返回。timeout为负数表示无线等待,timeout为0表示调用后立即返回。执行结果:为0表示超时前没有任何事件发生;-1表示失败;成功则返回结构体中revents不为0的文件描述符个数。pollfd结构监控的事件类型如下:
[cpp] view
plaincopy
#define POLLIN 0x0001
#define POLLPRI 0x0002
#define POLLOUT 0x0004
#define POLLERR 0x0008
#define POLLHUP 0x0010
#define POLLNVAL 0x0020
#define POLLRDNORM 0x0040
#define POLLRDBAND 0x0080
#define POLLWRNORM 0x0100
#define POLLWRBAND 0x0200
#define POLLMSG 0x0400
#define POLLREMOVE 0x1000
#define POLLRDHUP 0x2000
如上是events事件掩码的值域,POLLIN|POLLPRI类似于select的读事件,POLLOUT|POLLWRBAND类似于select的写事件。当events属性为POLLIN|POLLOUT,表示监控是否可读或可写。在poll返回时,即可通过检查revents变量对应的标志位与events是否相同,比如revents中POLLIN事件标志位被设置,则表示文件描述符可以被读取。代码段示例:
[cpp] view
plaincopy
int sockfd; //套接字句柄
struct pollfd pollfds;
int timeout;
timeout = 5000;
pollfds.fd = sockfd; //设置监控sockfd
pollfds.events = POLLIN|POLLPRI; //设置监控的事件
for(;;){
switch(poll(&pollfds,1,timeout)){ //开始监控
case -1: //函数调用出错
printf("poll error \r\n");
break;
case 0:
printf("time out \r\n");
break;
default: //得到数据返回
printf("sockfd have some event \r\n");
printf("event value is 0x%x",pollfds.revents);
break;
}
}
使用select函数可以处理socket多连接的问题(select的用法参见:/article/1948373.html),使用POLL也可以实现同样的功能,且调用方式更加简单。原型是:
[cpp] view
plaincopy
struct pollfd {
int fd; //文件描述符
short events; //要求查询的事件掩码
short revents; //返回的事件掩码
};
int poll(struct pollfd *ufds, unsigned int nfds, int timeout);
poll函数使用pollfd类型的结构来监控一组文件句柄,ufds是要监控的文件句柄集合,nfds是监控的文件句柄数量,timeout是等待的毫秒数,这段时间内无论I/O是否准备好,poll都会返回。timeout为负数表示无线等待,timeout为0表示调用后立即返回。执行结果:为0表示超时前没有任何事件发生;-1表示失败;成功则返回结构体中revents不为0的文件描述符个数。pollfd结构监控的事件类型如下:
[cpp] view
plaincopy
#define POLLIN 0x0001
#define POLLPRI 0x0002
#define POLLOUT 0x0004
#define POLLERR 0x0008
#define POLLHUP 0x0010
#define POLLNVAL 0x0020
#define POLLRDNORM 0x0040
#define POLLRDBAND 0x0080
#define POLLWRNORM 0x0100
#define POLLWRBAND 0x0200
#define POLLMSG 0x0400
#define POLLREMOVE 0x1000
#define POLLRDHUP 0x2000
如上是events事件掩码的值域,POLLIN|POLLPRI类似于select的读事件,POLLOUT|POLLWRBAND类似于select的写事件。当events属性为POLLIN|POLLOUT,表示监控是否可读或可写。在poll返回时,即可通过检查revents变量对应的标志位与events是否相同,比如revents中POLLIN事件标志位被设置,则表示文件描述符可以被读取。代码段示例:
[cpp] view
plaincopy
int sockfd; //套接字句柄
struct pollfd pollfds;
int timeout;
timeout = 5000;
pollfds.fd = sockfd; //设置监控sockfd
pollfds.events = POLLIN|POLLPRI; //设置监控的事件
for(;;){
switch(poll(&pollfds,1,timeout)){ //开始监控
case -1: //函数调用出错
printf("poll error \r\n");
break;
case 0:
printf("time out \r\n");
break;
default: //得到数据返回
printf("sockfd have some event \r\n");
printf("event value is 0x%x",pollfds.revents);
break;
}
}
相关文章推荐
- Linux查询MySql默认配置文件加载路径
- 一些有用但你有可能不知道的Linux命令
- 命令查看linux 系统的位数
- python2.7执行Linux系统命令
- Linux 内核编译
- CentOS安装之后的系统安全配置
- 关于mysql安装后更改数据库路径方法-linux环境
- CentOS 安装 PostgreSQL 9.3
- linux上打包程序tar && 压缩程序 && 源代码安装软件 --简介
- CentOS 6.4 安装搭建 Scrapy 0.22 环境
- linux基础知识
- Linux中SCSI总线的磁盘扫描与盘符管理
- 利用U盘与163的yum源镜像网站安装Centos6.5 x86_64系统 推荐
- 查看Linux登陆日志
- ubuntu与centos安装软件的不同点总结
- linux grep命令
- linux下的find文件查找命令与grep文件内容查找命令
- CentOS通过DNSpod实现动态域名
- Linux串口——Struct termios结构体
- Linux下高并发socket最大连接数所受的各种限制 .