I know that read() is a blocking call unless I make the socket non-blocking. So I expect read() call which requests 4K of data should return a positive value ( no of bytes read) or -1 on error ( possible connection reset by client etc). My question is: Can read() return '0' on any occasion?
I am handling the read() this way:
if ((readval = read(acceptfd, buf, sizeof(buf) - 1)) < 0) { } else { buf[readval] = 0; //Do some thing with data }
This code bombs if read() return zero and I know how to fix it. But is it possible for read() to return zero?
In the absence of errors, or if error detection is not performed, the read() function shall return zero and have no other results.
A returned value of zero indicates one of the following: The partner program has sent a NULL message (a datagram with no user data), A shutdown() to disable reading was previously done on the socket. The buffer length specified was zero.
It reads input from the user on the standard input stream, and then forwards that text to the echo server by writing the text to the socket. The server echoes the input back through the socket to the client. The client program reads and displays the data passed back to it from the server.
Upon successful completion, recv() returns the length of the message in bytes. If no messages are available to be received and the peer has performed an orderly shutdown, recv() returns 0. Otherwise, -1 is returned and errno is set to indicate the error.
When a TCP connection is closed on one side read() on the other side returns 0 byte.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With