Return mechanism about epoll_wait

72 Views Asked by At

I've seen all epoll exmaples using epoll_wait like

int i, n;
...
n = epoll_wait(epfd, events, MAX_EVENTS, -1);
for (i = 0; i < n; ++i) {
    do_something_with(events[i]);
}

I change it to

int i;
...
for (i = 0; i < epoll_wait(epfd, events, MAX_EVENTS, -1); ++i) {
    do_something_with(events[i]);
}

When a socket connect to this program and send something, epoll_wait would return only once, with the connecting action, but unable to read after that (previous version, twice, connect and read). What's the magic with it?

The complete C code is at http://pastebin.com/bx4hbhdM

Python client is

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 4999))
s.sendall('hello')
s.close()
1

There are 1 best solutions below

0
On BEST ANSWER

The changed version calls epoll_wait for every loop iteration so you are blocking again after processing only one event. You have to call epoll_wait once up front and then do the loop.