All functions used by EPOLL are declared in the header file sys / epoll.h, and the data structure and functions used below are briefly described below:
Data structure used
TypedEf Union Epoll_data {
Void * PTR;
Int fd;
__UINT32_T U32;
__UINT64_T U64;
} EPOLL_DATA_T;
Struct EPOLL_EVENT {
__UINT32_T Events; / * EPOLL Events * /
EPOLL_DATA_T DATA; / * USER DATA VARIABLE * /
}
Structural EPOLL_EVENT is used to register the incoming events and the event to be processed, where the EPOLL_DATA consort is used to save the data related to a file descriptor of the event, such as a client connected to the server, the server passes calls The Accept function can get the corresponding socket file descriptor corresponding to this client, which can describe the file descriptor to the FD field of EPOLL_DATA, so that the following read-write operations are performed on this file descriptor. The EPENTS field of the EPOLL_EVENT structure is an event that is interested, and the triggered event may be valued:
EPOLLIN: Indicates the corresponding file descriptor to read;
EPOLLOUT: Indicates the corresponding file descriptor to write;
EPOLLPRI: Indicates that the corresponding file descriptor has emergency data readable (here you should indicate that there is an outgoing data arrival);
EPOLLLERR: Indicates an error in the corresponding file descriptor;
EPOLLHUP: Indicates the corresponding file descriptor being hanging;
EPOLLET: Indicates that the corresponding file descriptor has an event;
The function used:
1, EPOLL_CREATE function
Function declaration:
INT EPOLL_CREATE (int size
)
This function generates an EPOLL-specific file descriptor, which is the maximum range of specified generating descriptors (I think this parameter and the first parameter of the select function should be similar, but how to set it, I am not too clear).
2, EPOLL_CTL function
Function declaration:
INT EPOLL_CTL (int EPFD
INT OP
, int FD
, Struct EPOLL_EVENT * EVENT
This function is used to control events on a file descriptor, can register events, modify events, and delete events.
Parameters: EPFD:
EPOLL_CREATE The generated EPOLL dedicated file descriptor;
OP: Operations To make, such as registration events, possible values
EPOLL_CTL_ADD registration,
EPOLL_CTL_MOD repair
change,
EPOLL_CTL_DEL Delete
FD: associated file descriptor;
Event: Pointer to EPOLL_EVENT;
If the call is successfully returned 0, it is not successful to return -1
3,
EPOLL_WAIT function function declaration: int EPOLL_WAIT (int EPFD, Struct Epoll_Event * Events, int maxevents, int Timeout)
This function is used to poll the occurrence of I / O events;
parameter:
EPFD:
EPOLL_CREATE The generated EPOLL dedicated file descriptor;
EPOLL_EVENT: An array used to return to generation;
MaxEvents: number of events that can be handled;
Timeout: Waiting for the timeout value of the I / O event to occur;
Returns the number of incidents.
example:
@ font-face
{Font-Family: Song;
Panose-1: 2 1 6 0 3 1 1 1 1 1;
@ font-face
{Font-Family: New Song;
Panose-1: 2 1 6 9 3 1 1 1 1 1;
@ font-face
{font-family: "/ @ 新 体";
Panose-1: 2 1 6 9 3 1 1 1 1 1;
@ font-face
{font-family: "/ @ 体";
Panose-1: 2 1 6 0 3 1 1 1 1 1;
/ * STYLE definitions * /
P.msonormal, li.msonormal, Div.msonormal
{margin: 0cm;
Margin-bottom: .0001pt;
Text-align: Justify;
Text-Justify: Inter-Ideograph;
FONT-SIZE: 10.5pt;
Font-Family: "Times New Roman";
/ * Page definitions * /
@Page Section1
{SIZE: 612.0pt 792.0pt;
Margin: 72.0pt 90.0pt 72.0pt 90.0pt;}
Div.section1
{Page: Section1;
->
#include
#include
#include
#include
#include
#include
#include
#include
#define maxline 10
#DEfine Open_MAX 100
#define Listenq 20
#define serv_port 5555
#define inFTim 1000
Void setnonblocking (int Sock)
{
Int OPTS;
OPTS = FCNTL (SOCK, F_GETFL);
IF (OPTS <0)
{
"" "FCNTL (SOCK, GETFL)");
Exit (1);
}
OPTS = OPTS | O_NONBLOCK;
IF (FCNTL (Sock, F_Setfl, OPTS) <0)
{
"" 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 variable of the EPOLL_EVENT structure, EV is used to register events, an array is used to return the event to be processed
Struct Epoll_Event EV, Events [20];
/ / Generate a file descriptor for handling an EPOLL of Accept
EPFD = EPOLL_CREATE (256);
Struct SockAddr_in ClientAddr;
Struct SockAddr_in ServerAddr;
Listenfd = Socket (AF_INET, SOCK_STREAM, 0);
// Set the socket to non-blocking mode
SetnonBlocking (Listenfd);
/ / Set the file descriptor related to the event to be processed
ev.Data.fd = listenfd;
/ / Set the type of event to be processed
ev. Events = EPOLLIN | EPOLLET; // Register EPOLL Events
EPOLL_CTL (EPFD, EPOLL_CTL_ADD, LISTENFD, & EV);
Bzero (& ServerAddr, SizeOf (ServerAddr));
ServerAddr.sin_Family = AF_INET;
CHAR * local_addr = "200.200.200.204";
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;
For (;;) {
// Waiting for the occurrence of EPOLL events
NFDS = EPOLL_WAIT (EPFD, Events, 20,500);
// Treat all events happened
For (i = 0; i { IF (Events [i] .data.fd == listenfd) { Connfd = Accept (Listenfd, (SockAddr *) & ClientAddr, & Clilen; IF (connfd <0) { PERROR ("Connfd <0"); Exit (1); } SetnonBlocking (connfd); Char * str = inet_ntoa (clientaddr.sin_addr); Std :: cout << "Connect from" <_U115? TR << std :: end1 // Set the file descriptor for read operations Ev.data.fd = connfd; // Set read operating events for the injection Ev.Events = EPOLLIN | EPOLLET; // Register EV EPOLL_CTL (EPFD, EPOLL_CTL_ADD, CONNFD, & EV); } Else IF (Events [i]. Events & EPOLLIN) { IF ((sockfd = events [i] .data.fd) <0) Continue; IF ((n = read (sockfd, line, maxline) <0) { IF (errno == ECONNRESET) { Close (SockFD); Events [i] .data.fd = -1; Else Std :: cout << "Readline Error" << std :: endl; } else if (n == 0) { Close (SockFD); Events [i] .data.fd = -1; } / / Set the file descriptor for write operations ev.Data.fd = SOCKFD; // Set a write operation event for the injection ev. Events = Epollout | EPOLLET; / / Modify the event to be processed on SOCKFD for ePollout EPOLL_CTL (EPFD, EPOLL_CTL_MOD, SOCKFD, & EV); } ELSE IF (Events [i]. Events & Epollout) { Sockfd = events [i] .data.fd; Write (Sockfd, Line, N); // Set the file descriptor for read operations ev.Data.fd = SOCKFD; // Set read operating events for the injection Ev.Events = EPOLLIN | EPOLLET; / / Modify the event to be processed on SOCKFD to ePolin EPOLL_CTL (EPFD, EPOLL_CTL_MOD, SOCKFD, & EV); } } } }