Search code examples
socketsnetwork-programmingtcpshutdown

The effect close/shutdown has on the TCP receive buffer


Suppose I have a TCP client and a TCP server. First, the client sends some data to the server, but the server is busy doing something else, so it does not read this connection. The data is kept in the TCP receive buffer on the server host. At this point, please help me clarify what will happen in the following different scenarios:

  1. Before the server call read, the client calls close on this connection. And the FIN has arrived in the server TCP, and the server TCP has sent ACK. At this point, if the server calls read on this socket, what will it return? Does it return the unread data in TCP receive buffer? Or does it just return 0 to indicate EOF, and the unread data is discarded?
  2. Before the server calls read, the client calls shutdown with SHUT_WR on this connection. And the FIN has arrived in the server TCP, and the server TCP has sent ACK. At this point, if the server calls read on this socket, what will it return? Does it return the unread data in TCP receive buffer? Or does it just return 0 to indicate EOF, and the unread data is discarded?
  3. Before the server calls read, the client just exits without calling either close or shutdown. And the TCP in client host will send the FIN, this FIN has arrived in the server TCP, and the server TCP has sent ACK. At this point, if the server calls read on this socket, what will it return? Does it return the unread data in TCP receive buffer? Or does it just return 0 to indicate EOF, and the unread data is discarded?

Solution

  • Before server call read, client calls close on this connection. And the FIN has arrived in server TCP, and server TCP has sent ACK. So at this point, if server calls read on this socket, what will read return? Does it return the unread data in TCP receive buffer?

    Yes.

    Or does it just return 0 to indicate EOF, and the unread data is discarded?

    No.

    Before server call read, client calls shutdown with SHUT_WR on this connection. And the FIN has arrived in server TCP, and server TCP has sent ACK. So at this point, if server calls read on this socket, what will read return? Does it return the unread data in TCP receive buffer? Or does it just return 0 to indicate EOF, and the unread data is discarded?

    Same as above.

    Before server call read, client just exit without calling either close or shutdown. And TCP in client host will send the FIN, this FIN has arrived in server TCP, and server TCP has sent ACK. So at this point, if server calls read on this socket, what will read return? Does it return the unread data in TCP receive buffer? Or does it just return 0 to indicate EOF, and the unread data is discarded?

    Same as above.

    The only way the receive buffer can get cleared is by reading all the data, or if the server receives a reset, i.e. an RST, which can be caused in various ways, or of course if the server closes the socket without reading all the data, in which case it issues an RST if there was pending read data.