TCP Connection Lifecycle: Handshakes, Termination, and State Transitions
TCP Control Flags
TCP headers utilize specific control bits to manage session states. Three critical flags control the connection lifecycle:
- SYN (Synchronize): Initiates a connection and synchronizes initial sequence numbers.
- ACK (Acknowledgment): Confirms the receipt of data packets. Once a connection is established, this flag is always set.
- FIN (Finish): Terminates an established connection, signaling that the sender has finished sending data.
Connection Establishment: The Three-Way Handshake
TCP uses a three-step process to establish a reliable connection between a client and a server.
- Client Request (SYN): The client initiates the connection by sending a segment with the SYN flag enabled. This segment contains a randomly generated Initial Sequence Number (ISN), denoted as
seq=x. The client enters theSYN_SENTstate. - Server Response (SYN-ACK): The server receives the SYN and replies with a segment containing both SYN and ACK flags. The ACK number is set to
x+1to acknowledge the client's SYN. The server also generates its own random ISN,seq=y. The server moves to theSYN_RCVDstate. - Client Confirmation (ACK): The client receives the server's SYN-ACK and sends a final acknowledgment segment. The ACK flag is set, and the acknowledgment number is
y+1. The sequence number is incremented toseq=x+1. Both endpoints transition to theESTABLISHEDstate, completing the handshake.
Connection Termination: The Four-Way Handshake
Because TCP is a full-duplex protocol—meaning data flows independently in both directions—closing a connection requires four steps to ensure both sides finish transmitting.
- Active Close (FIN): The client (initiator) sends a segment with the FIN flag set (
FIN=1) and a sequence numberseq=x. The client enters theFIN_WAIT_1state. - Acknowledgment of Close (ACK): The server acknowledges the termination request by sending an ACK segment with
ack=x+1. The server transitions to theCLOSE_WAITstate. Upon receiving this ACK, the client moves toFIN_WAIT_2. The connection is now half-closed. - Server Initiation of Close (FIN): After sending any remaining data, the server sends its own FIN segment (
FIN=1) withseq=zto close its side of the connection. The server enters theLAST_ACKstate. - Final Acknowledgment (ACK): The client sends a final ACK with
ack=z+1and enters theTIME_WAITstate. The server receives this ACK and transitions toCLOSED. After waiting for a period defined as 2*MSL (Maximum Segment Lifetime), the client also closes.
TCP State Transition Overview
The lifecycle of a TCP socket moves through distinct states defined by the protocol standard.
- CLOSED: The initial state indicating no active connection.
- LISTEN: The server is waiting for a connection request from a client.
- SYN_SENT: The client has sent a connection request and is waiting for a matching ACK.
- SYN_RCVD: The server has received a SYN and sent an acknowledgment, waiting for the final ACK.
- ESTABLISHED: The connection is open, and data exchange is possible.
- FIN_WAIT_1: The active closer has sent a FIN and waits for an ACK.
- FIN_WAIT_2: The active closer has received an ACK for its FIN and waits for a FIN from the peer.
- CLOSE_WAIT: The passive closer has received a FIN and acknowledged it, waiting for the local application to close the socket.
- LAST_ACK: The passive closer has sent its FIN and waits for the final ACK.
- TIME_WAIT: The active closer waits to handle any delayed or retransmitted segments before fully closing.
- CLOSING: A rare state occurring when the active closer receives a FIN from the peer before receiving the ACK for its own FIN (simultaneous close).
State Transition Flow: Connection Establishment
Endpoint State Action Peer State
---------------------------------------------------------------
Server CLOSED socket/listen CLOSED
Server LISTEN <-- SYN -- CLOSED
Client SYN_SENT -- SYN(x) --> LISTEN
Server SYN_RCVD <-- SYN(y), ACK(x+1) -- SYN_SENT
Client ESTABLISHED -- ACK(y+1) --> SYN_RCVD
Server ESTABLISHED <-- ACK -- ESTABLISHED
State Transition Flow: Connection Termination
Endpoint State Action Peer State
---------------------------------------------------------------
Client ESTABLISHED active close ESTABLISHED
Client FIN_WAIT_1 -- FIN(x) --> ESTABLISHED
Server CLOSE_WAIT <-- ACK(x+1) -- FIN_WAIT_1
Client FIN_WAIT_2 (Waiting for Server) CLOSE_WAIT
Server LAST_ACK -- FIN(z) --> FIN_WAIT_2
Client TIME_WAIT <-- ACK(z+1) -- LAST_ACK
Server CLOSED (Connection Closed) TIME_WAIT
Client CLOSED (After 2MSL) CLOSED