I have a server that sends data to a client every 5 seconds. I want the client to block on read() until the server sends some data and then print it. I know read () is block
What Greg Hewgill already wrote as a comment: An EOF (that is, an explicit stop of writing, be it via close()
or via shutdown()
) will be communicated to the receiving side by having recv()
return 0. So if you get 0, you know that there won't be any data and you can terminate the reading loop.
If you had non-blocking enabled and there are no data, you will get -1
and errno
will be set to EAGAIN
or EWOULDBLOCK
.