Linux Network Programming epoll Model

Source: Internet
Author: User
# Include <stdio. h> # include <sys/types. h> # include <sys/socket. h> # include <sys/epoll. h> # include <errno. h> # include <netinet/in. h> # include <unistd. h> # include <ARPA/inet. h> # include <stdlib. h> # include <iostream> using namespace STD; # define listenq 5 # define open_max 1024 # define serv_port 10088 # define max_line 1024 # define inftim-1 # define maxevents 1000 void echo_srv (INT clientfd) {// process user request data char line [max_l INE]; cout <"start to read data" <Endl; int n = read (clientfd, line, sizeof (line); If (n <0) {// # define econnreset 104/* Connection reset by peer */If (errno = econnreset) {close (clientfd); printf ("exceptional exit \ n ");} else {printf ("network exception"); exit (-1) ;}} else if (n = 0) {close (clientfd ); printf ("normal exit \ n");} else {line [N] = 0; printf ("received data: % s \ n", line); write (clientfd, line, n) ;}} int main (INT argc, char ** argv) {struct sockaddr_in C Liaddr, 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 (servaddr) <0) {Printf ("bind function execution failed"); return 1 ;}if (Listen (listenfd, listenq) <0) {printf ("Listen function execution failed "); return 1;} printf ("Listen function execution succeeded \ n"); // epoll part int epollfd; If (epollfd = epoll_create (1) =-1) {cerr <"create epoll_create falid" <Endl; return false;} epoll_event epollevent; epollevent. data. FD = listenfd; epollevent. events = epollin | epollet; If (epoll_ctl (epollfd, epoll_ctl_add, listenfd, & epolle Vent )! = 0) {cerr <"create epoll Err! "<Endl; return 1 ;}cout <" create epoll OK! "<Endl; epoll_event * pepollevents = new epoll_event [maxevents]; while (true) {int n = epoll_wait (epollfd, pepollevents, maxevents,-1 ); cout <"epoll wait conn" <Endl; For (INT I = 0; I <n; I ++) {epoll_event & E = pepollevents [I]; if (E. events & epollerr) | (E. events & epollhup) | (! (E. events & epollin) {cerr <"epoll error for client" <E. data. FD <Endl; close (E. data. FD);} else if (listenfd = E. data. FD) // process the user connection {socklen_t clilen = sizeof (cliaddr); int connfd = accept (listenfd, (struct sockaddr *) & cliaddr, & clilen ); if (connfd <0) {If (errno! = Eagain) & (errno! = Ewouldblock) {cerr <"fail to accept new client" <Endl; Continue ;}printf ("IP: % s to this tour \ n ", inet_ntoa (cliaddr. sin_addr); // Add the user connection to epoll for monitoring e. data. FD = connfd; E. events = epollin | epollet; If (epoll_ctl (epollfd, epoll_ctl_add, connfd, & E )! = 0) {cerr <"create epoll Err! "<Endl; Continue ;}} else {echo_srv (E. Data. FD );}}}}

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.