Is it wrong to wait for EPOLLIN, read all data from the socket, and then immediately send the response?

Is it better to wait for EPOLLOUT before sending the response? If so - why? If not - what exactly is the purpose of EPOLLOUT?

I've seen some epoll examples that wait for EPOLLOUT and some that don't.

有帮助吗?

解决方案

If you wait for EPOLLOUT, you are guaranteed that the next send will not block. That means it will accept at least 1 byte (this is admittedly a quite poor guaranteee, but unluckily it's just that, you're never guaranteed that send accepts more than at least 1 byte).

You can do perfectly well without waiting for EPOLLOUT if either blocking is no issue or if the socket is nonblocking (in which case send would fail with EWOULDBLOCK). It sure results in much less complicated code.
It's not wrong to do either.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top