I have two UDP connections and I'm trying to add them to use epoll()
. I am looking at this example:
https://programmer.ink/think/epoll-for-linux-programming.html
I've pasted the code below.
At the beginning they create an epoll event along with an array of epoll events:
struct epoll_event ev,events[20];
I'm not sure why both are needed.
They then call epoll_wait()
:
nfds=epoll_wait(epfd,events,20,500);
and the socket is retrieved:
if(events[i].data.fd==listenfd)
However, events
was not populated between these three lines.
So how does events[i].data.fd
contain the socket file descriptor?
Why do we set ev.data.fd=listenfd;
if we have an array of epoll_event
, which has a file descriptor data member?
struct epoll_event ev,events[20];
//Generate epoll-specific file descriptors for processing accept s
epfd=epoll_create(256);
struct sockaddr_in clientaddr;
struct sockaddr_in serveraddr;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
//Set socket to non-blocking
//setnonblocking(listenfd);
//Set the file descriptor associated with the event to be processed
ev.data.fd=listenfd;
//Set the type of event to process
ev.events=EPOLLIN|EPOLLET;
//ev.events=EPOLLIN;
//Register epoll events
epoll_ctl(epfd,EPOLL_CTL_ADD,listenfd,&ev);
bzero(&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
char *local_addr="127.0.0.1";
inet_aton(local_addr,&(serveraddr.sin_addr));//htons(portnumber);
serveraddr.sin_port=htons(portnumber);
bind(listenfd,(struct sockaddr *)&serveraddr, sizeof(serveraddr));
listen(listenfd, LISTENQ);
maxi = 0;
for ( ; ; ) {
//Waiting for the epoll event to occur
nfds=epoll_wait(epfd,events,20,500);
//Handle all events that occur
for(i=0;i<nfds;++i)
{
if(events[i].data.fd==listenfd)//If a new SOCKET user is detected to be connected to a bound SOCKET port, establish a new connection.
{
connfd = accept(listenfd,(struct sockaddr *)&clientaddr, &clilen);
if(connfd<0){
perror("connfd<0");
exit(1);
}
//setnonblocking(connfd);
char *str = inet_ntoa(clientaddr.sin_addr);
printf("accapt a connection from\n ");
//Setting file descriptors for read operations
ev.data.fd=connfd;
//Set Read Action Events for Annotation
ev.events=EPOLLIN|EPOLLET;
//ev.events=EPOLLIN;
//Register ev
epoll_ctl(epfd,EPOLL_CTL_ADD,connfd,&ev);
}
else if(events[i].events&EPOLLIN)//If the user is already connected and receives data, read in.
{
printf("EPOLLIN\n");
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
printf("readline error\n");
} else if (n == 0) {
close(sockfd);
events[i].data.fd = -1;
}
if(n<MAXLINE-2)
line[n] = '\0';
//Setting file descriptors for write operations
ev.data.fd=sockfd;
//Set Write Action Events for Annotation
ev.events=EPOLLOUT|EPOLLET;
//Modify the event to be handled on sockfd to EPOLLOUT
//epoll_ctl(epfd,EPOLL_CTL_MOD,sockfd,&ev);
}
else if(events[i].events&EPOLLOUT) // If there is data to send
{
sockfd = events[i].data.fd;
write(sockfd, line, n);
//Setting file descriptors for read operations
ev.data.fd=sockfd;
//Set Read Action Events for Annotation
ev.events=EPOLLIN|EPOLLET;
//Modify the event to be processed on sockfd to EPOLIN
epoll_ctl(epfd,EPOLL_CTL_MOD,sockfd,&ev);
}
}
}
return 0;
}
- So how does
events[i].data.fd
contain the socket file descriptor?
It's put in there by epoll_wait()
. It fills in the events
array with information about all the events that occurred.
- Why do we set
ev.data.fd=listenfd;
if we have an array ofepoll_event
, which has a file descriptor data member?
ev
is used to register events to wait for with epoll_ctl()
, the events
array contains the events that occurred.