Search code examples
csocketsnonblockingmultiplexing

Multiplexing non-blocking socket IOs with EOF


I am implementing a non-blocking socket IO reactor using linux select. Let's say, a server and a client are in communication. If the client or the server is down, the other side is supposed to receive an EOF which can be told by the return value of read call (C function call).

if(read(fd, ...) == 0) { printf("Endpoint connection failed\n"); }

My question is, will this EOF event be overlapped or merged with other data read? For example, the client sends 1 byte to the server and accidentally shutdowns immediately. If they happen very close by time, is the read on server still separable to 1 byte and EOF?


Solution

  • If the other end sends one byte and then closes the connection, then you will first read one byte, and then the next call to read will return 0.

    There's no way for a single read call to do both - because it has to return 0 to indicate closure, and non-0 if it read some data.