Epoll in event triggering time
In the first case, an in event is triggered when in is set and the client sends data.
In the second case, set in, but the client sends 100 bytes of data. The server reads 20 bytes and does not trigger in until in is set again or new data arrives.
In the third case, set in. If the client is closed (close socket or the program is closed), The in event is triggered, and the Recv returns 0. If the event is not Del, And the out event is set, an out event is triggered. If you keep setting (querying) The in event, you will always receive the in event. For Network disconnection and power failure, Recv returns-1 and erron is etimeout. If keep-alive is not set, the in event is not triggered. If keep-alive is set, an in event is triggered, however, after receiving an in event, setting an out event does not trigger an out event.
In the third case, keep-alive is not set, and the client closes the socket or the program closes:
# Include <iostream> # include <sys/socket. h> # include <sys/epoll. h> # include <netinet/in. h> # include <ARPA/inet. h> # include <fcntl. h> # include <unistd. h> # include <stdio. h> # include <errno. h> # include <netinet/TCP. h> using namespace STD; # define maxline 5 # define open_max 100 # define listenq 20 # define serv_port 6002 # define inftim 1000int socket_set_keepalive (int fd) {int ret, error, flag, alive, idle, CNT, intv ;/* Set: Use keepalive on FD */alive = 1; if (setsockopt (FD, sol_socket, so_keepalive, & alive, sizeof alive )! = 0) {printf ("set keepalive error: % S. \ n ", strerror (errno); Return-1;}/* No data in 10 seconds, trigger the retention mechanism, send the retention package */idle = 10; if (setsockopt (FD, sol_tcp, tcp_keepidle, & idle, sizeof idle )! = 0) {printf ("set keepalive idle error: % S. \ n ", strerror (errno); Return-1;}/* If no response is received, resend the live packet in 5 seconds */intv = 5; if (setsockopt (FD, sol_tcp, tcp_keepintvl, & intv, sizeof intv )! = 0) {printf ("set keepalive intv error: % S. \ n ", strerror (errno); Return-1;}/* If the Active packet is not received three times in a row, the connection is regarded as invalid */CNT = 3; if (setsockopt (FD, sol_tcp, tcp_keepcnt, & CNT, sizeof CNT )! = 0) {printf ("set keepalive CNT error: % S. \ n ", strerror (errno); Return-1;} return 0;} void setnonblocking (INT sock) {int opts; opts = fcntl (sock, f_getfl ); if (OPTs <0) {perror ("fcntl (sock, getfl)"); exit (1) ;}opts = opts | o_nonblock; If (fcntl (sock, f_setfl, opts) <0) {perror ("fcntl (sock, setfl, opts)"); exit (1) ;}} int main () {int I, Maxi, listenfd, connfd, sockfd, epfd, NFDs; ssize_t N; char line [maxline]; socklen_t clilen; // declare the epoll_event struct variable. eV is used to register events, array is used to return the event struct epoll_event eV, events [20]; // generate a file descriptor epfd = epoll_create (256) for processing the accept epoll; struct sockaddr_in clientaddr; struct sockaddr_in serveraddr; listenfd = socket (af_inet, sock_stream, 0); int reuse = 1; setsockopt (listenfd, sol_socket, so_reuseaddr, & reuse, sizeof (reuse )); // set the socket to a non-blocking method // setnonblocking (listenfd); // set the file descriptor EV associated with the event to be processed. data. FD = listenfd; // set the event type EV to be processed. events = epollin | epollet; // eV. events = epollin; // register epoll event epoll_ctl (epfd, epoll_ctl_add, listenfd, & eV); bzero (& serveraddr, sizeof (serveraddr); serveraddr. sin_family = af_inet; char * local_addr = "0.0.0.0"; inet_aton (local_addr, & (serveraddr. sin_addr); // htons (serv_port); serveraddr. sin_port = htons (serv_port); BIND (listenfd, (sockaddr *) & serveraddr, sizeof (serveraddr); Listen (listenfd, listenq); Maxi = 0; (;;) {// wait for the occurrence of the epoll event NFDs = epoll_wait (epfd, events, 20,-1); // process all events that occur for (I = 0; I <NFDs; ++ I) {If (events [I]. data. FD = listenfd) {connfd = accept (listenfd, (sockaddr *) & clientaddr, & clilen); setnonblocking (connfd); If (connfd <0) {perror ("connfd <0"); exit (1) ;}setnonblocking (connfd); // socket_set_keepalive (connfd); char * STR = inet_ntoa (clientaddr. sin_addr); cout <"accapt a connection from" <STR <Endl; // sets the file descriptor EV used for read operations. data. FD = connfd; // set the read operation event eV for the test. events = epollout | epollet; // eV. events = epollin; // register EV epoll_ctl (epfd, epoll_ctl_add, connfd, & eV); char Buf [1024] = "bytes"; int I = 0; while (1) {If (send (connfd, Buf, strlen (BUF) + 1, 0) <0) {cout <"error -->" <strerror (errno) <Endl; break;} usleep (100000); cout <++ I <Endl;} // sets the read operation event used for the test. // eV. events = epollin | epollet; // eV. events = epollin; // register EV // epoll_ctl (epfd, epoll_ctl_mod, connfd, & eV);} else if (events [I]. events & epollin) {cout <"epollin" <Endl; // static int I = 0; // if (I = 0) // {sockfd = events [I]. data. FD; char Buf; int n = 0; while (n = read (sockfd, & Buf, 1)> 0) {printf ("% C \ n ", buf);} printf ("% d \ n", n); If (n = 0) {// eV. data. FD = sockfd; // epoll_ctl (epfd, epoll_ctl_del, sockfd, & eV) ;}else {cout <"error-222 --->" <strerror (errno) <Endl;} eV. data. FD = sockfd; eV. events = epollet | epollout; epoll_ctl (epfd, epoll_ctl_mod, sockfd, & eV);} else if (events [I]. events & epollout) {cout <"epollout" <Endl; // cout <"epollout end" <Endl; sockfd = events [I]. data. FD; // write (sockfd, line, n); // sets the file descriptor used for read Operations // sleep (5); eV. data. FD = sockfd; // set the read operation event eV for the injection test. events = epollin | epollet; // modify the event to be handled on sockfd to epolin epoll_ctl (epfd, epoll_ctl_mod, sockfd, & eV) ;}} return 0 ;}
The output is as follows:
Epollin
0
Epollout
Epollin
In the third case, set keep-alive to enable the client to power off or unplug the network cable:
# Include <iostream> # include <sys/socket. h> # include <sys/epoll. h> # include <netinet/in. h> # include <ARPA/inet. h> # include <fcntl. h> # include <unistd. h> # include <stdio. h> # include <errno. h> # include <netinet/TCP. h> using namespace STD; # define maxline 5 # define open_max 100 # define listenq 20 # define serv_port 6002 # define inftim 1000int socket_set_keepalive (int fd) {int ret, error, flag, alive, idle, CNT, intv ;/* Set: Use keepalive on FD */alive = 1; if (setsockopt (FD, sol_socket, so_keepalive, & alive, sizeof alive )! = 0) {printf ("set keepalive error: % S. \ n ", strerror (errno); Return-1;}/* No data in 10 seconds, trigger the retention mechanism, send the retention package */idle = 10; if (setsockopt (FD, sol_tcp, tcp_keepidle, & idle, sizeof idle )! = 0) {printf ("set keepalive idle error: % S. \ n ", strerror (errno); Return-1;}/* If no response is received, resend the live packet in 5 seconds */intv = 5; if (setsockopt (FD, sol_tcp, tcp_keepintvl, & intv, sizeof intv )! = 0) {printf ("set keepalive intv error: % S. \ n ", strerror (errno); Return-1;}/* If the Active packet is not received three times in a row, the connection is regarded as invalid */CNT = 3; if (setsockopt (FD, sol_tcp, tcp_keepcnt, & CNT, sizeof CNT )! = 0) {printf ("set keepalive CNT error: % S. \ n ", strerror (errno); Return-1;} return 0;} void setnonblocking (INT sock) {int opts; opts = fcntl (sock, f_getfl ); if (OPTs <0) {perror ("fcntl (sock, getfl)"); exit (1) ;}opts = opts | o_nonblock; If (fcntl (sock, f_setfl, opts) <0) {perror ("fcntl (sock, setfl, opts)"); exit (1) ;}} int main () {int I, Maxi, listenfd, connfd, sockfd, epfd, NFDs; ssize_t N; char line [maxline]; socklen_t clilen; // declare the epoll_event struct variable. eV is used to register events, array is used to return the event struct epoll_event eV, events [20]; // generate a file descriptor epfd = epoll_create (256) for processing the accept epoll; struct sockaddr_in clientaddr; struct sockaddr_in serveraddr; listenfd = socket (af_inet, sock_stream, 0); int reuse = 1; setsockopt (listenfd, sol_socket, so_reuseaddr, & reuse, sizeof (reuse )); // set the socket to a non-blocking method // setnonblocking (listenfd); // set the file descriptor EV associated with the event to be processed. data. FD = listenfd; // set the event type EV to be processed. events = epollin | epollet; // eV. events = epollin; // register epoll event epoll_ctl (epfd, epoll_ctl_add, listenfd, & eV); bzero (& serveraddr, sizeof (serveraddr); serveraddr. sin_family = af_inet; char * local_addr = "0.0.0.0"; inet_aton (local_addr, & (serveraddr. sin_addr); // htons (serv_port); serveraddr. sin_port = htons (serv_port); BIND (listenfd, (sockaddr *) & serveraddr, sizeof (serveraddr); Listen (listenfd, listenq); Maxi = 0; (;;) {// wait for the occurrence of the epoll event NFDs = epoll_wait (epfd, events, 20,-1); // process all events that occur for (I = 0; I <NFDs; ++ I) {If (events [I]. data. FD = listenfd) {connfd = accept (listenfd, (sockaddr *) & clientaddr, & clilen); setnonblocking (connfd); If (connfd <0) {perror ("connfd <0"); exit (1) ;}setnonblocking (connfd); socket_set_keepalive (connfd); char * STR = inet_ntoa (clientaddr. sin_addr); cout <"accapt a connection from" <STR <Endl; // sets the file descriptor EV used for read operations. data. FD = connfd; // set the read operation event eV for the test. events = epollout | epollet; // eV. events = epollin; // register EV epoll_ctl (epfd, epoll_ctl_add, connfd, & eV); char Buf [1024] = "bytes"; int I = 0; while (1) {If (send (connfd, Buf, strlen (BUF) + 1, 0) <0) {cout <"error -->" <strerror (errno) <Endl; break;} usleep (100000); cout <++ I <Endl;} // sets the read operation event used for the test. // eV. events = epollin | epollet; // eV. events = epollin; // register EV // epoll_ctl (epfd, epoll_ctl_mod, connfd, & eV);} else if (events [I]. events & epollin) {cout <"epollin" <Endl; // static int I = 0; // if (I = 0) // {sockfd = events [I]. data. FD; char Buf; int n = 0; while (n = read (sockfd, & Buf, 1)> 0) {printf ("% C \ n ", buf);} printf ("% d \ n", n); If (n = 0) {// eV. data. FD = sockfd; // epoll_ctl (epfd, epoll_ctl_del, sockfd, & eV) ;}else {cout <"error-222 --->" <strerror (errno) <Endl;} eV. data. FD = sockfd; eV. events = epollet | epollout; epoll_ctl (epfd, epoll_ctl_mod, sockfd, & eV);} else if (events [I]. events & epollout) {cout <"epollout" <Endl; // cout <"epollout end" <Endl; sockfd = events [I]. data. FD; // write (sockfd, line, n); // sets the file descriptor used for read Operations // sleep (5); eV. data. FD = sockfd; // set the read operation event eV for the injection test. events = epollin | epollet; // modify the event to be handled on sockfd to epolin epoll_ctl (epfd, epoll_ctl_mod, sockfd, & eV) ;}} return 0 ;}
Unplug the network cable or power off. The output is as follows:
Error --> resource temporarily unavailable
Epollout
Epollin
-1
Error-222 ---> connection timed out