The poll of Linux/unix network programming

Source: Internet
Author: User
Tags readable

Transfer from http://www.cnblogs.com/zhuwbox/p/4222382.html

Poll is similar to select, which iterates through the descriptor to see if a descriptor is ready. Returns the number of ready file descriptors if one is available. The poll function is as follows:

#include <poll.h>

int poll (struct POLLFD *fdarray, unsigned long nfds, int timeout)

The first argument points to the pointer to the first element of the structure array, each of which is a POLLFD structure that specifies the condition to test for a given descriptor, FD.

struct POLLFD

{

int FD;

Short events;//cares about the events happening on FD

Short REVENTS;//FD actually happened on the event

}

The condition to be tested is specified by the events member, and the function returns the state of the descriptor in the corresponding Revents member (each descriptor has two variables, one is the call value and the other is the return result, thus avoiding the value-result parameter) Each of the two members is composed of one or more bits that specify a particular condition. The subscript lists some of the constant values for the specified events flag and for the test revents flag.

----------------------------+---------------------------------------+---------------------------------------+-- --------------------------------------+

Constants  | what can I do as an input to events?     | can it be as a result of revents? | Description |

----------------------------+---------------------------------------+---------------------------------------+-- --------------------------------------+

Pollin |       can |  can | Normal or priority with data-readable |

Pollrdnorm |       can |  can | Plain Data Readable |

Pollrdband |       can |  can | Priority with data-readable |

Pollpri |       can |  can | High-priority Data readable |

----------------------------+---------------------------------------+---------------------------------------+-- --------------------------------------+

Pollout |       can | can | Plain data can be written |

Pollwrnorm |       can | can | Plain data can be written |

Pollwrband |       can | can | Priority band data Write-up |

----------------------------+---------------------------------------+--------------------------------------+--- --------------------------------------+

Pollerr |       | can | Error occurred |

Pollhup |       | can | Occurrence Hangs |

Pollnval |         | can | The description word is not an open file |

-----------------------------+--------------------------------------+---------------------------------------+-- --------------------------------------+

The second parameter Nfds the number of elements in the array. The third parameter specifies how long to wait before the poll function returns. Inftim means to wait forever, 0 to return immediately, > 0 to wait for a specified number of seconds.

#include <sys/socket.h>#include<netinet/inch.h>#include<stdio.h>#include<error.h>#include<errno.h>#include<unistd.h>#include<string.h>#include<stdlib.h>#include<sys/wait.h>#include<limits.h>#include<poll.h>#include<sys/stropts.h>#include<signal.h>#defineMAXLINE 5#defineOpen_max 1024#defineSA struct SOCKADDRintMain () {intlistenfd, CONNFD, SOCKFD, I, Maxi; intNready;    Socklen_t Clilen;    ssize_t N; CharBuf[maxline]; structPOLLFD Client[open_max]; structsockaddr_in servaddr, cliaddr; //creating a listening socket    if(LISTENFD = socket (af_inet, Sock_stream,0)) <0) {printf ("socket () error!"); Exit (0); }    //first, we need to clear the Protocol address 0Bzero (&AMP;SERVADDR,sizeof(SERVADDR)); //set to IPv4 or IPv6servaddr.sin_family =af_inet; //bind to the port numberServaddr.sin_port = htons (9805); //any IP address, let the kernel choose itselfSERVADDR.SIN_ADDR.S_ADDR =htonl (Inaddr_any); //bind socket interface to local protocol address    if(Bind (LISTENFD, (SA *) &servaddr,sizeof(SERVADDR)) <0) {printf ("bind () error!"); Exit (0); }    //Server starts listening    if(Listen (LISTENFD,5) <0) {printf ("Listen () error!"); Exit (0); } client[0].FD =LISTENFD; client[0].events = Pollrdnorm;//concerned about the reading event of the listening set machine word     for(i =1; i < Open_max; ++i) {client[i].fd= -1; } Maxi=0;  for(;;) {Nready= Poll (client, Maxi +1, -1); if(client[0].revents &pollrdnorm) {Clilen=sizeof(CLIADDR); //accept the following two parameters are values-the result parameter, they keep the remote connection computer information, if regardless of the new remote connection computer information, you can set these two parameters to NULLCONNFD = Accept (LISTENFD, (SA *) &cliaddr, &Clilen); if(CONNFD <0)            {                Continue; }             for(i =1; i < Open_max; ++i) {if(Client[i].fd <0) CLIENT[I].FD=CONNFD;  Break; }            if(i = =Open_max) {printf ("Too many clients"); Exit (0); } client[i].events=Pollrdnorm; if(I >Maxi) {Maxi=i; }            if(--nready <=0 )                Continue; }         for(i =1; i < Open_max; ++i) {if((SOCKFD = CLIENT[I].FD) <0)            {                Continue; }            if(Client[i].revents & Pollrdnorm |Pollerr) {                if((n = Read (SOCKFD, buf, MAXLINE)) <0)                {                    if(errno = =Econnreset)                        {Close (SOCKFD); CLIENT[I].FD= -1; }                    Else{printf ("Read error!\n"); }                }                Else if(n = =0) {close (SOCKFD); CLIENT[I].FD= -1; }                Else{Write (SOCKFD, buf, N); }                if(--nready <=0)                     Break; }        }    }}

The poll of Linux/unix network programming

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.