Compare TCP and UDP and know when each is appropriate
Describe the TCP three-way handshake and four-way teardown
Explain port numbers, well-known ports, and socket pairs
Read a TCP segment header and identify key fields
Understand windowing, flow control, and congestion management
TCP vs UDP at a Glance
Feature
TCP
UDP
Connection
Connection-oriented (handshake)
Connectionless
Reliability
Guaranteed delivery, retransmission
Best-effort, no guarantees
Ordering
Sequence numbers ensure order
No ordering
Flow Control
Sliding window
None
Header Size
20-60 bytes
8 bytes
Speed
Slower (overhead)
Faster (minimal overhead)
Use Cases
Web, email, file transfer, SSH
DNS, DHCP, VoIP, streaming, gaming
TCP Segment Structure
Every TCP segment contains a header with critical control information. Understanding these fields is essential for packet analysis and troubleshooting.
Sequence Number: Tracks byte position in the data stream. Enables ordered reassembly.
Acknowledgment Number: The next byte the receiver expects. Confirms receipt of all prior data.
Window Size: How many bytes the receiver can accept before needing an ACK. Controls flow rate.
The Three-Way Handshake
TCP establishes a connection before any data is exchanged. This ensures both sides are ready, agrees on initial sequence numbers, and negotiates parameters like MSS (Maximum Segment Size).
/* Connection Establishment */Step 1: Client --> Server [SYN] seq=100
// Client picks ISN (Initial Sequence Number), sets SYN flagStep 2: Server --> Client [SYN-ACK] seq=300, ack=101
// Server picks its own ISN, acknowledges client's ISN+1Step 3: Client --> Server [ACK] seq=101, ack=301
// Client acknowledges server's ISN+1. Connection ESTABLISHED.
The Four-Way Teardown
Either side can initiate connection termination. The four-way process allows each direction to close independently (half-close).
Step 1: Client --> Server [FIN, ACK]
// Client says "I'm done sending"Step 2: Server --> Client [ACK]
// Server acknowledges the FINStep 3: Server --> Client [FIN, ACK]
// Server says "I'm done too"Step 4: Client --> Server [ACK]
// Client acknowledges. Connection CLOSED.// Client enters TIME_WAIT (2x MSL) before fully releasing.
Port Numbers
Port numbers identify specific services and applications on a host. Combined with an IP address, they form a socket (e.g., 192.168.1.10:443). A connection is uniquely identified by the 5-tuple: protocol, source IP, source port, destination IP, destination port.
TCP uses a sliding window mechanism to control how much data can be sent before requiring an acknowledgment. The receiver advertises its window size -- the amount of buffer space available. The sender must not exceed this limit.
/* Sliding Window Example */
Sender window size: 4 segmentsSent & ACKed: [1] [2] [3]
Sent, waiting: [4] [5] [6] [7] <-- current windowNot yet sent: [8] [9] [10] ...
/* When ACK for segment 4 arrives, window slides forward:
[5] [6] [7] [8] become the new window. *//* Congestion control algorithms (Slow Start, Congestion
Avoidance) dynamically adjust the effective window size
based on network conditions. */
UDP Datagram Structure
UDP is deliberately simple. Its 8-byte header contains only four fields -- just enough to deliver data to the right port. No sequencing, no acknowledgment, no retransmission. Applications that use UDP handle reliability themselves (if needed).
/* UDP Header (8 bytes total) */
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Port | Destination Port |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Length | Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/* Why use UDP? When speed matters more than reliability:
- DNS queries (small, fast lookups)
- VoIP (a dropped packet = brief silence, not a retransmission delay)
- Gaming (stale position data is useless -- send the new one)
- DHCP (client doesn't have an IP yet to establish TCP) */
Practical: Reading a Packet Capture
In a Wireshark capture, you can identify the three-way handshake by filtering for TCP flags:
/* Wireshark display filter for handshakes */Filter: tcp.flags.syn == 1
/* Example capture output */
No. Time Source Dest Info
1 0.000 192.168.1.50 93.184.216.34 TCP 74 49152->443 [SYN]2 0.023 93.184.216.34 192.168.1.50 TCP 74 443->49152 [SYN,ACK]3 0.024 192.168.1.50 93.184.216.34 TCP 66 49152->443 [ACK]4 0.025 192.168.1.50 93.184.216.34 TLS Client Hello
Key Insight:
The time between packet 1 and packet 2 (0.023s) is the Round Trip Time (RTT). This is one of the most important network performance metrics. High RTT means latency issues -- critical for VoIP, gaming, and real-time applications.
Key Takeaways
TCP is connection-oriented with guaranteed delivery; UDP is connectionless and fast
TCP uses SYN/SYN-ACK/ACK to establish connections and FIN/ACK to tear them down
Port numbers identify services: 0-1023 well-known, 49152-65535 ephemeral
TCP flow control uses sliding windows; the receiver advertises available buffer space
UDP has only 8 bytes of header overhead -- used where speed beats reliability
A socket = IP address + port number; a connection = 5-tuple (protocol, src IP, src port, dst IP, dst port)