The book UNIX Network Programming comes with many small and exquisite programs. When I read this book, rewrite the code in the book according to your own understanding (most of them are on the copybook) to deepen your understanding (it's too difficult to read a book, huh, huh ). This example is successfully tested on ubuntu10.04.
PS: The program uses the declaration of the wrap function (the first character is an upper-case function) and constant (all letters are uppercase constants). In the my_unp.h file, it is defined in unp_base.c and unp_thread.c, address: http://blog.csdn.net/aaa20090987/article/details/8096701
Program Introduction: This is a server model that uses epoll functions for Io reuse. It is currently the most efficient and popular Io multiplexing transmission model on UNIX and Linux platforms.
Code:
# Include "my_unp.h" int main (void) {int listenfd, connfd, sockfd, epfd; int I, Maxi, NFDs; ssize_t N; char Buf [maxline]; socklen_t clilen; struct sockaddr_in cliaddr; struct sockaddr_in servaddr; // declare the variables of the epoll_event struct. eV is used to register events, and an array is used to return the events to be processed. struct epoll_event eV, events [256]; // create an epoll handle. size is used to tell the kernel the total number of the listeners. epfd = epoll_create (256); // create a socket for TCP protocol listenfd = socket (af_inet, sock_stream, 0 ); Memset (& servaddr, 0, sizeof (servaddr); servaddr. sin_family = af_inet; servaddr. sin_addr.s_addr = htonl (inaddr_any); servaddr. sin_port = htons (serv_port); // link the socket and socket address structures to bind (listenfd, (Sa *) & servaddr, sizeof (servaddr )); // start listening to the listenq port listen (listenfd, listenq); // set the file descriptor and event EV related to the event to be processed. data. FD = listenfd; eV. events = epollin | epollet; // register the epoll event epoll_ctl (epfd, epoll_ctl_add, listenfd, & EV); Maxi = 0; while (1) {// wait for the occurrence of the epoll event // return the number of events to be processed NFDs. If 0 is returned, the Operation has timed out. NFDs = epoll_wait (epfd, events, 20,500); // process all events that occur for (I = 0; I <NFDs; ++ I) {// if a new socket user is detected to connect to the bound socket port, create a new connection. If (events [I]. data. FD = listenfd) {connfd = accept (listenfd, (Sa *) & cliaddr, & clilen); printf ("connection from % s, port % d. \ n ", inet_ntop (af_inet, (void *) & cliaddr. sin_addr, Buf, sizeof (BUF), ntohs (cliaddr. sin_port); // sets the file descriptor and event eV for read operations. data. FD = connfd; eV. events = epollin | epollet; // register the epoll_ctl event (epfd, epoll_ctl_add, connfd, & eV) ;}// read the connected user and receive the data. Else if (events [I]. events & epollin) {sockfd = events [I]. data. FD; If (sockfd <0) continue; n = read (sockfd, Buf, maxline); If (n <0) {// Connection Reset: the end of your connection has been disconnected, // but you are still trying to read and write data on the socketfd that has been disconnected from the other end! If (errno = econnreset) {close (sockfd); events [I]. data. FD =-1;} else error_quit ("read error");} // If the read data is empty, else if (n = 0) {close (sockfd ); events [I]. data. FD =-1;} else {// sets the file descriptor and event eV for write operations. data. FD = sockfd; eV. events = epollout | epollet; // register the epoll_ctl event (epfd, epoll_ctl_mod, sockfd, & eV) ;}// if there is data, send else if (events [I]. events & epollout) {sockfd = events [I]. data. FD; writen (sockfd, Buf, n); // sets the file descriptor and event eV for read operations. data. FD = sockfd; eV. events = epollin | epollet; // register the epoll_ctl event (epfd, epoll_ctl_mod, sockfd, & eV) ;}} return 0 ;}
Its Supporting client is here: http://blog.csdn.net/aaa20090987/article/details/8462262