Linux I/O multiplexing--poll ()

Source: Internet
Author: User

1, poll ()

The poll () system call is similar to select () and also polls a certain number of file descriptors to test if they have a ready person.

API functions:

int poll (struct POLLFD *fds, nfds_t nfds, int timeout);

Parameters: nfds+1;

struct POLLFD {
int FD; /* File Descriptor */
Short events; /* Requested Events */
Short revents; /* Returned events */
};

where the FD member is the specified file descriptor, the events member tells poll which events to listen on, and Revents is modified by the kernel.

There are some macros as auxiliary event parameters;

2. Code implementation

(1), utili.h

#include <unistd.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys /socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <assert.h> #include <sys/ select.h> #define SERVER_IP "127.0.0.1" #define Server_port 8787#define listen_queue 5#define SIZE 10#define buffer_ SIZE 256#include<poll.h> #define OPEN_MAX 1000

(2), ser.c

#include ". /utili.h "Static int socket_bind (const char *ip, int port) {     int listenfd = socket (af_inet, sock_stream, 0);     struct  sockaddr_in addrSer;    addrSer.sin_family = AF_INET;   &NBSP;&NBSP;//ADDRSER.SIN_ADDR.S_ADDR&NBSP;=&NBSP;INET_ADDR (IP);     inet_pton (AF_INET, &NBSP;IP,&NBSP;&AMP;ADDRSER.SIN_ADDR);     addrser.sin_port = htons (port);     bind (listenfd,  (struct sockaddr*) &addrser, sizeof (STRUCT&NBSP;SOCKADDR)); &NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;LISTENFD;} Static void handle_connection (Struct pollfd *connfds, int num) {     char buffer[buffer_size];    int i;    for (I=1;  i<=num; ++i) {        if (CONNFDS[I].FD&NBSP;==&NBSP;-1)              continue ;         if (Connfds[i].revents & pollin) {   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RECV (Connfds[i].fd, buffer, buffer_size,  0);              printf ("Server  Accept client msg:>%s\n ", buffer);             send (Connfds[i].fd, buffer, strlen (buffer) +1, 0);          }       }   }static void  Do_poll (INT&NBSP;LISTENFD) {    struct sockaddr_in addrcli;     int connfd;    struct pollfd clientfds[OPEN_MAX];     clientfds[0].fd = listenfd;    clientfds[0].events = pollin;    int i;     for (i=1; i<open_max; ++i) {         clientfds[i].fd = -1;    }    int nready;     int max = 0;    for (;;) {        nready = poll (clientfds, max+1, -1);         if (nready == -1) {             perror ("poll");             return;        }         if (nready == 0) {             printf ("server wait time out.\n");             continue;         }        if (Clientfds[0].revents & pollin) {             socklen_t len = sizeof ( STRUCT&NBSP;SOCKADDR);             connfd =  accept (listenfd,  (struct sockaddr*) &addrcli, &len);             int i;             for (i=1; i<open_max; ++i) {                 if (clientfds[i].fd == -1) {                     clientfds[i]. fd = connfd;                     break;                }             }             if (I == open_max) {                 printf ("over load.\n");                 exit (1);             }             clientfds[i].events = POLLIN;             max =  (I>max ? i : max);         }   &nbSp;    handle_connection (Clientfds, max);     }}int main (void ) {    int listenfd;    listenfd = socket_bind (SERVER_IP ,  server_port);     listen (listenfd, listen_queue);     do_ Poll (LISTENFD);     return 0;}

(3), CLI.C

#include ". /utili.h "Static void handle_connection (INT&NBSP;SOCKFD) {&NBSP;&NBSP;&NBSP;&NBSP;STRUCT&NBSP;POLLFD  pfds[2];    pfds[0].fd = sockfd;    pfds[0].events  = pollin;    pfds[1].fd = stdin_fileno;    pfds[1 ].events = pollin;    char buffer[buffer_size];    for ( ;;) {        poll (pfds, 2, -1);   //-1 means never timeout          if (Pfds[0].revents & pollin) {     &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RECV (sockfd, buffer, buffer_size, 0);              printf ("msg:> %s\n", buffer);         }            if (pfds[1].revents & pollin) {             scanf ("%s",  buffer);             //read (STDIN_ Fileno, buffer, buffer_size);             send (Sockfd, buffer, strlen (buffer) +1, 0);          }       }   }int main (void) {     int sockfd = socket (af_inet, sock_stream, 0);      struct sockaddr_in addrSer;    addrSer.sin_family = AF_INET;     addrser.sin_port = htons (server_port);     addrser.sin_ ADDR.S_ADDR&NBSP;=&NBSP;INET_ADDR (SERVER_IP);     connect (sockfd,  (struct  sockaddr*) &addrser, sizeof (STRUCT&NBSP;SOCKADDR));     handle_connection (SOCKFD);     return 0;} 

Run results

Server-side

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/88/19/wKiom1fpUfCi0wBWAABAoQ1HNE0226.png-wh_500x0-wm_3 -wmp_4-s_995272177.png "title=" Qq20160927005044.png "alt=" Wkiom1fpufci0wbwaabaoq1hne0226.png-wh_50 "/>

Client 1

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/88/16/wKioL1fpUjrD3uxiAAAll0njD24146.png-wh_500x0-wm_3 -wmp_4-s_4244803526.png "title=" Qq20160927005156.png "alt=" Wkiol1fpujrd3uxiaaall0njd24146.png-wh_50 "/>

Client 2

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/88/19/wKiom1fpUoCjyuP_AAAkjmdZIgc333.png-wh_500x0-wm_3 -wmp_4-s_991912699.png "title=" Qq20160927005303.png "alt=" Wkiom1fpuocjyup_aaakjmdzigc333.png-wh_50 "/>




This article is from the "11586096" blog, please be sure to keep this source http://11596096.blog.51cto.com/11586096/1856818

Linux I/O multiplexing--poll ()

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.