UDP datagrams
Send independent datagrams with no connection and see how UDP differs from TCP.
import socket
receiver = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
receiver.bind(("127.0.0.1", 0))
address = receiver.getsockname()
sender = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sender.sendto(b"one", address)
sender.sendto(b"two", address)
first, _ = receiver.recvfrom(1024)
second, _ = receiver.recvfrom(1024)
sender.close()
receiver.close()
print(first, second)b'one' b'two'
Explanation
UDP is a connectionless transport protocol. There is no handshake: a sender simply sends a datagram to an address and port. Each datagram is delivered, if it is delivered at all, as one whole unit, so message boundaries are preserved, unlike TCP's byte stream.
UDP itself provides no guarantees beyond an optional checksum. Datagrams can be lost, duplicated, or arrive out of order, and there is no flow or congestion control at the UDP level. That makes it a good fit for DNS lookups, real-time audio and video, and games, where speed matters more than perfect delivery, and for protocols such as QUIC that build their own reliability on top.
The example sends two datagrams over the loopback interface. On loopback they arrive in order and intact, which is why the output is predictable, but that is a property of this local setup. Real networks may reorder or drop packets, and an application using UDP has to handle that itself, for example with sequence numbers, acknowledgements, and timeouts.
Explore the dry run
import socket receiver = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)receiver.bind(("127.0.0.1", 0))address = receiver.getsockname() sender = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)sender.sendto(b"one", address)sender.sendto(b"two", address) first, _ = receiver.recvfrom(1024)second, _ = receiver.recvfrom(1024)sender.close()receiver.close()print(first, second)Follow the example
Start to inspect the source, values, and output. These prepared steps explain the published sample; grouped loop steps describe multiple iterations.
View the complete dry-run table
| Step / line | What happens | State after step | Output so far |
|---|---|---|---|
| The receiver binds a UDP socket to a free local port. No listen or accept is needed. | — | — | |
| Each sendto is an independent datagram addressed to the receiver. There is no connection. | — | — | |
| recvfrom returns exactly one datagram and the sender's address. | — | — | |
| Message boundaries are preserved: two sends produce two separate receives. | — | b'one' b'two' |
Cost and common mistakes
Each send and receive is O(n) in the datagram size. UDP adds no connection setup.
- UDP does not guarantee delivery or order. Do not assume that a datagram you sent will arrive.
- A recvfrom buffer smaller than the datagram silently truncates it. Size the buffer for the largest message you expect.
- Large datagrams can be fragmented or dropped by the path. Keep payloads small, commonly under roughly 1200 bytes for the Internet.
Try it yourself
Which protocol would you choose for a live video call, and why?
Reveal answer and reasoning
Typically UDP-based transport. A late frame is worthless, so retransmitting old data (as TCP would, with head-of-line blocking) hurts more than skipping it.