Let’s suppose both client and server are in the Established state.
- Although the TCP connection termination can be initiated by either the client or the server, let’s assume here that the application process on the client side informs TCP that it no longer needs the connection. Client’s TCP protocol sends a FIN segment and enters the FIN-WAIT-1 state. At this point the local application no longer sends data, but it still can receive data.
- server’s TCP receives the FIN segment and informs the application process to terminate the connection. Server sends back an ACK and enters the CLOSE-WAIT state. When the client receives the ACK, it enters the FIN-WAIT-2 state.
- the application process at the server side finishes what it was doing and signals TCP that it is ready to close the connection. TCP sends a FIN segment to the client and enters the LAST-ACK state
- client receives the FIN segment from the server, sends ACK, enters TIME-WAIT state and starts a timer that equals two times the value of the Maximum Segment Life (MSL) time. This timer ensures that the server receives its ACK
- server receives the ACK and enters the CLOSED state
- client timer expires and enters the CLOSED state
The following diagram describes the TCP connection termination sequence:
The TCP connection termination process can also be described in a Finite State Machine (see this post for more information on Finite State Machines)?
TCP allows also for “Simultaneous close”, where both parties of the communication initiate a TCP connection close request.