Streams vs Datagrams — Heavy vs Lightweight
People call TCP "heavyweight" and UDP "lightweight". The difference is not mainly about header bytes. It is about how much each protocol does for you, and how much you must do yourself in return.
The rule
TCP carries a stream you must slice; UDP carries messages already cut.
TCP is heavy because it does a lot on your behalf:
- a handshake, and per-connection state kept in the kernel,
- a header of 20 bytes or more,
- retransmission timers,
- flow control, so it never overwhelms the receiver, and congestion control, so it never overwhelms the network,
- and a byte stream, which keeps the order of your bytes but not the edges between your messages.
UDP is light because it does almost nothing: no state, an 8-byte header, no timers, no controls. But it keeps message edges, so one send is one datagram.
The stream point catches everyone once. The lobby sends two chat lines, "hi" and "gg", with two separate sends. The player's program may read higg in one go, or h and then igg. That is not a bug; TCP never promised to keep edges. The fix is framing: put each message's length in front of it.
two sends: "hi" "gg"
one possible read: h i g g ← where does one message end?
with framing: 02 h i 02 g g ← read the length, then that many bytesEach protocol has its own word for its unit: TCP sends segments, UDP sends datagrams, and IP carries either one inside a packet. Everyday speech calls all of them packets.
Only UDP can broadcast. One send to the special address 255.255.255.255 reaches every machine on the local network, and Manja Arena's LAN-party mode uses it to find servers in the same room. TCP is always one sender to one receiver. Routers do not pass broadcasts on to the wider internet, so an online match server still sends a separate copy to each of its 16 players.
The shortcut
Count what each protocol does for you: TCP = order + delivery + pacing, paid for with state and waiting. UDP = none of it, so you choose exactly what to build.
Broadcast the question
A client sends one UDP datagram, "any Manja Arena servers here?", to 255.255.255.255 on port 7778.
Drills
Answer fast — trust the rule.
The lobby sends "hi" and then "gg" as two sends over TCP. One read on the other side returns higg. What is going on?
What is the unit of data called in TCP, and in UDP?
Which can reach every machine on a home network with a single send?
With no options, how many bytes of IPv4 plus transport headers does each packet carry over TCP, and over UDP?
What is the difference between flow control and congestion control?
Remember
- TCP carries a stream you must slice; UDP carries messages already cut.
- TCP = order + delivery + pacing, paid for with state and waiting; UDP = none of it, so you choose.
- Trap: reading a TCP stream as if each read were one message. Always frame.
