IT240 Business Data Communication and Networking

Business Data Communication and NetworkingUnit 613 min read

Transport Layer: TCP vs UDP – Protocols, Flow Control & Error Handling

Unit 6 of Business Data Communication and Networking explores the Transport Layer (Layer 4) of the OSI model, focusing on TCP (Transmission Control Protocol) and UDP (User Datagram Protocol)—their mechanisms, differences, and real-world applications in apps like WhatsApp, eSewa, and Ncell. Learn how TCP ensures reliabl

Core Concepts: The Transport Layer’s Role

The Transport Layer sits between the Network Layer (IP) and Application Layer (HTTP, FTP, etc.). Its job:

  • Segmentation: Breaks application data into smaller segments (TCP) or datagrams (UDP) for transmission.
  • Multiplexing/Demultiplexing: Uses port numbers (e.g., 80 for HTTP, 443 for HTTPS) to direct data to the correct application.
  • Error Control: Detects and corrects errors (TCP) or ignores them (UDP).
  • Flow Control: Adjusts data speed to prevent overwhelming the receiver (TCP only).

1. TCP (Transmission Control Protocol): The Reliable Messenger

TCP is like a courier service—it guarantees delivery, confirms receipt, and retries if something goes wrong. Key features:

How TCP Works: A Step-by-Step Trace

flowchart LR
    A["Application Layer\n(Sends data: e.g., WhatsApp message)"] --> B["Transport Layer\n(Segments data + adds TCP header)"]
    B --> C["Network Layer\n(Adds IP header → Packet)"]
    C --> D["Link Layer\n(Transmits packet over medium)"]
    D --> E["Receiver’s Link Layer"]
    E --> F["Receiver’s Network Layer\n(Extracts IP header)"]
    F --> G["Receiver’s Transport Layer\n(Reassembles segments, checks sequence numbers)"]
    G --> H["Application Layer\n(Delivers data to app)"]
    G -->|"Error Detected?"| I["Retransmit\n(If ACK not received)"]
    G -->|"No Error"| J["Send ACK\n(Acknowledgment to sender)"]

Key Mechanisms:

  • Sequence Numbers: Each segment gets a unique number to reassemble data in order.
  • Acknowledgments (ACK): Receiver sends back ACKs to confirm receipt.
  • Retransmission: If ACK isn’t received within a timeout, TCP resends the segment.
  • Flow Control: Uses a sliding window to adjust how much data is sent at once (prevents buffer overflow).
  • Congestion Control: Avoids network overload by slowing down during traffic jams (e.g., Additive Increase/Multiplicative Decrease algorithm).

TCP Header: The Control Panel


Critical Fields:

Field Purpose
Source/Dest Port Identifies the application (e.g., 443 for HTTPS).
Sequence Number Tracks segment order for reassembly.
ACK Number Confirms the next expected byte (acknowledgment).
Flags (SYN, ACK, FIN) Controls connection setup/teardown (e.g., SYN starts a connection).
Window Size Tells the sender how much data the receiver can handle.
Checksum Detects errors in the header/data.

TCP Connection: The 3-Way Handshake

Before data transfer, TCP establishes a connection:

sequenceDiagram
    Client->>Server: SYN (Sequence X)
    Server->>Client: SYN-ACK (Sequence Y, ACK X+1)
    Client->>Server: ACK (ACK Y+1)

Example: When you load a webpage (HTTPS), your browser and the server perform this handshake before exchanging data.


TCP in Action: Ncell’s VoIP Calls

Ncell uses VoIP (Voice over IP) for calls, which relies on RTP (Real-time Transport Protocol) over UDP for speed, but TCP for setup (SIP protocol). However, if a call drops due to packet loss, TCP’s reliability ensures retransmission of critical signaling data (e.g., call initiation/termination).


2. UDP (User Datagram Protocol): The Fast but Carefree Protocol

UDP is like sending postcards—no confirmation of delivery, but instant and lightweight. Key features:

  • No connection setup: Just send datagrams (no handshake).
  • No retransmission: If a packet is lost, it’s gone (no ACKs).
  • No ordering guarantee: Segments may arrive out of order.
  • Smaller header (8 bytes vs. TCP’s 20+ bytes): Faster processing.

UDP Header: Minimalist Design


Field Purpose
Source/Dest Port Identifies the application.
Length Total size of UDP header + data.
Checksum Optional error-checking (some OSes skip it).

When to Use UDP: Real-World Examples

Application Why UDP?
WhatsApp Calls Voice/video streams need low latency more than reliability.
eSewa Transactions Payment requests (e.g., POST /pay) use HTTP over TCP, but DNS lookups (which resolve esewa.com.np) often use UDP for speed.
Online Gaming (e.g., PUBG) Player movements must update instantly; lost packets are less critical than lag.
NTC’s IPTV Streaming Video buffers can hide minor packet loss; TCP’s retransmissions would cause delays.
DNS Queries A single lost query can be resent (UDP is faster than TCP for this).

3. TCP vs. UDP: Side-by-Side Comparison

Feature TCP UDP
Connection Connection-oriented (3-way handshake) Connectionless (just send)
Reliability Guaranteed delivery (ACKs, retransmissions) No guarantee (fire-and-forget)
Ordering Sequenced (reassembles out-of-order segments) No ordering (segments may arrive randomly)
Flow Control Yes (sliding window) No
Congestion Control Yes (avoids network overload) No
Header Size 20–60 bytes 8 bytes
Speed Slower (due to overhead) Faster (no handshake or ACKs)
Use Cases Web browsing (HTTP/HTTPS), emails (SMTP), file transfers (FTP) Live streaming, gaming, VoIP, DNS
Example in Nepal Daraz order processing (HTTP over TCP ensures no lost order data) Pathao ride updates (UDP for real-time location tracking)

4. Hybrid Protocols: Best of Both Worlds

Some applications use both TCP and UDP:

  • YouTube Streaming:
    • TCP for initial setup (handshake, metadata).
    • UDP for video/audio chunks (to reduce latency).
  • WhatsApp:
    • TCP for messages (reliability).
    • UDP for voice/video calls (low latency).

In the Real World

  1. eSewa Payments:

    • When you transfer money via eSewa, the HTTP request (e.g., POST /api/transfer) uses TCP to ensure the transaction data isn’t lost or corrupted. If your internet drops mid-transfer, TCP’s retransmission guarantees the payment goes through.
    • Worked Example: Suppose you send ₹5,000 to a friend. The eSewa server segments this request into TCP packets. If packet #3 is lost, TCP’s ACK mechanism detects the missing ACK for #3 and automatically resends it, ensuring the transfer completes.
  2. Nepal Telecom’s VoIP Calls:

    • Ncell’s VoIP service uses RTP (UDP) for voice packets because a slight delay is tolerable, but packet loss causes choppy audio. However, the Session Initiation Protocol (SIP), which sets up the call, uses TCP to ensure the call initiation/termination commands aren’t lost.
  3. Daraz Order Processing:

    • When you place an order on Daraz, the HTTP request (e.g., POST /order/create) travels over TCP to guarantee the order details (product IDs, quantity, shipping address) reach the server. If your connection drops, TCP’s reliability ensures the order isn’t lost.
    • Visual: Imagine Daraz’s order queue as a buffer. TCP’s flow control adjusts the "speed" of orders entering the queue to match the server’s processing capacity, preventing crashes during sales like Dashain.

Exam Tip

What Examiners Look For

  1. TCP/UDP Definitions:

    • Know the core differences (reliability, connection, speed) and when each is used.
    • Common Pitfall: Students often confuse UDP’s "no reliability" with "no error checking" (UDP does have a checksum, but no retransmission).
  2. TCP Mechanisms:

    • 3-way handshake: Draw it and explain SYN, SYN-ACK, ACK.
    • Sequence/ACK numbers: Understand how they ensure ordered delivery.
    • Flow control: Explain the sliding window (e.g., "If the window size is 1000 bytes, the sender can transmit up to 1000 bytes before waiting for an ACK").
    • Worked Example: If a TCP segment has Sequence Number = 1000 and Length = 500, what’s the ACK number for the next segment? Answer: 1500 (1000 + 500).
  3. Real-World Applications:

    • Nepal-specific: Relate TCP to bank transactions (Nabil Bank’s online transfers) or eSewa payments; UDP to NTC’s IPTV or Pathao’s live tracking.
    • Global: WhatsApp (UDP for calls), YouTube (hybrid), DNS (UDP).
    • Case Study: Chaudhary Group’s Daraz uses TCP for order processing but may use UDP for real-time inventory updates in warehouses.
  4. Diagrams:

    • Must-draw:
      • TCP/UDP header formats.
      • 3-way handshake.
      • Sliding window flow control.
    • Pro Tip: Label every field in headers (e.g., "Checksum = 0xABCD").
  5. Shortcomings:

    • TCP’s overhead (slow for real-time apps).
    • UDP’s no retransmission (bad for critical data).
    • Exam Question: "Why doesn’t WhatsApp use TCP for calls?" Answer: "TCP’s retransmissions introduce delay, which is unacceptable for real-time voice/video."
  6. Numerical Problems:

    • Calculate checksums (simple XOR-based examples).
    • Sequence/ACK numbers: Given a scenario, determine missing ACKs or sequence gaps.
    • Example: If a receiver sends ACK = 2000 for a segment of length 1000, what was the segment’s sequence number? Answer: 1000 (ACK = Sequence + Length).

Common Exam Questions & How to Answer

Question Type Key Points to Include
"Explain TCP’s reliability mechanism." Sequence numbers, ACKs, retransmission timeout, sliding window, checksum.
"Why is UDP faster than TCP?" No handshake, no ACKs, smaller header, no flow/congestion control.
"Compare TCP and UDP with examples." Use the table above + add Nepal examples (eSewa = TCP, NTC IPTV = UDP).
"Draw the TCP header and label fields." Include all fields: ports, sequence/ACK, flags, window size, checksum.
"How does flow control work in TCP?" Sliding window: sender adjusts data rate based on receiver’s Window Size field.
"Why can’t we use TCP for live streaming?" Retransmissions cause delays; UDP’s speed is prioritized over reliability.

Final Case Study: Kathmandu Traffic Routes (TCP Analogy)

Imagine Kathmandu’s traffic as a network:

  • TCP = A guided taxi service:
    • Your driver (TCP) calls ahead to confirm your pickup (SYN).
    • They send updates (ACK) to avoid wrong turns (retransmit if lost).
    • If traffic is heavy (congestion), they slow down (flow control).
  • UDP = A motorbike taxi:
    • No confirmation—you just hop on and hope for the best.
    • If the driver misses a turn (packet loss), you’re stuck.
    • Real-world tie: Pathao’s ride updates use UDP for speed, but payment processing uses TCP for reliability.

mindmap
  root((Transport Layer: TCP vs UDP))
    TCP
      Connection-Oriented: "3-way handshake (SYN, SYN-ACK, ACK)"
      Reliable: "ACKs, retransmission, sequence numbers"
      Flow Control: "Sliding window (adjusts data rate)"
      Congestion Control: "AIMD algorithm"
      Use Cases: "HTTP, FTP, emails (SMTP)"
      Nepal Example: "eSewa payments, Daraz orders"
    UDP
      Connectionless: "No handshake, just send"
      Unreliable: "No ACKs, no retransmission"
      Fast: "Smaller header (8 bytes), no overhead"
      Use Cases: "DNS, VoIP, gaming, live streaming"
      Nepal Example: "NTC IPTV, Pathao ride updates"
    Comparison
      TCP: "Reliable but slow"
      UDP: "Fast but unreliable"
    Hybrid Protocols
      Example: "YouTube (TCP for setup, UDP for streaming)"
      Example: "WhatsApp (TCP for messages, UDP for calls)"

Based on the TU BIM syllabus for Business Data Communication and Networking (IT240), unit 6.

Discussion

Loading…