The two transport protocols make opposite trade-offs. TCP is a reliable, ordered stream; UDP is fast, unreliable datagrams. Understanding the difference is the single most important choice in game networking.
TCP gives you a connection that behaves like a reliable, ordered pipe: bytes you write come out the other end in order, with none missing, and it handles retransmission, ordering, flow control, and congestion control for you. This is perfect for web pages, logins, and chat — anything where correctness matters more than the last few milliseconds. The cost is that reliability adds latency.
TCP = a reliable, ordered, connection-based STREAM of bytes.
✓ every byte arrives, exactly once, IN ORDER (retransmits lost data)
✓ flow control (don't overwhelm the receiver)
✓ congestion control (back off when the network is busy)
- it's a STREAM, not messages: you must frame your own message boundaries
- reliability costs LATENCY: a lost packet stalls everything behind it
("head-of-line blocking") until it's retransmitted
Great for: logins, chat, matchmaking, purchases, downloads — correctness first.UDP sends independent datagrams (packets) with almost no overhead: no connection, no ordering, no retransmission, no congestion control. Packets may be lost, duplicated, or arrive out of order — and that is a feature for real-time games, because a dropped position update is stale anyway; you would rather have the next one than wait for the old one. UDP is the foundation of nearly all fast-paced multiplayer.
UDP = fire-and-forget DATAGRAMS (independent packets), minimal overhead.
✓ no connection setup, tiny header -> low latency
✓ packets are independent -> a lost one doesn't block the next
- NO guarantees: packets may be LOST, DUPLICATED, or REORDERED
- no flow/congestion control -> you build what you need on top
Why games love it: a dropped position update is already STALE — you'd rather
get the NEXT update than wait for TCP to resend the old one. Real-time games
build their OWN lightweight reliability on UDP (only for what needs it).
Great for: player movement, physics state, anything real-time.The rule for games: use UDP for the real-time simulation (movement, actions, world state) where fresh data beats complete data, and TCP for out-of-band reliability (login, chat, matchmaking, transactions) where you must not lose anything. Many games run both. Some modern stacks use protocols like QUIC or WebRTC data channels that give UDP-like speed with optional reliability — but the underlying trade-off is the same.
The game networking rule of thumb:
UDP for the real-time SIMULATION — freshness beats completeness:
player input, movement, physics/world state, hit events.
(you add reliability ONLY for the few messages that need it.)
TCP for OUT-OF-BAND reliability — must not lose it, latency is fine:
login/auth, chat, matchmaking, inventory/purchases, downloads.
Many games run BOTH sockets at once. Modern options: QUIC (UDP + optional
reliable streams), WebRTC data channels (browser games), ENet/GameNetworkingSockets
(UDP + reliability layers). Same trade-off underneath: pick freshness vs. guarantees.Because UDP guarantees nothing, a game protocol adds back exactly the guarantees it needs and no more. You sequence-number packets to detect loss and reordering, acknowledge important messages and resend only those, and often send state as periodic snapshots so a lost packet is simply corrected by the next one. Building this thin, selective reliability layer is a core skill covered in depth by the Game Networking course.
# a minimal reliability header on top of UDP (games build this):
import struct
# each packet carries a sequence number + an ack of what we've received
def make_header(seq: int, ack: int) -> bytes:
return struct.pack("!II", seq, ack) # 2 x uint32
# receiver: detect loss/reorder by gaps/order in 'seq';
# sender: resend only messages flagged RELIABLE that weren't acked.
# unreliable state (position) is just re-sent fresh next tick -> no resend needed.
# key idea: add back ONLY the guarantees you need (ordering for some messages,
# reliability for a few), keeping UDP's low latency for everything else.
# (full treatment: the Game Networking course.)