TCP vs UDP
TCP vs UDP: Deep Architectural Comparison
At the transport layer of the internet protocol suite, two primary protocols govern how bytes are moved between computers: Transmission Control Protocol (TCP) and User Datagram Protocol (UDP).
Choosing between TCP and UDP requires evaluating trade-offs between reliability and delivery guarantees versus latency and protocol overhead.
1. Protocol Architecture & Characteristics
Direct Technical Comparison
| Metric | TCP (SOCK_STREAM) | UDP (SOCK_DGRAM) |
|---|---|---|
| Connection Model | Connection-oriented (Requires 3-way handshake) | Connectionless (No handshake, no teardown) |
| Delivery Guarantee | Guaranteed: Lost packets are retransmitted | Unreliable: Packets may drop silently |
| Ordering | Strictly In-Order: Packets reassembled sequentially | Unordered: Packets may arrive out of sequence |
| Data Boundaries | Continuous byte stream (No message boundaries) | Discrete Datagrams (Preserves packet boundaries) |
| Flow & Congestion | Implements sliding window flow & congestion control | No congestion or rate control |
| Header Overhead | 20 to 60 bytes | Exactly 8 bytes |
| Speed / Latency | Higher latency (handshakes, ACK round-trips) | Lowest possible latency (immediate dispatch) |
2. The Framing Problem: Stream vs Datagram
"A", "B", and "C", the server may receive them merged into a single chunk "ABC" or split across arbitrary fragments like "AB" and "C". Application protocols over TCP must implement framing (such as newline delimiters \n or length prefixes).UDP, by contrast, preserves message boundaries: one sendto() call corresponds directly to exactly one recvfrom() call on the receiving socket.
3. Implementing UDP in Python (sendto and recvfrom)
Because UDP is connectionless, a UDP server does not call listen() or accept(). It simply binds to a port and reads datagrams directly, identifying the sender via recvfrom():
The UDP Server
Visual Architecture & Process Flow
How data and code flow step-by-step
The UDP Client
Visual Architecture & Process Flow
How data and code flow step-by-step
4. Industry Decision Matrix
5. Architectural Summary Table
| Requirement | Preferred Protocol | Rationale |
|---|---|---|
| Web APIs & HTTP | TCP | Dropped bytes mean corrupted JSON payloads or missing files |
| Live Audio / Video Streaming | UDP | A dropped audio packet for 10ms is unnoticeable; waiting for retransmission causes lag |
| Multiplayer Game Position Sync | UDP | Stale coordinate data is useless; fresh positions arrive immediately |
| DNS Lookups | UDP | Query and response fit in single datagrams; faster than 3-way handshake |
| File Transfer (FTP / SFTP) | TCP | Even a single flipped or missing bit corrupts the executable or archive |
Multiple Choice Questions
1.
What type of socket is created using socket.socket(socket.AF_INET, socket.SOCK_DGRAM)? A. TCP Stream Socket B. UDP Datagram Socket C. Raw ICMP Socket D. Unix Domain Socket
SOCK_DGRAM creates a datagram-oriented socket, which utilizes the User Datagram Protocol (UDP).2.
Why does a UDP server NOT need to invoke .listen() or .accept() methods? A. UDP is connectionless; packets are addressed and routed individually without negotiating a persistent session handshake. B. UDP only works on client machines. C. .accept() is deprecated in Python 3. D. The operating system kernel accepts UDP connections automatically.
recvfrom().3.
What is the "framing problem" inherent in TCP that does not exist in UDP? A. TCP packets cannot exceed 64 bytes. B. TCP is a continuous byte stream without message delimiters, meaning distinct send() calls can arrive coalesced together or fragmented arbitrarily. C. TCP cannot transmit text files. D. TCP does not support Unicode.
4.
What method on a UDP socket is used to transmit data to a specific remote destination? A. sock.connect() B. sock.sendall() C. sock.sendto(data, address) D. sock.push()
sock.sendto(data, (host, port)) transmits a datagram directly to the specified destination tuple without requiring prior connection establishment.5.
Which application is best suited for UDP rather than TCP? A. Secure Shell (SSH) remote terminal session. B. Real-time multiplayer game player position updates. C. Online banking ledger transfers. D. Downloading an operating system ISO image.
Building a Chat Server
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Socket Programming Basics | Building a Chat Server |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.