Quick Summary: At Layer 4 (Transport Layer) of the OSI model, TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are the two core protocols that route data between network applications. TCP is a connection-oriented, reliable protocol that guarantees in-order, error-checked delivery via a 3-way handshake and automatic retransmission. UDP is a connectionless, lightweight datagram protocol that prioritizes speed and minimal latency over reliability, allowing packet loss in exchange for instant real-time throughput.
Every internet interaction depends on this tradeoff: downloading an executable file or loading a web page mandates TCP's 100% data fidelity, whereas watching a live football broadcast, making a VoIP call, or playing a competitive FPS game demands UDP's low latency.
Defined in RFC 793, TCP establishes an end-to-end dedicated virtual connection before any application data is sent. It guarantees that:
SYN → SYN-ACK → ACK).Defined in RFC 768, UDP is a minimal, connectionless transport protocol that operates on a "best-effort" delivery model. Its characteristics include:
Comparison Parameter |
TCP (Transmission Control Protocol) |
UDP (User Datagram Protocol) |
|---|---|---|
Connection Model |
Connection-Oriented: Requires 3-way handshake prior to data exchange. |
Connectionless: Fire-and-forget; transmits immediately without establishing a session. |
Reliability Guarantee |
Guaranteed Delivery: All lost or damaged packets are automatically retransmitted. |
Best-Effort: No retransmissions; packets can be lost, duplicated, or dropped. |
Data Ordering |
Strictly In-Order: Bytes are sequenced and assembled in their original transmitted sequence. |
Unordered: Packets arrive in whichever order the network routes deliver them. |
Header Size |
20 to 60 Bytes: Contains sequence numbers, ACK numbers, control flags, and window sizing. |
Fixed 8 Bytes: Only Source Port, Destination Port, Length, and Checksum. |
Speed & Latency |
Slower: Connection setup, acknowledgments, and congestion control add latency. |
Fastest: Near-zero latency overhead; ideal for real-time applications. |
Flow & Congestion Control |
Yes: Implements dynamic sliding window and congestion avoidance algorithms. |
No: Sends data as fast as the application generates it, regardless of network load. |
Broadcast & Multicast |
Unicast Only: Point-to-point connection between exactly one sender and one receiver. |
Unicast, Multicast, & Broadcast: Can send a single packet to multiple recipients simultaneously. |
Common Protocols |
HTTP/HTTPS (Web), SSH, FTP, SMTP (Email), MySQL/PostgreSQL. |
DNS, DHCP, VoIP (SIP/RTP), Video Streaming (WebRTC), NTP, Online Gaming. |
Understanding the internals of packet headers illustrates why UDP is so lightweight and why TCP provides bulletproof reliability:
================================================================================
TCP Header Structure (20 - 60 Bytes):
--------------------------------------------------------------------------------
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 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Data | |U|A|P|R|S|F| |
| Offset| Reserved |R|C|S|S|Y|I| Window |
| | |G|K|H|T|N|N| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Checksum | Urgent Pointer |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options | Padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
================================================================================
UDP Header Structure (Strictly 8 Bytes Fixed):
--------------------------------------------------------------------------------
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 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1. Which flags are exchanged during the initial TCP 3-Way Handshake?
A) PSH → RST → FIN
B) SYN → SYN-ACK → ACK
C) URG → ACK → FIN
D) ECHO → REPLY → ACK
2. What happens when a UDP packet is corrupted or dropped in transit?
A) The sender's kernel automatically retransmits it
B) The router requests an ICMP retransmission
C) The packet is silently discarded with no retransmission
D) The connection resets
3. Why does standard DNS resolution prefer UDP port 53 for queries?
A) Because DNS records require 256-bit encryption
B) Because a single request and response fit inside one packet, avoiding handshake latency
C) Because TCP cannot route across the public Internet
D) Because UDP prevents DDoS attacks
Answer: Because TCP guarantees strict in-order delivery, if a single packet is delayed or dropped in the network, all subsequent packets already received must sit in the buffer and wait until the missing packet is retransmitted. This phenomenon delays the entire application stream.
Answer: Yes, by implementing reliability at Layer 7 (Application Layer). Protocols like Google's QUIC (HTTP/3), TFTP, and proprietary gaming engines run on top of UDP but maintain their own custom sequence numbers, selective acknowledgments, and retransmission logic.
Answer: DNS switches from UDP to TCP when the response payload exceeds 512 bytes (or EDNS0 limits), during DNS zone transfers between primary and secondary name servers (AXFR/IXFR), or when DNSSEC digital signatures exceed standard datagram limits.
Answer: TCP maintains a Congestion Window (cwnd). It starts in Slow Start (exponential window growth), transitions to Congestion Avoidance (additive increase linear growth), and rapidly throttles throughput (multiplicative decrease) upon detecting packet loss.
Answer: TCP requires a dedicated point-to-point stateful session with synchronized sequence numbers between exactly two endpoints. Tracking acknowledgments and retransmissions for millions of recipients simultaneously is architecturally incompatible with TCP's design.
Explore synchronized companion classes across our networking and web engineering series:
192.168.1.10:8080). TCP uses Stream Sockets (SOCK_STREAM), while UDP uses Datagram Sockets (SOCK_DGRAM).