fooll
fooll

Reputation: 39

In tcp connection termination

If it doesn't close in both directions, will it never close - regardless of expiration timers? - It can half-close but can the tcp connection terminate if only one initiates the close()?

In other words: In TCP connection termination - can you close the connection fully when only the client initiates a close but the server does not. Or can a tcp connection be closed by both ways independently?

Upvotes: 0

Views: 362

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123531

Each peer can close the TCP connection independent from the other and the peer will simply get an EOF (e.g no more bytes) when it tries to read from the peer or get an ECONNRESET or EPIPE if it tries to write to a connection which was closed by the peer, but only if the socket is aware that the peer does not want to receive more data, see below.

Closing a connection consists actually of two parts:

  • Application will not send any more data: shutdown(sock,SHUT_WR). In this case the kernel will send a FIN to the peer so signalize that no more data will follow. Reading from the peer will return EOF.
  • Application does not want to receive more data: shutdown(sock,SHUT_RD). In this case no information will be send to the peer initially, but if data get received from the peer they will be rejected with RST.

A call of close() is thus equivalent of shutting down both sides of the connection at the same time (SHUT_RDWR).

Upvotes: 1

Related Questions