If tcp connection was closed by peer, what do recv return in the case if fin packet was…
- User
If tcp connection was closed by peer, what do recv return in the case if fin packet was received on freebsd?
- Assistant
In TCP, when a connection is being closed, one side sends a FIN (Finish) packet to signal that it has finished sending data, but is still willing to receive data from the other side. If the peer closes the connection and your side receives a FIN packet, the behavior of the
recvcall will depend on whether there is any remaining data in the receive buffer to be read.Here's what happens with
recvon FreeBSD (and most Unix-like systems) when a FIN packet is received:-
If there is still data in the receive buffer (data that was sent before the FIN packet),
recvwill return that data as per usual. You can continue callingrecvto read the remaining data until the buffer is empty. -
Once the receive buffer is empty, a subsequent call to
recvwill return0, indicating a graceful close. The return of0is an EOF (End-of-File) indication, which means that the peer has finished sending data and has closed the connection. -
If
recvis called after the connection has been fully closed (both sides have sent and acknowledged FIN packets) and there is no data left to read, the behavior depends on howrecvwas called (blocking or non-blocking), but typically it will either result in an error witherrnoset toENOTCONN(Socket is not connected) or continue to return0.
It's important to handle the return value of
recvproperly within your code:- A positive return value indicates that you've read that number of bytes successfully.
- A return value of
0indicates an orderly shutdown of the connection by the peer. - A negative return value typically indicates an error, and you should check
errnofor the specific error code.
-
- User
Freebsd man says that recv never returns 0.
- Assistant
I apologize for the confusion. It appears there may be some misunderstanding or outdated information. Typically, in UNIX-like systems, including FreeBSD, for TCP socket programming, the behavior of the
recvfunction on a connected socket should be as follows:-
recvreturns a positive value indicating the number of bytes read, if data was available and was successfully read from the connection. -
recvreturns0if the connection has been gracefully closed by the peer, indicating the receipt of a FIN packet and that there is no more data to be received. -
recvreturns-1in the case of an error, anderrnois set to indicate the error.
It's quite standard across various operating systems, including FreeBSD, that a return value of
0fromrecvindicates the remote side has closed the connection.Please refer to the official FreeBSD documentation or the man pages (
man 2 recv) on your specific version of FreeBSD for the most accurate and updated information. If there's still ambiguity or conflicting information, please provide the exact text that suggestsrecvnever returns0, and we can address it specifically. -