poll函数实现多路复用

来源:互联网 发布:编辑图片大小的软件 编辑:程序博客网 时间:2024/06/08 09:24

结构体pollfd

struct pollfd
{
int fd;               //file descriptor
short event;   //event of interest on fd
short reven;  //event that occurred on fd
}

每一个pollfd结构体指定了一个被监视的文件描述符,可以传递多个结构体,指示poll()监视多个文件描述符。每个结构体的events域是监视该文件描述符的事件掩码,由用户来设置这个域。revents域是文件描述符的操作结果事件掩码。内核在调用返回时设置这个域。events域中请求的任何事件都可能在revents域中返回。

pollfd结构体合法的事件

POLLIN
有数据可读。
POLLRDNORM
有普通数据可读。
POLLRDBAND
有优先数据可读。
POLLPRI
有紧迫数据可读。
POLLOUT
写数据不会导致阻塞。
POLLWRNORM
写普通数据不会导致阻塞。
POLLWRBAND
写优先数据不会导致阻塞。
POLLMSG
SIGPOLL 消息可用。
此外,revents域中还可能返回下列事件:
POLLER
指定的文件描述符发生错误。
POLLHUP
指定的文件描述符挂起事件。
POLLNVAL
指定的文件描述符非法。
这些事件在events域中无意义,因为它们在合适的时候总是会从revents中返回。使用poll()和select()不一样,你不需要显式地请求异常情况报告。
POLLIN | POLLPRI等价于select()的读事件,POLLOUT |POLLWRBAND等价于select()的写事件。POLLIN等价于POLLRDNORM |POLLRDBAND,而POLLOUT则等价于POLLWRNORM。
例如,要同时监视一个文件描述符是否可读和可写,我们可以设置 events为POLLIN |POLLOUT。在poll返回时,我们可以检查revents中的标志,对应于文件描述符请求的events结构体。如果POLLIN事件被设置,则文件描述符可以被读取而不阻塞。如果POLLOUT被设置,则文件描述符可以写入而不导致阻塞。这些标志并不是互斥的:它们可能被同时设置,表示这个文件描述符的读取和写入操作都会正常返回而不阻塞。
timeout参数指定等待的毫秒数,无论I/O是否准备好,poll都会返回。timeout指定为负数值表示无限超时;timeout为0指示poll调用立即返回并列出准备好I/O的文件描述符,但并不等待其它的事件。这种情况下,poll()就像它的名字那样,一旦选举出来,立即返回。

函数原型

int poll(struct pollfd fdarray[], nfds_t nfds, int timeout);
poll返回值和错误代码
成功时,poll()返回结构体中revents域不为0的文件描述符个数;如果在超时前没有任何事件发生,poll()返回0;失败时,poll()返回-1,并设置errno为下列值之一:
EBADF
一个或多个结构体中指定的文件描述符无效。
EFAULT
fds指针指向的地址超出进程的地址空间。
EINTR
请求的事件之前产生一个信号,调用可以重新发起。
EINVAL
nfds参数超出PLIMIT_NOFILE值。
ENOMEM
可用内存不足,无法完成请求。

以上理论摘自http://www.cnblogs.com/nathan-1988/archive/2012/07/01/2571786.html 谢谢~~~

代码如下所示:

[cpp] view plaincopy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <unistd.h>  
  4. #include <errno.h>  
  5. #include <string.h>  
  6. #include <sys/types.h>  
  7. #include <sys/socket.h>  
  8. #include <netinet/in.h>  
  9. #include <arpa/inet.h>  
  10. #include <poll.h>  
  11. #define MYPORT 6666    // the port users will be connecting to  
  12.   
  13. #define BACKLOG 5     // how many pending connections queue will hold  
  14. #ifndef INFTIM  
  15. #define INFTIM -1  
  16. #endif  
  17. #define BUF_SIZE 1024  
  18. #define MAXLINE 100  
  19. #define MAXCLIENT 5  
  20. int fd_access[BACKLOG];    // accepted connection fd  
  21. int conn_amount;    // current connection amount  
  22.   
  23. int main(void)  
  24. {  
  25.         int sock_fd, new_fd,len,conn_no;  // listen on sock_fd, new connection on new_fd  
  26.         struct sockaddr_in server_addr;    // server address information  
  27.         struct sockaddr_in client_addr; // connector's address information  
  28.         socklen_t sin_size;  
  29.         char buf[BUF_SIZE];  
  30.         int result;  
  31.         int i;  
  32.         int maxsock=0;  
  33.         int conn_fd;  
  34.         struct pollfd client[MAXCLIENT];  
  35.         if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)   
  36.         {  
  37.                 perror("socket");  
  38.                 exit(1);  
  39.         }   
  40.         server_addr.sin_family = AF_INET;         // host byte order  
  41.         server_addr.sin_port = htons(MYPORT);     // short, network byte order  
  42.         server_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP  
  43.         memset(server_addr.sin_zero, '\0'sizeof(server_addr.sin_zero));  
  44.         if (bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1)   
  45.         {  
  46.                 perror("bind");  
  47.                 exit(1);  
  48.         }  
  49.         if (listen(sock_fd, BACKLOG) == -1)   
  50.         {  
  51.                 perror("listen");  
  52.                 exit(1);  
  53.         }  
  54.         printf("listen port %d\n", MYPORT);  
  55.   
  56.         sin_size = sizeof(client_addr);  
  57.         client[0].fd=sock_fd;  
  58.         client[0].events=POLLIN;  
  59.         for(i=1;i<MAXCLIENT;i++)  
  60.                 client[i].fd=-1;  
  61.         while (1)   
  62.         {  
  63.                 printf("helo\n");  
  64.                 result=poll(client,maxsock+1,INFTIM);  
  65.                 if(client[0].revents&POLLIN)  
  66.                 {  
  67.                         new_fd=accept(sock_fd,(struct sockaddr*)&client_addr,&sin_size);  
  68.                         for(i = 1; i < MAXCLIENT;++i)  
  69.                         {  
  70.                                 if( client[i].fd < 0 )  
  71.                                 {         
  72.                                         client[i].fd = new_fd;  
  73.                                         client[i].events = POLLIN;//POLLRDNORM;  
  74.                                         break;  
  75.                                 }  
  76.                         }  
  77.                         if( i == MAXCLIENT)  
  78.                         {  
  79.                                 printf("too many clients");   
  80.                                 exit(1);  
  81.                         }  
  82.                         if( i > maxsock )  
  83.                                 maxsock = i;  
  84.                         if( --result<= 0 )  
  85.                                 continue;  
  86.                 }  
  87.                 for(i = 1; i <= maxsock; i++)  
  88.                 {  
  89.                         if( (conn_fd = client[i].fd) < 0)  
  90.                                 continue;  
  91.                         if(client[i].revents & (POLLIN | POLLERR))  
  92.                         {  
  93.                                 if( (len = read(conn_fd, buf, MAXLINE)) < 0)   
  94.                                 {  
  95.                                         if( errno == ECONNRESET)  
  96.                                         {  
  97.                                                 close(conn_fd);  
  98.                                                 client[i].fd = -1;  
  99.                                         }  
  100.                                         else  
  101.                                                 perror("read error");  
  102.                                 }  
  103.                                 else if(len == 0)  
  104.                                 {  
  105.                                         close(conn_fd);  
  106.                                         client[i].fd = -1;  
  107.                                 }  
  108.                                 else  
  109.                                         printf("%s\n",buf);  
  110.                                 if(--conn_no <= 0)  
  111.                                         break;  
  112.   
  113.                         }  
  114.                 }  
  115.         }  
  116.         exit(0);  
  117. }  
  118.   
  119. (END)   


以上是服务器端

客户端代码见http://blog.csdn.net/xluren/article/details/8043484#t15




FROM: http://blog.csdn.net/xluren/article/details/8206371

0 0