Linux under Poll programming

Source: Internet
Author: User

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 a pointer to the first element of the structure array, each of which is a POLLFD structure iouyonghu the condition that the amount is a given descriptor.

struct POLLFD

{

int FD;

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

Short REVENTS;//FD actually happened on the event

}

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.

Poll programming instance, this is the TCP connection server-side code:

1#include <sys/socket.h>2#include <netinet/inch.h>3#include <stdio.h>4#include <error.h>5#include <errno.h>6#include <unistd.h>7#include <string.h>8#include <stdlib.h>9#include <sys/wait.h>Ten#include <limits.h> One#include <poll.h> A#include <sys/stropts.h> -#include <signal.h> - #defineMAXLINE 5 the #defineOpen_max 1024 - #defineSA struct SOCKADDR -  -  + intMain () - { +     intlistenfd, CONNFD, SOCKFD, I, Maxi; A     intNready; at socklen_t Clilen; - ssize_t N; -     CharBuf[maxline]; -     structPOLLFD Client[open_max]; -     structsockaddr_in servaddr, cliaddr; -     //creating a listening socket in     if(LISTENFD = socket (af_inet, Sock_stream,0)) <0) -     { toprintf"socket () error!"); +Exit0); -     } the     //first, we need to clear the Protocol address 0 *Bzero (&AMP;SERVADDR,sizeof(SERVADDR)); $     //set to IPv4 or IPv6Panax Notoginsengservaddr.sin_family =af_inet; -     //bind to the port number theServaddr.sin_port = htons (9805); +     //any IP address, let the kernel choose itself ASERVADDR.SIN_ADDR.S_ADDR =htonl (inaddr_any); the     //bind socket interface to local protocol address +     if(Bind (LISTENFD, (SA *) &servaddr,sizeof(SERVADDR)) <0) -     { $printf"bind () error!"); $Exit0); -     } -     //Server starts listening the     if(Listen (LISTENFD,5) <0) -     {Wuyiprintf"Listen () error!"); theExit0); -     } Wuclient[0].FD =LISTENFD; -client[0].events = Pollrdnorm;//concerned about the reading event of the listening set machine word About      for(i =1; i < Open_max; ++i) $     { -CLIENT[I].FD =-1; -     } -Maxi =0; A      for(;;) +     { theNready = Poll (client, Maxi +1, -1); -         if(client[0].revents &pollrdnorm) $         { theClilen =sizeof(CLIADDR); the             //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 NULL theCONNFD = Accept (LISTENFD, (SA *) &cliaddr, &Clilen); the             if(CONNFD <0) -             { in                 Continue; the             } the              for(i =1; i < Open_max; ++i) About             { the                 if(Client[i].fd <0) theCLIENT[I].FD =CONNFD; the                  Break; +             } -             if(i = =Open_max) the             {Bayiprintf"Too many clients"); theExit0); the             } -Client[i].events =Pollrdnorm; -             if(I >Maxi) the             { theMaxi =i; the             } the             if(--nready <=0 ) -                 Continue; the         } the          for(i =1; i < Open_max; ++i) the         {94             if((SOCKFD = CLIENT[I].FD) <0) the             { the                 Continue; the             }98             if(Client[i].revents & Pollrdnorm |Pollerr) About             { -                 if((n = Read (SOCKFD, buf, MAXLINE)) <0)101                 {102                     if(errno = =econnreset)103                     {104 Close (SOCKFD); theCLIENT[I].FD =-1;106                     }107                     Else108                     {109printf"Read error!\n"); the                     }111                 } the                 Else if(n = =0)113                 { the Close (SOCKFD); theCLIENT[I].FD =-1; the                 }117                 Else118                 {119 Write (SOCKFD, buf, n); -                 }121                 if(--nready <=0)122                      Break;123             }124         } the     }126}

With Linux under Select Programming is a complete client/server-side code, the results of the operation are as follows:

Client:

  

Server-side:

    

    

Linux under Poll 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.