Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

TCP Connection Lifecycle: Handshakes, Termination, and State Transitions

Tech May 13 1

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.

  1. 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 the SYN_SENT state.
  2. 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+1 to acknowledge the client's SYN. The server also generates its own random ISN, seq=y. The server moves to the SYN_RCVD state.
  3. 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 to seq=x+1. Both endpoints transition to the ESTABLISHED state, 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.

  1. Active Close (FIN): The client (initiator) sends a segment with the FIN flag set (FIN=1) and a sequence number seq=x. The client enters the FIN_WAIT_1 state.
  2. Acknowledgment of Close (ACK): The server acknowledges the termination request by sending an ACK segment with ack=x+1. The server transitions to the CLOSE_WAIT state. Upon receiving this ACK, the client moves to FIN_WAIT_2. The connection is now half-closed.
  3. Server Initiation of Close (FIN): After sending any remaining data, the server sends its own FIN segment (FIN=1) with seq=z to close its side of the connection. The server enters the LAST_ACK state.
  4. Final Acknowledgment (ACK): The client sends a final ACK with ack=z+1 and enters the TIME_WAIT state. The server receives this ACK and transitions to CLOSED. 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
Tags: TCP/IP

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.