Saturday, April 20, 2013

How a TCP Connection is closed?


Closing a TCP connection is a four step process as opposed to three way process for connection establishment. The extra step is due to the full duplex nature of the TCP connection where both client and server may be sending data at any given time.

  1. TCP connection closing is initiated either by client or server, by sending a TCP segment with FIN flag set to the other end indicating that it is wishing to close the connection. So if client sends a TCP segment with FIN flag set to the server, then server is said to be on the CLOSE_WAIT state and client is said to be on  FIN_WAIT_1 state.
  2. After the FIN is received by the server, the server sends a TCP segment with ACK flag set to the client, incrementing the sequence number by one. Now the client goes into FIN_WAIT_2 state. The server also indicates to it's own higher layer protocols that the connection is terminated.
  3. The server closes the connection by sending a TCP segemnt with FIN flag set to the client. This causes the server to go into LAST_ACK state while the client goes into TIME_WAIT state.
  4. Finally, the client acknowledges the FIN sent by the server with an ACK and increments the sequence number by one. This causes the connection to go into CLOSED state.

Because TCP connections can be closed by either side, a TCP connection can exist in half-closed mode in which one end has initiated the FIN sequence but the other end has not done so.

RST - TCP connections can also be terminated with one end sending a TCP segment with RST(reset) flag set. This informs the other side to use an abortive release method. This is opposed to the normal termination of TCP connection sometimes referred to as orderly release.

No comments:

Post a Comment