The UDP Header and Its Checksum
A TCP header holds at least 20 bytes of machinery. A UDP header holds 8 bytes and almost no machinery at all. That emptiness is the point: it leaves the design of everything else to your game.
The rule
Eight bytes: two ports, a length, a checksum — the rest is yours.
0 16 32 bits
+-----------------+-----------------+
| source port | destination port|
+-----------------+-----------------+
| length | checksum |
+-----------------+-----------------+
| payload: the game's own bytes |
+-----------------------------------+- Source and destination port — 16 bits each, just as in TCP.
- Length — the size of the whole datagram in bytes, header included, so the smallest possible value is 8.
- Checksum — the same kind of 16-bit sum TCP uses, over the header, the payload and the IP addresses. A datagram that fails it is dropped. Over IPv4 the checksum is optional, and a value of 0 means "not computed". Over IPv6 it is required.
The checksum is a safety net for accidents, and a thin one. A 16-bit sum misses some combinations of errors, and anyone who wants to forge a datagram can simply compute a valid checksum for it. Protection against people comes from cryptography, never from checksums.
So Manja Arena starts every payload with a 10-byte header of its own:
| protocol id (2) | sequence (2) | ack (2) | ack bits (4) | messages ... |- Protocol id — a fixed value, the two letters "MA". Datagrams that reach port 7777 without it, such as port scans or stray traffic, are thrown away at once.
- Sequence — this packet's own number, so the other side can spot gaps and stale packets.
- Ack and ack bits — which of the other side's packets have arrived. The next lesson shows how they work.
- Messages — the inputs, snapshots and events themselves.
The shortcut
Size any datagram from the inside out: payload, then + 8 for UDP, then + 20 for IPv4 (or + 40 for IPv6). The UDP length field stops at the UDP layer; the IP header is not part of it.
Payload
The game header's 10 bytes plus one 6-byte input message make 16 bytes.
Drills
Answer fast — trust the rule.
How big is a UDP header?
A datagram carries 100 bytes of payload. What value is in its UDP length field?
Over IPv4, a UDP datagram arrives with its checksum field set to 0. What does that mean?
Why does every Manja Arena packet begin with the protocol id "MA"?
Can a valid UDP checksum prove that a datagram really came from a Manja Arena player?
Remember
- Eight bytes: two ports, a length, a checksum — the rest is yours.
- Size from the inside out: payload, + 8 for UDP, + 20 for IPv4.
- Trap: treating the checksum as proof of anything beyond "not accidentally damaged".
