Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
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.
| 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) |
"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.
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():
How data and code flow step-by-step
How data and code flow step-by-step
| 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 |
UDP is connectionless and best-effort: packets can be dropped, duplicated, or arrive out of order. Never use raw UDP for data requiring strict integrity without an application-level ACK protocol.
Sending UDP packets larger than the network Path MTU (typically 1500 bytes) causes IP packet fragmentation, drastically increasing packet loss rates.
Send a high-speed UDP datagram using socket.SOCK_DGRAM and measure the round-trip latency.
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).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().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.
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.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.
In the next lesson, we will continue your advanced Python journey with Building a Chat Server (10: Networking with Python).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Socket Programming Basics | Building a Chat Server |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.