T
T
tmp_ilya12019-03-14 21:05:39
linux
tmp_ilya1, 2019-03-14 21:05:39

Why is the fd field in the epoll_event not set?

Hello!
After the epoll_wait method receives the EPOLLIN event, the epoll_event.data.fd structure field is set to 0, although the documentation says that there should be a client socket descriptor (by the way, the epoll_event.data.ptr field is filled in correctly).

int n = epoll_wait(epoll_fd, events, maxEvents, -1);
for (int i = 0; i < n; i++)
{
epoll_event e = events[i];
int s = e.data.fd;   <---- всегда 0
}

Code for incoming socket:
int cfd = accept4(sfd, null, null, SOCK_NONBLOCK);

epoll_event e;
e.events = EPOLLIN;
e.data.fd = cfd;
e.data.ptr = (void*)cfd;
epoll_ctl(efd, EPOLL_CTL_ADD, cfd, &e);

The situation is similar when connecting a new client, the documentation says that there should be a server socket descriptor, but there is 0.
Code for the server socket:
epoll_event se;
se.data.fd = sfd;
se.events = EPOLLIN;
epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sfd, &se);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2019-03-14
@tmp_ilya1

e.data.fd = cfd;
e.data.ptr = (void*)cfd;

epoll_data is a union, you don't need to initialize it first with one and then with another:
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 */
           };

the documentation says that there should be a value that was in the data field at the time the epoll_ctl was called.
Also, 0 is a perfectly normal file descriptor.
In short, I tried, it works for me. If it doesn't work for you, post the whole code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question