I/O多路转接& # 160;- - - - - & # 160;民意调查

  

一、调查

,,,,。


二、调查相关函数

# include & lt; poll.h>

int调查(struct pollfd * fds, nfds_t nfds, int超时);,

//fds: pollfd结构体,

,,,事件:

,,,防止:

,,,,,,,,,,,,

,,,,,,,,,,,,

,,,,,,,,,,,,

,,,,,,,,,,,,

,,,,,,,,,,,,

,,,,,,,,,,,,

,,,,,,,,,,,,

,,,,,,,,,,,,

,,,,,,,,,,,,

,,,,,,,,,,,,存储

,,,,,,,,,,,,

,,,,,,,,,,,,

,,,,,,,,,,,,

struct  pollfd    {,,,int ,, fd,,,,,,,,,,/*,文件描述符,*/,   ,,,,short 事件,,,,,,/*,请求的事件,*/,,,   ,,,,short 防止;,,,,/*,返回的事件,*/,,   };

//nfds:,

//超时:

,,,,,,:永不超时

,,,,,,,, 0:立即返回

,,,,,,,,在0:等待指定的时间

2,特点:

,,,, pollfd并没有最大数量限制(但是数量过大后性能也是会下降)。

,,,,和选择函数一样,调查返回后,需要轮询pollfd来获取就绪的描述符。

,,,,选择和调查都需要在返回后,通过遍历文件描述符来获取已经就绪的插座。事实上,同时连接的大量客户端在一时刻可能只有很少的处于就绪状态,因此随着监视的描述符数量的增长,其效率也会线性下降。

三、调查服务器

# include  & lt; stdio.h>   # include   # include   # include   # include   # include   # include   # include   # include   # include   # include      # define  _BACKLOG_  5   # define  _MAX_NUM_  64年   void 使用(const  char  * proc)   {   printf("用法:,% s  (ip)(港口)\ n”, proc);   }   static  int 启动(const  char  * ip, const  int 端口)   {   int 袜子=插座(AF_INET SOCK_STREAM 0);   如果(sock  & lt;, 0)   {   perror(“套接字”);   退出(1);   }   int 选择=1;   setsockopt(袜子、SOL_SOCKET SO_REUSEADDR,及选择,sizeof(选择);      当地struct  sockaddr_in ;   local.sin_family=AF_INET;   local.sin_port=htons(港口);   local.sin_addr.s_addr=inet_addr (ip);      如果绑定(袜子,(struct  sockaddr *),当地,sizeof(本地),& lt;, 0)   {   perror(“绑定”);   出口(2);   }   如果(听(袜子,_BACKLOG_), & lt;, 0)   {   perror(“听”);   出口(3);   }   return 袜子;   }   static  int  poll_server (int  listen_sock)   {   struct  sockaddr_in 客户端;   socklen_t  len=sizeof(客户端);      struct  pollfd  fds (_MAX_NUM_);   fds [0] .fd=listen_sock;   fds [0] .events=波淋;   fds [0] .revents=0;      size_t  i=0;   (;i<_MAX_NUM_; + + i)   {   fds[我].fd=1;   fds[我].events=0;   fds[我].revents=0;   }   int  max_fd=1;   int 超时=5000;   而(1)   {   开关(民意调查(fds、max_fd超时))   {   case  1://错误   perror(“投票”);   打破;   case  0://超时   printf (" poll 超时”);   打破;   默认值://normal    {   (i=0; i< _MAX_NUM_; + + i)   {//is  listen  events ,准备好了吗?   如果(fds[我].fd ==, listen_sock ,,, fds[我].revents ==,波淋)   {   int  accept_sock=accept (listen_sock (struct  sockaddr *),客户,和len);   如果(accept_sock  & lt;, 0)   {   perror(“接受”);   继续;   }   printf (" get  a 客户端…[ip: % s][端口:% d] \ n”, \   inet_ntoa (client.sin_addr)、ntohs (client.sin_port));   (i=0; i< _MAX_NUM_; + + i)   {   如果(fds[我].fd ==, 1)   {   fds[我].fd=accept_sock;   fds[我].events=波淋;   max_fd + +;   打破;   }   }   如果小姐:==,_MAX_NUM_)   {   关闭(accept_sock);   }   }   else 如果(fds[我].fd 祝辞,0,,,,fds[我].revents ==,波淋)   {   char  buf [1024];   ssize_t  _size=阅读(fds[我].fd,缓冲区,sizeof (buf) 1);   如果(_size 祝辞,0)//read 成功   {   但[_size]=' \ 0 ';   printf (" # Client  % s”, buf);   }   else 如果(_size ==, 0)//client 接近   {   printf (" clint 密切…\ n ");   struct  pollfd  tmp=fds(我);   fds[我]=fds (max_fd-1);   关闭(fds [max_fd-1] .fd);   fds [max_fd-1] .fd=1;   fds [max_fd-1] .events=0;   fds [max_fd-1] .revents=0;   ——max_fd;   }      }   其他的   {}   }   }   打破;   }   }   return  0;   }   null   null   null   null   null   null   null   null   null   null   null   null   null   null

I/O多路转接& # 160;- - - - - & # 160;民意调查