TCP, UDP, flow control, congestion control, and ports
Layer 4 - End-to-end reliable data transfer
| Function | Description |
|---|---|
| Segmentation | Break data into segments |
| Multiplexing | Multiple apps share network |
| Port Addressing | Identify applications |
| Flow Control | Prevent receiver overflow |
| Error Control | Reliable delivery (TCP) |
| Feature | TCP | UDP |
|---|---|---|
| Connection | Connection-oriented | Connectionless |
| Reliability | Reliable (ACKs, retransmit) | Unreliable |
| Ordering | Guaranteed order | No ordering |
| Speed | Slower (overhead) | Faster |
| Header | 20-60 bytes | 8 bytes |
| Use Case | Web, Email, File transfer | Streaming, DNS, Gaming |
TCP: When data integrity matters
- Web browsing (HTTP)
- Email (SMTP)
- File transfer (FTP)
- Database transactions
UDP: When speed matters
- Video streaming
- Online gaming
- DNS queries
- VoIP calls
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
┌───────────────────────────────┬───────────────────────────────┐
│ Source Port │ Destination Port │
├───────────────────────────────┴───────────────────────────────┤
│ Sequence Number │
├───────────────────────────────────────────────────────────────┤
│ Acknowledgment Number │
├───────┬───────┬─┬─┬─┬─┬─┬─┬───────────────────────────────────┤
│ Offset│Reserved│U│A│P│R│S│F│ Window Size │
├───────┴───────┴─┴─┴─┴─┴─┴─┴───────────────────────────────────┤
│ Checksum │ Urgent Pointer │
└───────────────────────────────┴───────────────────────────────┘
Flags: URG, ACK, PSH, RST, SYN, FIN
| Flag | Name | Purpose |
|---|---|---|
| SYN | Synchronize | Connection establishment |
| ACK | Acknowledge | Acknowledge received data |
| FIN | Finish | Connection termination |
| RST | Reset | Abort connection |
| PSH | Push | Send immediately |
| URG | Urgent | Priority data |
Client Server
| |
|------- SYN --------> | (seq=x)
| |
|<---- SYN + ACK ---- | (seq=y, ack=x+1)
| |
|------- ACK --------> | (ack=y+1)
| |
| CONNECTION OPEN |
Client Server
| |
|------- FIN --------> | Client wants to close
| |
|<------ ACK ------- | Server acknowledges
| |
|<------ FIN ------- | Server ready to close
| |
|------- ACK --------> | Client acknowledges
| |
| CONNECTION CLOSED |
Prevent sender from overwhelming receiver
Sender Window = min(Receiver Window, Congestion Window)
Receiver advertises its buffer space in Window Size field
Sender adjusts sending rate accordingly
Example:
Receiver says: "Window = 4000 bytes"
Sender can send 4000 bytes before waiting for ACK
Prevent sender from overwhelming network
1. Slow Start
- Start with cwnd = 1 MSS
- Double cwnd each RTT (exponential)
- Until threshold (ssthresh)
2. Congestion Avoidance
- After threshold, increase cwnd by 1/RTT (linear)
- Gentle growth to probe capacity
3. Fast Retransmit
- 3 duplicate ACKs = packet loss
- Retransmit without waiting for timeout
4. Fast Recovery
- After fast retransmit
- cwnd = ssthresh + 3 MSS
- Avoid slow start
cwnd
| ***
| **** | congestion avoidance
| **** | (linear)
| ****-------------|---- ssthresh
| ** |
| * |
|* slow start |
| (exponential) |
+---------------------+---> Time
| Port | Protocol | Service |
|---|---|---|
| 20 | TCP | FTP Data |
| 21 | TCP | FTP Control |
| 22 | TCP | SSH |
| 23 | TCP | Telnet |
| 25 | TCP | SMTP |
| 53 | TCP/UDP | DNS |
| 80 | TCP | HTTP |
| 110 | TCP | POP3 |
| 143 | TCP | IMAP |
| 443 | TCP | HTTPS |
0-1023: Well-known (system)
1024-49151: Registered (applications)
49152-65535: Dynamic/Private (ephemeral)
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
┌───────────────────────────────┬───────────────────────────────┐
│ Source Port │ Destination Port │
├───────────────────────────────┼───────────────────────────────┤
│ Length │ Checksum │
└───────────────────────────────┴───────────────────────────────┘
Only 8 bytes! (vs TCP's 20+ bytes)
| Concept | Key Point |
|---|---|
| TCP | Reliable, connection-oriented, slower |
| UDP | Fast, connectionless, unreliable |
| 3-way handshake | SYN → SYN-ACK → ACK |
| 4-way handshake | FIN → ACK → FIN → ACK |
| Flow Control | Receiver's window size |
| Congestion Control | Slow start → congestion avoidance |
| HTTP Port | 80 (HTTP), 443 (HTTPS) |
Test your understanding with step-by-step solutions
10 questions · 90s per question
Each question has a 90-second time limit. Unanswered questions will be auto-submitted when time runs out.