The Datagram — Fire and Forget
When a match begins, Manja Arena's clients stop using the TCP lobby and start sending inputs to the match server, 20 times a second, over UDP. There is no handshake, no stream and no resending. What the game gets in exchange is speed, and full control over what happens when something goes missing.
The rule
One send is one datagram: it arrives whole, or not at all.
A datagram is a self-contained message. Every UDP datagram carries its full destination address and port, so there is nothing to set up first: the very first datagram can already carry game data.
- Whole. The boundaries you send are the boundaries that arrive. Send a 30-byte input and the receiver reads exactly those 30 bytes, never half of one and never two glued together. TCP, being a byte stream, makes no such promise.
- Or not at all. A datagram can be lost, and nobody tells you. Datagrams can also arrive out of order, and occasionally the same one arrives twice.
- No connection. The match server needs just one socket for all 16 players. Each read returns the data together with the sender's address and port, and that is how the server knows who sent what.
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # a UDP socket
sock.bind(("0.0.0.0", 7777)) # the match server's port
data, addr = sock.recvfrom(1500) # one whole datagram, plus who sent it
sock.sendto(b"welcome", addr) # reply straight to that addressIn theory a UDP datagram can carry almost 64 KB. In practice a large one is split into pieces by IP on the way, and losing any piece loses the whole datagram. Games keep each datagram small, well under 1,500 bytes; the IP section shows exactly how small.
The shortcut
For every UDP design question, picture the worst network the rules allow: each datagram independently lost, late, reordered or doubled. If your design still works on that network, it is ready.
Send
A player's client sends inputs numbered 41, 42 and 43, one per tick.
The classic mistake is testing only on a quiet office network, where UDP almost never loses anything, then shipping code that falls apart on a phone in a moving train. Test with a tool that drops, delays and reorders packets on purpose.
Drills
Answer fast — trust the rule.
How many round trips must pass before a client can send its first UDP datagram carrying game data?
A client sends two 30-byte datagrams to the server. Which outcome is possible?
The match server has a single UDP socket on port 7777. How does it tell which player sent a datagram?
Which of these does UDP NOT do?
Remember
- One send is one datagram: it arrives whole, or not at all.
- Shortcut: assume every datagram may be lost, late, reordered or doubled.
- Trap: assuming order or delivery. Numbering and resending are the game's job.
