现在的位置: 首页 > 操作系统 > 正文

Linux网络编程:I/O复用之select详解

2020年02月10日 操作系统 ⁄ 共 5668字 ⁄ 字号 评论关闭
文章目录

一、I/O复用概述

I/O复用概念:

解决进程或线程阻塞到某个 I/O 系统调用而出现的技术,使进程不阻塞于某个特定的 I/O 系统调

I/O复用使用的场合:

1.当客户处理多个描述符(通常是交互式输入、网络套接字)时,必须使用I/O复用。

2.tcp服务器既要处理监听套接字,又要处理已连接套接字,一般要使用I/O复用。

3.如果一个服务器既要处理tcp又要处理udp,一般要使用I/O复用。

4.如果一个服务器要处理多个服务或多个服务时,一般要使用I/O复用。

I/O复用常用函数:

select、poll

二、select()函数

select函数介绍:

int select(int maxfd, fd_set *readset,fd_set *writeset, fd_set *exceptset,const struct timeval *timeout);

功能:轮询扫描多个描述符中的任一描述符是否发生响应,或经过一段时间后唤醒

参数:

参数名称说明maxfd指定要检测的描述符的范围所检测描述符最大值+1readset可读描述符集监测该集合中的任意描述符是否有数据可读writeset可写描述符集监测该集合中的任意描述符是否有数据可写exceptset异常描述符集监测该集合中的任意描述符是否发生异常timeout超时时间超过规定时间后唤醒

返回值:

0:超时

-1:出错

>0:准备好的文件描述符数量

头文件:

#include <sys/select.h>#include <sys/time.h>

超时时间:

//该结构体表示等待超时的时间struct timeval{long tv_sec;//秒long tv_usec;//微秒};

//比如等待10.2秒struct timeval timeout;timeoout.tv_sec = 10;timeoout.tv_usec = 200000;

//将select函数的timeout参数设置为NULL则永远等待

描述符集合的操作:

select()函数能对多个文件描述符进行监测,如果一个参数对应一个描述符,那么select函数的4个参数最多能监测4个文件描述,那他如何实现对多个文件描述符的监测的呢?

大家想一想文件描述符基本具有3种特性(读、写、异常),如果我们统一将监测可读的描述符放入可读集合(readset),监测可写的描述符放入可写集合(writeset),监测异常的描述符放入异常集合(exceptset)。然后将这3个集合传给select函数,是不是就可监测多个描述符呢.

如何将某个描述符加入到特定的集合中呢?这时就需要了解下面的集合操作函数

/初始化描述符集void FD_ZERO(fd_set *fdset);

//将一个描述符添加到描述符集void FD_SET(int fd, fd_set *fdset);

//将一个描述符从描述符集中删除void FD_CLR(int fd, fd_set *fdset);

//检测指定的描述符是否有事件发生int FD_ISSET(int fd, fd_set *fdset);

select()函数整体使用框架:

例子:检测 0、4、5 描述符是否准备好读

while(1){fd_set rset;//创建一个描述符集rsetFD_ZERO(&rset);//对描述符集rset清零FD_SET(0, &rset);//将描述符0加入到描述符集rset中FD_SET(4, &rset);//将描述符4加入到描述符集rset中FD_SET(5, &rset);//将描述符5加入到描述符集rset中if(select(5+1, &rset, NULL, NULL, NULL) > 0){ if(FD_ISSET(0, &rset)) { //描述符0可读及相应的处理代码 } if(FD_ISSET(4, &rset)) { //描述符4可读及相应的处理代码 } if(FD_ISSET(5, &rset)) { //描述符5可读及相应的处理代码 }}}

三、select函数的应用对比

我们通过udp同时收发的例子来说明select的妙处。

对于udp同时收发立马想到的是一个线程收、另一个线程发,下面的代码就是通过多线程来实现

#include <string.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/select.h>#include <sys/time.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <pthread.h>

//接收线程:负责接收消息并显示void *recv_thread(void* arg){int udpfd = (int)arg;struct sockaddr_in addr;socklen_t addrlen = sizeof(addr);bzero(&addr,sizeof(addr));while(1){ char buf[200] = ""; char ipbuf[16] = ""; recvfrom(udpfd, buf, sizeof(buf), 0, (struct sockaddr*)&addr, &addrlen); printf("\r33[31m[%s]:33[32m%s\n",inet_ntop(AF_INET,&addr.sin_addr,ipbuf,sizeof(ipbuf)),buf); write(1,"UdpQQ:",6);}return NULL;}

int main(int argc,char *argv[]){char buf[100] = "";int udpfd = 0;pthread_t tid;struct sockaddr_in addr;struct sockaddr_in cliaddr;//对套接字地址进行初始化bzero(&addr,sizeof(addr));addr.sin_family = AF_INET;addr.sin_port = htons(8000);addr.sin_addr.s_addr = htonl(INADDR_ANY);

bzero(&cliaddr,sizeof(cliaddr));cliaddr.sin_family = AF_INET;cliaddr.sin_port = htons(8000);

//创建套接口if( (udpfd = socket(AF_INET,SOCK_DGRAM, 0)) < 0){ perror("socket error"); exit(-1);}//设置端口if(bind(udpfd, (struct sockaddr*)&addr, sizeof(addr)) < 0){ perror("bind error"); close(udpfd); exit(-1);}printf("input: \"sayto 192.168.221.X\" to sendmsg to somebody\n");//创建接收线程pthread_create(&tid, NULL, recv_thread, (void*)udpfd); //创建线程printf("33[32m"); //设置字体颜色fflush(stdout);while(1){ //主线程负责发送消息 write(1,"UdpQQ:",6);//1 表示标准输出 fgets(buf, sizeof(buf), stdin); //等待输入 buf[strlen(buf) - 1] = ''; //确保输入的最后一位是'' if(strncmp(buf, "sayto", 5) == 0) { char ipbuf[INET_ADDRSTRLEN] = ""; inet_pton(AF_INET, buf+6, &cliaddr.sin_addr);//给addr套接字地址再赋值. printf("\rconnect %s successful!\n",inet_ntop(AF_INET,&cliaddr.sin_addr,ipbuf,sizeof(ipbuf))); continue; } else if(strncmp(buf, "exit",4) == 0) { close(udpfd); exit(0); } sendto(udpfd, buf, strlen(buf),0,(struct sockaddr*)&cliaddr, sizeof(cliaddr));}return 0;}

运行结果:

用select来完成上述同样的功能:

#include <string.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/select.h>#include <sys/time.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>

int main(int argc,char *argv[]){int udpfd = 0;struct sockaddr_in saddr;struct sockaddr_in caddr;

bzero(&saddr,sizeof(saddr));saddr.sin_family = AF_INET;saddr.sin_port = htons(8000);saddr.sin_addr.s_addr = htonl(INADDR_ANY);bzero(&caddr,sizeof(caddr));caddr.sin_family = AF_INET;caddr.sin_port = htons(8000);//创建套接字if( (udpfd = socket(AF_INET,SOCK_DGRAM, 0)) < 0){ perror("socket error"); exit(-1);}//套接字端口绑字if(bind(udpfd, (struct sockaddr*)&saddr, sizeof(saddr)) != 0){ perror("bind error"); close(udpfd); exit(-1);}

printf("input: \"sayto 192.168.220.X\" to sendmsg to somebody33[32m\n");while(1){ char buf[100]=""; fd_set rset;//创建文件描述符的聚合变量 FD_ZERO(&rset); //文件描述符聚合变量清0 FD_SET(0, &rset);//将标准输入添加到文件描述符聚合变量中 FD_SET(udpfd, &rset);//将udpfd添加到文件描述符聚合变量中 write(1,"UdpQQ:",6); if(select(udpfd + 1, &rset, NULL, NULL, NULL) > 0) { if(FD_ISSET(0, &rset))//测试0是否可读写 { fgets(buf, sizeof(buf), stdin); buf[strlen(buf) - 1] = ''; if(strncmp(buf, "sayto", 5) == 0) { char ipbuf[16] = ""; inet_pton(AF_INET, buf+6, &caddr.sin_addr);//给addr套接字地址再赋值. printf("\rsay to %s\n",inet_ntop(AF_INET,&caddr.sin_addr,ipbuf,sizeof(ipbuf))); continue; } else if(strcmp(buf, "exit")==0) { close(udpfd); exit(0); } sendto(udpfd, buf, strlen(buf),0,(struct sockaddr*)&caddr, sizeof(caddr)); } if(FD_ISSET(udpfd, &rset))//测试udpfd是否可读写 { struct sockaddr_in addr; char ipbuf[INET_ADDRSTRLEN] = ""; socklen_t addrlen = sizeof(addr); bzero(&addr,sizeof(addr)); recvfrom(udpfd, buf, 100, 0, (struct sockaddr*)&addr, &addrlen); printf("\r33[31m[%s]:33[32m%s\n",inet_ntop(AF_INET,&addr.sin_addr,ipbuf,sizeof(ipbuf)),buf); } }}return 0;}

运行结果:

本文代码可以到Linux公社资源站下载:

------------------------------------------分割线------------------------------------------

免费下载地址在 http://linux.xuebuyuan.com/

用户名与密码都是www.xuebuyuan.com

具体下载目录在 /2017年资料/2月/13日/Linux网络编程:IO复用之select详解/

下载方法见 http://www.xuebuyuan.com/Linux/2013-07/87684.htm

------------------------------------------分割线------------------------------------------

本文永久更新链接地址:http://www.xuebuyuan.com/Linux/2017-02/140571.htm

以上就上有关Linux网络编程:I/O复用之select详解的相关介绍,要了解更多I/O复用,select,Linux网络编程,Linux网络编程:I/O复用之select详解,编程,Linux编程,Linux Shell,Android,Android教程,JAVA,C语言,Python,HTML5内容请登录学步园。

抱歉!评论已关闭.