Linux Network Programming poll Model

Source: Internet
Author: User
// Distribution process // listen # include <stdio. h> # include <sys/types. h> # include <sys/socket. h> # include <sys/poll. h> # include <errno. h> # include <netinet/in. h> # include <unistd. h> # include <ARPA/inet. h> # include <stdlib. h> # define listenq 5 # define open_max 1024 # define serv_port 10088 # define max_line 1024 # define inftim-1 int main (INT argc, char ** argv) {int err, maxpoll, nreadly; struct pollfd client [open_max]; struct sock Addr_in cliaddr, servaddr; int listenfd = socket (af_inet, sock_stream, 0); If (listenfd <0) {printf ("socket function execution failed"); return 1;} servaddr. sin_family = af_inet; servaddr. sin_addr.s_addr = htonl (inaddr_any); // inet_aton ('10. 132.10.64', & (servaddr. sin_addr); // servaddr. sin_addr.s_addr = inet_addr ("10.132.10.64"); servaddr. sin_port = htons (serv_port); If (BIND (listenfd, (struct sockaddr *) & servaddr, sizeof (serva DDR) <0) {printf ("bind function execution failed"); return 1;} If (Listen (listenfd, listenq) <0) {printf ("Listen function execution failed"); return 1;} client [0]. FD = listenfd; client [0]. events = pollrdnorm; For (INT I = 1; I <open_max; I ++) {client [I]. FD =-1;} maxpoll = 0; printf ("the listen function is successfully executed \ n"); While (true) {socklen_t clilen = sizeof (cliaddr ); nreadly = poll (client, maxpoll + 1, 1000); If (Client [0]. revents & pollrdnorm) {int idx =-1; for (I NT I = 1; I <open_max; I ++) {If (Client [I]. FD <0) {idx = I; break;} If (idx =-1) {// discard connection continue;} int connfd = accept (listenfd, (struct sockaddr *) & cliaddr, & clilen); If (connfd <0) {printf ("Accept function execution failed"); break;} printf ("IP: % s to this game \ n ", inet_ntoa (cliaddr. sin_addr); client [idx]. FD = connfd; client [idx]. events = pollrdnorm; maxpoll = (idx> maxpoll? Idx: maxpoll); If (-- nreadly <= 0) {continue ;}}for (INT I = 1; I <open_max; I ++) {int sockfd = client [I]. FD; If (sockfd <0) {continue;} If (Client [I]. revents & (pollrdnorm | pollerr) {char line [max_line]; int n = read (sockfd, line, sizeof (line); If (n <0) {If (errno = econnreset) {close (sockfd); client [I]. FD =-1; printf ("abnormal exit \ n");} else {printf ("network exception"); exit (-1 );}} else if (n = 0) {close (sockfd); client [I]. FD =-1; printf ("normal exit \ n");} else {line [N] = 0; printf ("received data: % s \ n ", line); write (sockfd, line, n);} If (-- nreadly <= 0) {continue ;}}}}}

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.