100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

TCP vs UDP

Compare TCP and UDP across reliability, ordering, connection state, overhead, and typical use cases.

Transport LayerBeginner9 min readJul 8, 2026
Analogies

Introduction

TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are the two workhorse protocols of the transport layer. They both deliver data between processes using ports, but they make fundamentally different trade-offs between reliability and speed, and choosing the right one is a key design decision for any networked application.

🏏

Cricket analogy: Choosing between a careful Test match defensive innings, TCP-like reliability, and a blistering T20 slog, UDP-like speed, is a key strategic decision a captain makes based on what the situation demands.

Explanation

TCP is connection-oriented: before any data is exchanged, the two endpoints perform a handshake to establish a shared connection state, and this state is maintained (and later torn down) throughout the conversation. TCP guarantees reliable delivery - it detects lost segments via acknowledgments and retransmits them - and guarantees in-order delivery, buffering and reordering segments as needed so the application always receives a clean, ordered byte stream. This reliability comes at a cost: TCP headers are larger (20+ bytes), and the handshake, acknowledgments, and retransmission logic add latency and overhead.

🏏

Cricket analogy: TCP is like a Test match toss and pre-match briefing, the handshake, that must happen before play, with every run meticulously verified by scorers and reviewed if disputed, ACKs and retransmission, which is thorough but slower than a quick gully game.

UDP is connectionless: there is no handshake and no persistent connection state between sender and receiver. UDP simply sends discrete datagrams and makes no promises about delivery, ordering, or duplication - packets may arrive out of order, be duplicated, or simply never arrive, and it is up to the application to handle that if it cares. In exchange, UDP has a much smaller header (8 bytes) and far less overhead, making it faster and lower-latency for applications that can tolerate some loss or that implement their own reliability logic.

🏏

Cricket analogy: UDP is like a quick backyard cricket game where kids just start bowling without agreeing on rules first, no handshake, and if someone misses a catch or a run is miscounted, nobody stops to redo it - the game just keeps moving fast.

Example

python
import socket

# TCP: connection-oriented, reliable, ordered - good for HTTP, file transfer
tcp_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_sock.connect(("example.com", 80))
tcp_sock.sendall(b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")

# UDP: connectionless, no delivery/order guarantees - good for DNS queries
udp_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_sock.sendto(b"\xaa\xaa\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00", ("8.8.8.8", 53))
response, _ = udp_sock.recvfrom(512)

Analysis

Notice that the TCP client calls connect() before sending anything - this triggers the underlying three-way handshake and establishes connection state. The UDP client, by contrast, calls sendto() directly with no prior setup; each datagram is independent. This maps directly to real-world use: HTTP/HTTPS and file transfer protocols use TCP because a corrupted webpage or a truncated file download is unacceptable, so the retransmission and ordering guarantees matter more than the extra latency. DNS queries, live video streaming, and online gaming favor UDP because a single lost or late packet (a dropped audio frame, a stale game-position update) is often better discarded than retransmitted late, and the lower overhead keeps latency down.

🏏

Cricket analogy: A Test match requires the toss and team sheets to be finalized before a ball is bowled, like connect(), because a wrongly recorded scorecard is unacceptable, whereas a quick informal net session just starts hitting balls, like sendto(), since a mistimed shot doesn't need fixing before the next one.

Key Takeaways

  • TCP is connection-oriented (handshake + maintained state); UDP is connectionless (no handshake, no persistent state).
  • TCP guarantees reliable, in-order delivery via acknowledgments and retransmission; UDP offers no such guarantees.
  • TCP has higher overhead (larger header, handshake, ACKs); UDP is lightweight with an 8-byte header.
  • TCP suits HTTP/HTTPS and file transfer; UDP suits DNS queries, video streaming, and online gaming.
  • Choosing TCP vs UDP is a trade-off between guaranteed correctness and speed/low latency.

Practice what you learned

Was this page helpful?

Topics covered

#Python#ComputerNetworksStudyNotes#ComputerNetworks#TCPVsUDP#TCP#UDP#Explanation#Example#Networking#StudyNotes#SkillVeris