To thoroughly understand poll or understand the following code, please refer to the "Linux network programming--i/o multiplexing poll function"
Code:
1#include <string.h>2#include <stdio.h>3#include <stdlib.h>4#include <unistd.h>5#include <sys/Select.h>6#include <sys/time.h>7#include <sys/socket.h>8#include <netinet/inch.h>9#include <arpa/inet.h>Ten#include <poll.h> One#include <errno.h> A #defineOpen_max 100 - - intMainintargcChar*argv[]) the { - //1. Create a TCP listener socket - intSOCKFD = socket (af_inet, Sock_stream,0); - + //2. Binding SOCKFD - structsockaddr_in my_addr; +Bzero (&MY_ADDR,sizeof(MY_ADDR)); Amy_addr.sin_family =af_inet; atMy_addr.sin_port = htons (8000); -MY_ADDR.SIN_ADDR.S_ADDR =htonl (Inaddr_any); -Bind (SOCKFD, (structSOCKADDR *) &my_addr,sizeof(MY_ADDR)); - - //3. Monitoring Listen -Listen (SOCKFD,Ten); in - //4.poll corresponding parameter preparation to structPOLLFD Client[open_max]; + inti =0, Maxi =0; - for(;i<open_max; i++) theCLIENT[I].FD =-1;//Initialize the file descriptor in the poll structure FD * $client[0].FD = SOCKFD;//descriptors that need to be monitoredPanax Notoginsengclient[0].events = Pollin;//Normal or priority with data- readable - the //5. Data processing for connected clients + while(1) A { the intRET = poll (client, maxi+1, -1);//monitoring of all elements added to the poll structure array + - //5.1 monitor SOCKFD (listening sockets) for connectivity $ if((client[0].revents & pollin) = =Pollin) $ { - structsockaddr_in cli_addr; - intClilen =sizeof(CLI_ADDR); the intCONNFD =0; - //5.1.1 Extracting the client from the TCP completion connectionWuyiCONNFD = Accept (SOCKFD, (structSOCKADDR *) &cli_addr, &Clilen); the - //5.1.2 The extracted connfd into an array of poll structures for easy poll function monitoring Wu for(i=1; i<open_max; i++) - { About if(Client[i].fd <0) $ { -CLIENT[I].FD =CONNFD; -Client[i].events =Pollin; - Break; A } + } the - //5.1.3 Maxi Update $ if(I >Maxi) theMaxi =i; the the //5.1.4 If there is no ready descriptor, continue to poll monitoring, otherwise continue to look down the if(--ret <=0) - Continue; in } the the //5.2 Continue response-Ready descriptor About for(i=1; i<=maxi; i++) the { the if(Client[i].fd <0) the Continue; + - if(Client[i].revents & (Pollin |Pollerr)) the { Bayi intLen =0; the Charbuf[ -] =""; the - //5.2.1 Accepting client data - if(len = recv (CLIENT[I].FD, buf,sizeof(BUF),0)) <0) the { the if(errno = = Econnreset)//TCP connection Timeout, RST the { the Close (CLIENT[I].FD); -CLIENT[I].FD =-1; the } the Else thePerror ("Read error:"); 94 the } the Else if(len = =0)//Client closes the connection the { 98 Close (CLIENT[I].FD); AboutCLIENT[I].FD =-1; - } 101 Else//data that is normally received from the server102Send (CLIENT[I].FD, buf, Len,0); 103 104 //5.2.2 All the ready descriptors have been processed, exit the current for loop and continue poll monitoring the if(--ret <=0) 106 Break; 107 108 } 109 } the } 111 return 0; the}
Operation Result:
Linux Network Programming--tcp Concurrent Server (poll implementation)