Computer NetworksUnit 89 min read

Transport Layer: TCP, UDP, Ports, Flow Control & Congestion Avoidance

Unit 8 of Computer Networks covers the Transport Layer (Layer 4 of the OSI model), focusing on TCP and UDP protocols, their mechanisms (connection setup, flow control, congestion avoidance), port numbers, and real-world applications in data transmission. This note explains how these protocols ensure reliable or best-ef

TAKEAWAYS:

  • The Transport Layer provides end-to-end communication between applications using TCP (reliable) or UDP (fast, connectionless).
  • TCP uses three-way handshake, sequence numbers, acknowledgments, and flow control (sliding window) to ensure data integrity.
  • UDP is lightweight, used for real-time apps (e.g., VoIP, video streaming) where speed matters more than reliability.
  • Port numbers (0–65535) identify specific processes or services (e.g., port 80 for HTTP, 443 for HTTPS).
  • Congestion control (e.g., AIMD, slow start) prevents network overload by dynamically adjusting transmission rates.
  • Real-world examples: WhatsApp uses UDP for voice calls, eSewa uses TCP for secure transactions, and Ncell’s mobile data relies on TCP for reliable downloads.

1. Role of the Transport Layer

The Transport Layer (Layer 4) sits above the Network Layer (IP) and below the Application Layer. Its primary job is to:

  • Provide logical communication between processes (not devices).
  • Ensure data integrity (TCP) or speed (UDP).
  • Multiplex/demultiplex data using port numbers.

Key Functions

Function TCP UDP
Connection Setup 3-way handshake (SYN, SYN-ACK, ACK) No connection (datagram-based)
Reliability Guaranteed (acks, retries) No guarantee (fire-and-forget)
Ordering Sequenced packets No ordering (may arrive out of order)
Flow Control Sliding window No flow control
Congestion Control Yes (AIMD, slow start) No
Overhead High (headers: 20–60 bytes) Low (headers: 8 bytes)

2. TCP (Transmission Control Protocol)

TCP is a connection-oriented, reliable protocol used for applications where data must arrive correctly and in order (e.g., file transfers, emails, web browsing).

TCP Header Structure

08162431Source Port16 bitsDestination Port16 bitsSequence Number32 bitsAcknowledgment Number32 bitsData Offset4 bitsReserved6 bitsFlags (URG,ACK, PSH, RST, 6 bitsWindow Size16 bitsChecksum16 bitsUrgent Pointer16 bitsOptions (variabl
TCP Header Structure (20 bytes minimum, 60 bytes maximum)

TCP Connection Establishment: 3-Way Handshake

sequenceDiagram
  participant Client as Client (e.g., eSewa App)
  participant Server as Server (e.g., eSewa Payment Gateway)
  Client->>Server: SYN (Sequence Number = x)
  Server->>Client: SYN-ACK (Sequence Number = y, ACK = x+1)
  Client->>Server: ACK (Sequence Number = x+1, ACK = y+1)
  Note over Client,Server: Connection Established!

Worked Example: eSewa Transaction When you pay via eSewa:

  1. Your phone (client) sends a SYN to the eSewa server.
  2. The server responds with SYN-ACK.
  3. Your phone sends ACK, and the TCP connection is ready for secure data transfer (e.g., payment details).

TCP Reliability Mechanisms

  1. Sequence and Acknowledgment Numbers
    • Each byte is numbered. The receiver sends an ACK for the next expected byte.
    • Example: If ACK = 1000, the sender knows bytes 1–999 were received.
Step 1Sender transmitssegment with Sequence Step 2Receiver sends ACK= x+1 if data receivedStep 3Retransmissiontimer expires → SenderStep 4Fast Retransmit:Duplicate ACKs trigger
TCP Reliability: ACKs and Retransmissions
  1. Retransmission Timeout (RTO)

    • If an ACK isn’t received within RTO, the sender retransmits.
    • RTO is dynamically adjusted (e.g., doubled on timeout).
  2. Sliding Window (Flow Control)

    • Controls how much data the sender can transmit before waiting for an ACK.
    • Window Size = Receiver’s buffer capacity.
    • Example: If the window size is 1000 bytes, the sender waits for ACK before sending more.

TCP Congestion Control

Congestion occurs when too many packets flood the network. TCP uses:

  1. Slow Start
    • Starts with a small window (e.g., 1 MSS) and exponentially increases until a threshold (ssthresh).
  2. Congestion Avoidance (AIMD)
    • After sstresh, the window increases linearly (add 1 MSS per RTT).
  3. Fast Retransmit
    • If 3 duplicate ACKs arrive, the sender assumes packet loss and retransmits without waiting for RTO.
  4. Fast Recovery
    • Reduces the window to ssthresh + 3 MSS and continues transmission.
1058SenderRouter 1Router 2Receiver
Congestion Window (cwnd) and Flight Size Visualization

Worked Example: Ncell Mobile Data When you download a large file on Ncell’s network:

  • TCP starts with slow start (small window).
  • If the network is congested (e.g., many users streaming during a match), TCP reduces its window size to avoid packet loss.

3. UDP (User Datagram Protocol)

UDP is connectionless, unreliable, and low-latency. Used for:

  • Real-time apps (VoIP, video calls).
  • DNS lookups.
  • Online gaming (where speed > reliability).

UDP Header Structure

0481215Source Port16 bitsDestination Port16 bitsLength16 bitsChecksum16 bits
UDP Header Structure (8 bytes fixed)

Comparison: TCP vs. UDP

Feature TCP UDP
Connection Connection-oriented Connectionless
Reliability Guaranteed Not guaranteed
Overhead High (20–60 bytes) Low (8 bytes)
Speed Slower (due to reliability) Faster
Use Cases Web, email, file transfer VoIP, video, DNS, gaming

4. Port Numbers

Ports (0–65535) identify processes/services on a device.

  • Well-known ports (0–1023): Assigned by IANA (e.g., 80 = HTTP, 443 = HTTPS).
  • Registered ports (1024–49151): Used by apps (e.g., 5432 = PostgreSQL).
  • Dynamic/Private ports (49152–65535): Temporary ports for client-side connections.

Example: Daraz Order Processing When you place an order on Daraz:

  1. Your browser (client) uses port 80 (HTTP) or 443 (HTTPS) to contact Daraz’s server.
  2. Daraz’s server responds on the same port, but its source port may be a dynamic port (e.g., 50000).

5. Real-World Applications

Example 1: WhatsApp (UDP for Voice Calls)

  • WhatsApp uses UDP for voice/video calls because:
    • Packets may arrive out of order or lost, but real-time is critical.
    • Lower latency than TCP.

Example 2: eSewa (TCP for Transactions)

  • eSewa uses TCP for payment processing because:
    • Reliability is critical (no lost transactions).
    • Order matters (e.g., "debit → transfer → confirm").

Example 3: Ncell Mobile Data (TCP for Downloads)

  • When you download an app from the Play Store:
    • TCP ensures all data packets arrive correctly and in order.
    • If packets are lost, TCP retransmits them automatically.

Example 4: Online Gaming (UDP for Low Latency)

  • Games like Free Fire or PUBG use UDP because:
    • A few lost packets don’t ruin gameplay.
    • Speed > perfect reliability.

6. Exam Tip: How This Unit is Tested

  1. Definitions & Differences

    • Expect questions comparing TCP vs. UDP (reliability, connection, overhead).
    • Know the 3-way handshake and sliding window mechanics.
  2. Worked Examples

    • Trace a TCP connection (e.g., "Show the handshake for an eSewa login").
    • Calculate window sizes in flow control (e.g., "If MSS=1000 and window=4000, how many packets can be sent?").
  3. Real-World Scenarios

    • Why does WhatsApp use UDP for calls but TCP for messages?
    • How does Ncell’s network handle congestion during peak hours?
  4. Diagrams & Packet Analysis

    • Draw TCP/UDP headers and label fields.
    • Sketch sliding window or congestion control graphs.
  5. Common Pitfalls

    • Don’t confuse port numbers with IP addresses (ports identify processes, IPs identify devices).
    • TCP is reliable but slower; UDP is fast but unreliable.
    • Congestion control is not flow control (flow control is sender-receiver; congestion is network-wide).

Based on the PU BE Computer (PU) syllabus for Computer Networks, unit 8.

Discussion

Loading…