Published on

Transmission Control Protocol


🔧 How TCP Works

1. Connection Establishment: The 3-Way Handshake

To start a TCP session, both sides perform a three-step handshake:

  1. SYN – The client sends a synchronize (SYN) message to the server.
  2. SYN-ACK – The server responds with a SYN-ACK (synchronize-acknowledge).
  3. ACK – The client sends back an ACK, and the connection is established.

This ensures both parties are ready to start data transfer.


2. Data Transmission

  • Segmentation: TCP breaks application data into manageable segments.
  • Sequence Numbers: Each byte of data is assigned a number. These help with ordering and detecting loss.
  • Acknowledgements (ACKs): The receiver sends ACKs back to confirm receipt of data.

3. Reliability Mechanisms

  • Retransmission: If an ACK isn't received in time, TCP resends the data.
  • Checksums: Each segment includes a checksum to verify data integrity.
  • Duplicate Data Handling: Duplicate segments are detected and discarded.

4. Flow Control (Sliding Window)

TCP uses a window size to control how much data can be sent before waiting for an ACK. This:

  • Prevents sender from overwhelming the receiver.
  • Allows multiple packets to be in transit (pipeline).

5. Congestion Control

To avoid overloading the network:

  • Slow Start: TCP starts with a small congestion window (cwnd) and increases it exponentially.
  • Congestion Avoidance: Once a threshold is reached, it grows linearly.
  • Fast Retransmit & Fast Recovery: Handle loss without waiting for timeout.

6. Connection Termination: 4-Way Teardown

TCP closes a connection using a four-step process:

  1. FIN from one side
  2. ACK from the other
  3. FIN from the second side
  4. ACK from the first side

Each side closes independently.


🛠️ TCP Header (Key Fields)

FieldPurpose
Source/Destination PortIdentifies sending and receiving application
Sequence NumberIndicates the position of data
Acknowledgment NumberConfirms received data
Flags (SYN, ACK, FIN, etc.)Controls connection states
Window SizeFlow control mechanism
ChecksumError detection

🌐 Typical Applications

TCP is used when data accuracy and order are more important than speed:

  • Web browsing (HTTP, HTTPS)
  • Email (SMTP, IMAP, POP3)
  • File transfers (FTP)
  • Remote terminals (SSH, Telnet)