multiplexed I/O Model poll () Model Code implementation

Source: Internet
Author: User

multiplexed I/O Model poll () Model Code implementation

The poll () mechanism is similar to the select () mechanism, and is a way to poll multiple descriptors.

The difference is that poll () does not have a limit on the number of descriptors.

is to poll each descriptor with a struct POLLFD struct.

struct POLLFD Fdarray

{

int FD; /* File Descriptor */

Short events; /* Indicates the awaited event */

Short revents;/* represents an event that actually occurs when the event is returned */

};

Data.h

#ifndef data_h#defineData_h#include<string.h>#include<stdio.h>#include<poll.h>#include<sys/types.h>#include<sys/stat.h>#include<stdlib.h>#include<errno.h>#include<netinet/inch.h>#include<sys/socket.h>#include<sys/Select.h>#include<arpa/inet.h>#include<assert.h>#include<unistd.h>#defineMAXN 1100#defineMAXLINE 10#defineLISTEN 10#defineIP "127.0.0.1"#definePORT 4578#defineBACKLOG 5#defineINFTIM-1#endif

Server.c

1#include"data.h"2 Static voidDeal_message (structPOLLFD *fd_array,intnum);3 Static voidPoll_accept (intsockfd);4 5 Static intInit ()6 {7     structsockaddr_in server_in;8     intsockfd;9     if(SOCKFD = socket (Af_inet,sock_stream,0)) == -1)Ten     { Onefprintf (stderr,"socket Fail,error:%s\n", Strerror (errno)); A         return-1; -     } -Bzero (&server_in,sizeof(server_in)); theserver_in.sin_family =af_inet; -Server_in.sin_port =htons (PORT); -Inet_pton (af_inet,ip,&server_in.sin_addr); -     if(Bind (SOCKFD, (structsockaddr*) &server_in,sizeof(structSOCKADDR)) = =-1) +     { -fprintf (stderr,"bind fail,error:%s\n", Strerror (errno)); +         return-1; A     } at Listen (sockfd,backlog); -     returnsockfd; - } -  - Static voidPoll_accept (intsockfd) - { in     structPOLLFD FD_ARRAY[MAXN]; -     intAfD; to     intPOLLFD; +     structsockaddr_in client_in; -Bzero (&client_in,sizeof(client_in)); the     inti =0; *fd_array[0].FD =sockfd; $fd_array[0].events =Pollin;Panax Notoginseng      for(i=0; i<maxn;i++) -     { theFD_ARRAY[I].FD =-1; +     } A     intnum =0; the     intLen =sizeof(client_in); +      while(1) -     { $          $POLLFD = Poll (fd_array,num+1, Inftim); -         if(POLLFD = =-1) -         { thefprintf (stderr,"Poll Fail,error%s\n", Strerror (errno)); -             return;Wuyi         } the         if(POLLFD = =0) -         { Wu             Continue; -         } About         if(fd_array[0].revents &pollout) $         { -             if(AfD = Accept (SOCKFD, (structsockaddr*) (&client_in,&len)) = =-1) -             { -fprintf (stderr,"Accept fail,error:%s\n", Strerror (errno)); A                 return ; +             } the         } -i =0; $          for(i =0; i<maxn;i++) the         { the             if(Fd_array[i].fd <0) the             { theFD_ARRAY[I].FD =AfD; -                  Break; in             } the         } the         if(i = =MAXN) About         { theprintf"too many to server!\n"); the             return ; the         } +Fd_array[i].events =Pollin; -         if(I >num) thenum =i;Bayi         if(--POLLFD <=0) the             Continue; the     } - deal_message (fd_array,num); - } the  the Static voidDeal_message (structPOLLFD *fd_array,intnum) the { the     intI,n; -     CharBUF[MAXN]; thememset (BUF,' /',sizeof(BUF)); the      for(i=1; i<=num;i++) the     {94         if(Fd_array[i].fd <0) the             Continue; the         if(fd_array[i].revents&Pollin) the         {98n =read (FD_ARRAY[I].FD,BUF,MAXN); About             if(n = =0) -             {101 Close (FD_ARRAY[I].FD);102FD_ARRAY[I].FD =-1;103                 Continue;104             } the write (stdout_fileno,buf,n);106 write (fd_array[i].fd,buf,n);107         }108     }109 } the intMain ()111 { the     intSOCKFD =init ();113 poll_accept (SOCKFD); the Close (SOCKFD); the     return 0; the}

multiplexed I/O Model poll () Model Code implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.