In an online game, a character's position is sent to the server several times per second.
Even if one position update is lost, a newer value arrives soon after, so it is better to send the latest value quickly than to resend an old one.
A payment approval message, on the other hand, must never be lost.
Some data is more important to deliver quickly than to deliver without fail, and
UDP
(User Datagram Protocol) is a common choice for that case.
In the previous post, we saw that UDP finds a socket using only the destination port.
This post looks at the structure of a UDP segment, the checksum that detects corruption, and the reasons for choosing UDP.
The checksum is a value that checks whether a segment was changed in transit.
The sender adds the segment in 16-bit units, adds back any carries, and computes a
one's complement sum
. The result goes into the checksum field.
The receiver adds everything in the same way, including the checksum field. If the result is all ones, 0xFFFF, it concludes there is no error.
The calculation also includes a virtual header called a pseudo-header, which contains the IP addresses.
So even a well-formed segment can be detected if it was delivered to the wrong address.
The receiver computes 0x6B61 + 0x949E, which gives 0xFFFF, so it treats the segment as error-free.
If one bit of 0x1C27 flips and becomes 0x1C37, the sum becomes 0x0010, which reveals the corruption.
๐
The checksum
detects corruption but does not repair it.
A segment that fails the check is silently discarded, and no retransmission happens.
Also, if several bits change in a way that produces the same sum, the error can slip through, so important data should be checked once more in the application.
In IPv4, a checksum field of 0 means that no checksum was used.
In IPv6, the UDP checksum is mandatory.
Because UDP does not create a connection, the sending code runs as-is even when no receiving socket exists.
The following example sends a datagram to a port where no server is listening.
When you run it, only sent 21 bytes is printed, and no error reports a delivery failure.
A successful send callback does not mean the peer received the data.
UDP has no connection setup stage that confirms whether the peer is ready.
It works on the same principle as the nc -u experiment in the transport layer services post.
Data is sent right away without a handshake, so there is no round trip spent establishing a connection as in TCP.
The header is small.
At 8 bytes, the overhead on each segment is low.
There is no congestion control.
The application decides the sending rate and timing itself.
Only the reliability you need is implemented.
Retransmission logic can be added only to the data that needs it.
Some well-known protocols run on top of UDP.
DNS consists of one short query and one response, so the cost of setting up a connection would be higher than the exchange itself.
In real-time voice and video, the latest frame matters more than a frame that arrives late.
HTTP/3's QUIC implements its own reliability and congestion control on top of UDP (RFC 9000 ยท QUICโ).
๐จโ๐ป
If the data you send over UDP needs retransmission, design
acknowledgments and retries
at the application level before switching to TCP.
Number each message, and if no acknowledgment arrives within a set time, send the same numbered message again.
With an 8-byte header, port numbers, and a checksum, UDP provides
the minimum delivery function
.
The checksum only detects corruption; UDP does not solve loss or reordering.
Because it has no connection setup or congestion control, UDP suits delay-sensitive services. If you need complete delivery, however, the application must implement that reliability itself.
The next post explains the principles for building