When reading from a socket using read(2)
and blocking I/O, when do I know that the other side (the client) has no more data to send? (by "no more data to send" I mean that, as an example, the client is waiting for a response). At first, I thought that this point is reached when less than count
bytes are returned by read
(as in read(fd, *buf, count)
).
But what if the client sends the data fragmented? Reading until read
returns 0
would be a solution, but as far as I know 0
is only returned when the client closes the connection - otherwise, read
would just block until the connection is closed. I thought of using non-blocking I/O and a timeout for select(2)
, but this does not seem to be a tidy solution to me.
Are there any known best practices?