Software-Engineering

Transmission Control Protocol (TCP)

What is TCP?

Transmission Control Protocol (TCP) is a core protocol of the Internet Protocol Suite (TCP/IP). It operates at the Transport Layer (Layer 4) of the OSI model and provides reliable, ordered, and error-checked delivery of data between applications running on hosts communicating over an IP network.

How Does TCP Work?

TCP establishes a connection-oriented communication channel between two endpoints. It breaks data into segments, adds sequence numbers and acknowledgments, and ensures reliable delivery through:

  1. Connection Establishment: Three-way handshake
  2. Data Transfer: Reliable, ordered delivery with flow control
  3. Connection Termination: Four-way handshake
  4. Error Recovery: Retransmission of lost packets
  5. Congestion Control: Prevents network congestion

TCP Header Structure

A TCP header contains 20 bytes of mandatory fields plus optional data:

Why is TCP Needed?

TCP is essential for applications requiring reliable data delivery:

Without TCP, applications like web browsing, email, and file transfers would be unreliable and prone to data loss.

TCP Three-Way Handshake

The three-way handshake establishes a TCP connection:

sequenceDiagram
    participant Client
    participant Server

    Client->>Server: SYN (seq=x)
    Server->>Client: SYN-ACK (seq=y, ack=x+1)
    Client->>Server: ACK (ack=y+1)
  1. SYN: Client sends synchronization packet with initial sequence number
  2. SYN-ACK: Server acknowledges client’s SYN and sends its own SYN
  3. ACK: Client acknowledges server’s SYN

TCP Connection Termination

TCP uses a four-way handshake to close connections gracefully:

sequenceDiagram
    participant Client
    participant Server

    Client->>Server: FIN
    Server->>Client: ACK
    Server->>Client: FIN
    Client->>Server: ACK