Vivek Gupta
Vivek Gupta

Reputation: 213

How does a TCP endpoint know whether the other endpoint has closed both halves of the connection or only one?

Consider a TCP connection established between two TCP endpoints, where one of them calls either:

  1. close(): Here, no further read or write is permitted.

  2. shutdown(fd, SHUT_WR): This converts the full duplex connection to simplex, where the endpoint invoking SHUT_WR can still read.

However, in both the cases a FIN packet is sent on the wire to the peer endpoint. So the question is, how can the TCP endpoint which receives the FIN distinguish whether the other endpoint has used close() or SHUT_WR, since in the latter scenario it should still be able to send data?

Upvotes: 4

Views: 1018

Answers (1)

Ilmari Karonen
Ilmari Karonen

Reputation: 50358

Basically, the answer is, it doesn't. Or, rather, the only general way to find out is to try to send some data and see if you get an ACK or an RST in response.

Of course, the application protocol might provide some mechanism for one side of the connection to indicate in advance that it no longer wants to receive any more data. But TCP itself doesn't provide any such mechanism.

Upvotes: 2

Related Questions