I have simple TCP server that runs with non-blocking sockets.
Quote from manpage of recv;
When a stream socket peer has performed an orderly shutdown, the return value will be 0 (the traditional "end-of-file" return).
The value 0 may also be returned if the requested number of bytes to receive from a stream socket was 0.
When socket is readable I read it with this code:
uint8_t buf[2048];
ssize_t rlen;
while(1){
rlen = recv(fd, buf, sizeof(buf), 0);
if(rlen < 0){
/* some error came, let's close socket... */
}
else if(rlen == 0){
/* is there no bytes to read? do we need break; in here? */
/* is socket closed by peer? do we need to close socket? */
}
/* some code that process buf and rlen... */
}
How we can know which case happens when recv
returns 0?