Network and Data CommunicationsUnit 78 min read
Transport Layer: TCP vs UDP, Ports, Segments, Reliability & Flow Control
Unit 7 of Network and Data Communications explores the Transport Layer (Layer 4) protocols TCP and UDP, their segment formats, connection management, reliability mechanisms, flow control, congestion control, and real-world applications in apps like WhatsApp, eSewa, and YouTube. Learn how these protocols ensure data del
TAKEAWAYS:
- TCP (Transmission Control Protocol) is connection-oriented, reliable, and uses three-way handshake for setup, while UDP (User Datagram Protocol) is connectionless, faster, and unreliable.
- TCP segments include sequence numbers, acknowledgments, checksums, and flags for reliability, while UDP has only source/destination ports, length, and checksum.
- Flow control (sliding window) and congestion control (slow start, AIMD) prevent network overload in TCP.
- Port numbers (0–65535) identify applications: well-known (0–1023), registered (1024–49151), and dynamic (49152–65535).
- TCP guarantees ordered, error-free delivery (used in HTTP, FTP, SSH), while UDP prioritizes speed (used in DNS, VoIP, video streaming).
- Real-world impact: eSewa uses TCP for secure transactions, WhatsApp uses UDP for low-latency messaging, and YouTube uses UDP for live streaming.
1. Introduction to the Transport Layer
The Transport Layer (Layer 4 of the OSI model) sits between the Network Layer (IP) and the Application Layer. Its primary role is to:
- Provide end-to-end communication between applications.
- Ensure reliable data transfer (TCP) or fast delivery (UDP).
- Multiplex/demultiplex data using port numbers.
- Handle flow control and congestion control.
Key Functions of the Transport Layer
classDiagram
class TransportLayer {
+ Segment data into packets
+ Multiplex/demultiplex using ports
+ Ensure reliability (TCP) or speed (UDP)
+ Flow & congestion control
+ Error recovery (TCP only)
}
class TCP {
+ Connection-oriented
+ Reliable delivery
+ Three-way handshake
+ Flow control (sliding window)
+ Congestion control (AIMD)
}
class UDP {
+ Connectionless
+ No reliability guarantees
+ Faster delivery
+ No handshake
}
TransportLayer <|-- TCP
TransportLayer <|-- UDP2. TCP (Transmission Control Protocol)
TCP is a connection-oriented, reliable protocol used for applications requiring error-free data transfer.
TCP Segment Format
Key Fields Explained:
- Sequence Number: Identifies the order of segments.
- Acknowledgment Number: Confirms received data.
- Flags:
- SYN: Synchronize (used in handshake).
- ACK: Acknowledgment.
- FIN: Terminate connection.
- RST: Reset connection.
- Checksum: Detects errors in the segment.
TCP Three-Way Handshake
sequenceDiagram
participant Client
participant Server
Client->>Server: SYN (Sequence Number = x)
Server->>Client: SYN-ACK (Sequence Number = y, ACK = x+1)
Client->>Server: ACK (ACK = y+1)
Note over Client,Server: Connection EstablishedExample in Real Life: When you log into eSewa, TCP ensures your transaction details reach the server without errors via this handshake before data transfer begins.
3. UDP (User Datagram Protocol)
UDP is a connectionless, unreliable protocol used for speed-critical applications.
UDP Segment Format
Key Differences from TCP:
| Feature | TCP | UDP |
|---|---|---|
| Connection | Connection-oriented | Connectionless |
| Reliability | Guaranteed delivery | No guarantee |
| Speed | Slower (due to overhead) | Faster |
| Use Cases | HTTP, FTP, SSH, Email | DNS, VoIP, Video Streaming |
| Error Handling | Retransmits lost packets | Drops lost packets |
Why Use UDP?
- Low latency (critical for live video streaming like YouTube).
- No connection setup (faster for DNS queries).
- Lightweight (used in online gaming like PUBG).
Example in Real Life: When you watch a YouTube live stream, UDP sends video packets without waiting for acknowledgments, reducing delay.
4. Port Numbers and Multiplexing
Ports (0–65535) identify applications on a device.
| Port Range | Description | Example Applications |
|---|---|---|
| 0–1023 | Well-known ports | HTTP (80), FTP (21), SSH (22) |
| 1024–49151 | Registered ports | Skype (4242) |
| 49152–65535 | Dynamic/Private ports | Temporary connections |
Example in Real Life: When you visit Daraz, your browser uses port 80 (HTTP) or 443 (HTTPS) to communicate with Daraz’s server.
5. Flow Control and Congestion Control in TCP
Flow Control (Sliding Window)
- Prevents the sender from overwhelming the receiver.
- Uses window size to adjust data transmission rate.
stateDiagram-v2
[*] --> Idle
Idle --> Sending: Data sent
Sending --> Waiting: ACK received
Waiting --> Sending: Window updated
Sending --> [*]: All data sentCongestion Control (AIMD Algorithm)
- Additive Increase/Multiplicative Decrease (AIMD):
- Increase window size exponentially (slow start).
- Decrease window size multiplicatively (congestion detected).
Example in Real Life: When Ncell’s 4G network is congested, TCP reduces data speed to avoid packet loss.
6. TCP vs. UDP: Comparison Table
| Feature | TCP | UDP |
|---|---|---|
| Connection | Connection-oriented | Connectionless |
| Reliability | Guaranteed delivery | No guarantee |
| Speed | Slower (overhead) | Faster |
| Use Cases | Web browsing, Email, File transfer | VoIP, Video Streaming, DNS |
| Error Handling | Retransmits lost packets | Drops lost packets |
| Flow Control | Yes (Sliding Window) | No |
| Congestion Control | Yes (AIMD) | No |
7. Real-World Applications
1. eSewa (TCP)
- Uses TCP for secure financial transactions.
- Ensures no data loss during payment processing.
2. WhatsApp (UDP for Messaging, TCP for File Transfer)
- Uses UDP for instant messaging (low latency).
- Uses TCP for file sharing (reliable transfer).
3. YouTube Live Streaming (UDP)
- Uses UDP to minimize delay in live broadcasts.
4. NTC’s Internet Service (TCP for Web Browsing)
- When you browse ntc.net.np, TCP ensures all web pages load correctly.
5. Pathao’s Ride Booking (UDP for GPS Updates)
- Uses UDP to send real-time GPS location of drivers.
8. Worked Example: TCP Connection Setup (eSewa Login)
Scenario: You log into eSewa to pay a bill.
- Client (Your Phone) → Server (eSewa):
SYN(Sequence Number = 1000) - Server → Client:
SYN-ACK(Sequence Number = 2000, ACK = 1001) - Client → Server:
ACK(ACK = 2001) - Connection Established!
- Now, TCP ensures all transaction data is delivered correctly.
9. Worked Example: UDP in Video Streaming (YouTube)
Scenario: You watch a YouTube live stream.
- No handshake (UDP is connectionless).
- Packets may arrive out of order or lost (but playback continues smoothly).
- Checksum detects errors but does not retransmit (unlike TCP).
10. Exam Tip
How to Score Full Marks in Exams: ✅ Compare TCP and UDP in a table (as above). ✅ Draw the TCP segment format and explain each field. ✅ Explain the three-way handshake with a sequence diagram. ✅ Relate real-world apps (eSewa, WhatsApp, YouTube) to TCP/UDP. ✅ Describe flow control and congestion control with sliding window and AIMD. ✅ Mention port numbers (well-known, registered, dynamic).
Common Mistakes to Avoid: ❌ Forgetting SYN, ACK, FIN flags in TCP. ❌ Confusing UDP’s speed with TCP’s reliability. ❌ Not explaining why UDP is used in VoIP/DNS.
Based on the TU BIT syllabus for Network and Data Communications (BIT254), unit 7.
Discussion
Loading…