CACS303 Computer Networking

Computer NetworkingUnit 913 min read

Transport Layer: TCP vs UDP, Segment Headers, Reliability & Flow Control

Unit 9 of Computer Networking explores the Transport Layer’s two protocols—TCP (reliable, connection-oriented) and UDP (fast, connectionless)—their segment headers, error control, flow control, and real-world applications in apps like WhatsApp (UDP for voice) and eSewa (TCP for transactions). Learn how TCP’s three-way

TAKEAWAYS:

  • TCP guarantees reliable, ordered delivery via acknowledgments, retransmissions, and sequence numbers, while UDP offers speed over accuracy with no handshake or error recovery.
  • The TCP segment header (20–60 bytes) includes flags (SYN, ACK, FIN), sequence/acknowledgment numbers, and checksums; UDP’s header (8 bytes) only has source/destination ports and checksum.
  • Flow control (TCP’s sliding window) prevents sender overload by dynamically adjusting window size, while congestion control (slow start, AIMD) manages network traffic.
  • Port numbers (0–65535) identify applications: HTTP (80), HTTPS (443), DNS (53), and ephemeral ports (>1024) for client responses.
  • Real-world trade-offs: WhatsApp uses UDP for voice calls (low latency), while eSewa uses TCP for transactions (no data loss).
  • Exam focus: Compare TCP/UDP headers, explain three-way handshake, and calculate window sizes or sequence numbers in worked examples.

1. Transport Layer Overview: Why TCP and UDP?

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

  • Segmentation: Breaks application data into smaller segments (TCP) or datagrams (UDP) for transmission.
  • Multiplexing/Demultiplexing: Uses port numbers to direct data to the correct application.
  • Reliability: Ensures data arrives correctly and in order (TCP) or delivers it as-is (UDP).
ApplicationDataTransportSegment (TCP) / Datagram (UDP)NetworkPacketData LinkFramePhysicalBits
Transport Layer (Layer 4) between Network (IP) and Application layers

2. UDP: The "Fire-and-Forget" Protocol

UDP (User Datagram Protocol) is fast but unreliable. It’s used where speed matters more than accuracy.

Key Features

  • No connection setup: Sends data immediately (no handshake).
  • No retransmissions: If a packet is lost, the app must handle it (e.g., video glitches).
  • No ordering guarantees: Packets may arrive out of order.
  • Minimal overhead: Only 8-byte header (vs. TCP’s 20–60 bytes).

UDP Header Format

Why UDP?

  • DNS queries (port 53): Speed > reliability.
  • VoIP (WhatsApp calls): Dropped packets cause minor glitches, not crashes.
  • Online gaming: Low latency is critical; lost packets are acceptable.

Worked Example: WhatsApp Voice Call

  • WhatsApp uses UDP for voice calls because:
    • A 50ms delay in retransmitting a lost packet feels like a 10-second lag to the user.
    • 1–2% packet loss is tolerable (human ear doesn’t notice).
  • If TCP were used, a single lost packet could stall the call until retransmitted.

3. TCP: The Reliable Workhorse

TCP (Transmission Control Protocol) ensures data arrives correctly, in order, and without loss. It’s used for web browsing, emails, file transfers.

Key Features

  • Connection-oriented: Uses a three-way handshake to establish a connection.
  • Reliable delivery: Retransmits lost packets and reorders out-of-sequence data.
  • Flow control: Adjusts sending rate to match the receiver’s capacity.
  • Congestion control: Avoids overwhelming the network.

TCP Three-Way Handshake

sequenceDiagram
    participant Client as Client (e.g., Browser)
    participant Server as Server (e.g., Google)
    Client->>Server: SYN (Seq=1000)
    Server->>Client: SYN-ACK (Seq=2000, Ack=1001)
    Client->>Server: ACK (Seq=1001, Ack=2001)
    Note over Client,Server: Connection Established!

Worked Example: eSewa Transaction

  1. Your phone (client) sends a SYN to eSewa’s server.
  2. Server replies SYN-ACK.
  3. You send ACK → connection open.
  4. You send payment data → server acknowledges receipt.
  5. If a packet is lost, TCP retransmits it before closing the connection (FIN handshake).

TCP Segment Header

08162431Source Port16 bitsDestination Port16 bitsSequence Number32 bitsAcknowledgment Number32 bitsData Offset (4)4 bitsReserved(6)6 bitsFlags (6)6 bitsWindow Size16 bitsChecksum16 bitsUrgent Pointer16 bits
TCP Segment Header (20-byte minimum)

Critical Fields Explained

Field Purpose
Sequence Number Tracks byte order (e.g., Seq=1000 means this is the 1000th byte).
Acknowledgment # Confirms receipt of data (e.g., Ack=2001 means "I got up to byte 2000").
Flags SYN (start connection), ACK (acknowledge), FIN (end connection).
Window Size Tells sender how much data the receiver can handle (flow control).

4. TCP Reliability Mechanisms

TCP ensures reliability through:

  1. Acknowledgments (ACKs): Receiver sends ACKs for every segment.
  2. Retransmissions: If ACK isn’t received within a timeout, TCP resends.
  3. Sequence Numbers: Ensures packets are reassembled in order.
  4. Checksum: Detects corrupted data (discards if invalid).

Worked Example: Downloading a File from Daraz

  • You request a 10MB file → Daraz’s server sends it in TCP segments.
  • If Segment 5 is lost:
    • Your device times out waiting for ACK.
    • Daraz retransmits Segment 5.
    • You ACK Segment 5 → download resumes.

5. Flow Control: Sliding Window

TCP uses a sliding window to control data flow and prevent overload.

[object Object][object Object][object Object]SenderReceiver
Sliding Window: Sender waits for ACK before sliding window forward

How It Works

  1. Sender sends up to Window Size bytes (e.g., 1000 bytes).
  2. Receiver sends ACK → window "slides" forward.
  3. If receiver is slow, it reduces window size (e.g., to 500 bytes).

Worked Example: Kathmandu Traffic (Analogy)

  • Imagine a one-lane road (window size = 1 car at a time).
  • If traffic ahead is slow (small window), you reduce speed (send fewer packets).
  • If traffic clears (large window), you speed up (send more packets).

6. Congestion Control: Avoiding Network Collapse

TCP monitors network congestion and adjusts sending rate using:

  • Slow Start: Exponentially increases window size until loss occurs.
  • AIMD (Additive Increase, Multiplicative Decrease): Gradually increases window, then halves it after packet loss.
StartWindow = 1 packetTime 1Window increasesby 1 (+AIMD)Time 2Window = 3 packetsLoss DetectedWindow halves (→1.5)Time 3Window increasesby 1 again
TCP AIMD Congestion Control: Gradual increase, multiplicative decrease

Real-World Impact

  • Without congestion control, YouTube videos would buffer constantly during peak hours.
  • NTC’s fiber network uses TCP’s AIMD to avoid overloading during exams.

7. TCP vs. UDP: Comparison Table

Feature TCP UDP
Connection Connection-oriented (handshake) Connectionless
Reliability Guaranteed (retransmits lost data) No guarantee
Ordering Packets arrive in order May arrive out of order
Header Size 20–60 bytes 8 bytes
Speed Slower (overhead) Faster
Use Cases Web (HTTP), Email (SMTP), FTP DNS, VoIP, Video Streaming
Flow Control Yes (sliding window) No
Congestion Control Yes (AIMD, Slow Start) No

8. Port Numbers: The Application’s Address

Ports (0–65535) identify applications on a device.

  • Well-known ports (0–1023): Assigned by IANA (e.g., 80 = HTTP).
  • Registered ports (1024–49151): Used by apps (e.g., 5432 = PostgreSQL).
  • Ephemeral ports (49152–65535): Temporary client ports (e.g., 54321).

Example: Accessing a Website

  1. Your browser opens an ephemeral port (e.g., 54321).
  2. It sends a request to port 80 (HTTP) or 443 (HTTPS) on the server.
  3. Server replies on port 80/443 → your browser reads it via 54321.
[object Object][object Object]
Port Numbers: Ephemeral (client) ↔ Well-known (server) ports

9. In the Real World

[object Object][object Object][object Object]
Hybrid Protocol: WhatsApp uses UDP for real-time media but TCP for metadata

1. WhatsApp (UDP for Voice Calls)

  • Why UDP?
    • Voice packets must arrive within 30ms to sound natural.
    • 1–2% packet loss is acceptable (human ear fills gaps).
    • TCP’s retransmissions would cause unacceptable delays.

2. eSewa (TCP for Transactions)

  • Why TCP?
    • No lost packets: A missing "transfer money" packet means failed transaction.
    • Ordered delivery: Ensures account numbers and amounts arrive correctly.
    • Flow control: Prevents server overload during peak hours (e.g., Dashain).

3. YouTube (UDP + TCP Hybrid)

  • Video streaming uses UDP for most packets (speed > perfection).
  • Critical metadata (e.g., subtitles) uses TCP to ensure accuracy.

4. NTC’s Fiber Network (TCP Congestion Control)

  • During exam seasons, NTC’s routers use TCP AIMD to:
    • Detect congestion (packet loss).
    • Reduce bandwidth for non-urgent traffic (e.g., Netflix).
    • Prioritize exams (lower latency for TU/PU portals).

5. Online Gaming (UDP + Custom Protocols)

  • Games like PUBG use UDP for gameplay but add:
    • Client-side prediction: Assumes actions happened (even if packets are lost).
    • Server reconciliation: Fixes mistakes after the fact.

10. Exam Tip: How to Score Full Marks

Common Exam Questions & How to Answer

Question Type Key Points to Include Marks Allocation
Compare TCP and UDP - Connection (oriented vs. connectionless) <br> - Reliability (ACKs vs. none) <br> - Speed (TCP slower due to overhead) <br> - Use cases (e.g., HTTP vs. DNS) 5–8 marks
TCP Header Explanation - Sequence/Ack numbers <br> - Flags (SYN, ACK, FIN) <br> - Window size (flow control) <br> - Checksum (error detection) 6–10 marks
Three-Way Handshake - SYN → SYN-ACK → ACK <br> - Sequence numbers in each step <br> - Connection states (SYN_SENT, ESTABLISHED) 4–6 marks
Flow Control - Sliding window mechanism <br> - Window size adjustment <br> - Example: Daraz download 5–7 marks
Port Numbers - Well-known (80, 443) vs. ephemeral ports <br> - How multiplexing works <br> - Example: Browser → Server communication 3–5 marks

Tips for Worked Examples

  1. Sequence Numbers: Always show Seq and Ack values (e.g., "Seq=1000, Ack=2001").
  2. Window Size: Calculate based on bytes sent and ACK received (e.g., "Window slides from 1000–2000 after ACK").
  3. Retransmissions: State timeout and retransmit logic (e.g., "No ACK for Segment 5 → retransmit").
  4. Real-World Tie-Ins: Link answers to eSewa (TCP), WhatsApp (UDP), or NTC congestion.

Avoid These Mistakes

  • ❌ Forgetting flags in TCP header (SYN, ACK, FIN).
  • ❌ Mixing up sequence number (sender’s byte count) and acknowledgment number (next expected byte).
  • ❌ Ignoring port numbers in UDP/TCP questions.
  • ❌ Describing UDP as "unreliable" without explaining why it’s acceptable (e.g., VoIP).

Based on the TU BCA syllabus for Computer Networking (CACS303), unit 9.

Discussion

Loading…