0
0
Computer Networksknowledge~6 mins

UDP datagram structure in Computer Networks - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine sending a quick message to a friend without waiting for a reply or confirmation. This is the problem UDP solves by providing a simple way to send data quickly over a network without extra checks.
Explanation
Source Port
This field holds the number of the sending application’s port. It tells the receiver where the message came from so it can reply if needed. If the sender does not expect a reply, this field can be zero.
The source port identifies the sender’s application endpoint.
Destination Port
This field contains the port number of the receiving application. It directs the incoming message to the correct program on the receiving device, like an address on a mailbox.
The destination port directs the datagram to the right application.
Length
This field shows the total size of the UDP datagram, including both header and data. It helps the receiver know where the message ends, ensuring the entire message is read.
Length tells how big the entire UDP datagram is.
Checksum
This is a simple error-checking code that helps detect if the data was corrupted during transmission. If the checksum does not match, the receiver knows the message may be damaged and can discard it.
Checksum helps detect errors in the datagram.
Data
This is the actual information or message being sent. It can be anything from a small command to a chunk of a file. UDP does not change or check this data beyond the checksum.
Data is the message content carried by the UDP datagram.
Real World Analogy

Imagine sending a postcard through the mail. The postcard has a return address, a destination address, a note about the size of the message, a stamp to check if it’s valid, and the message itself written on it.

Source Port → Return address on the postcard showing who sent it
Destination Port → Destination address on the postcard showing where it should go
Length → Note about how big the message is to ensure the whole postcard is received
Checksum → Stamp or mark that helps verify the postcard wasn’t damaged
Data → The written message on the postcard
Diagram
Diagram
┌───────────────┬───────────────┬───────────────┬───────────────┐
│ Source Port   │ Destination   │ Length        │ Checksum      │
│ (16 bits)     │ Port (16 bits)│ (16 bits)     │ (16 bits)     │
├───────────────────────────────────────────────────────────────┤
│ Data (variable length)                                         │
└───────────────────────────────────────────────────────────────┘
This diagram shows the UDP datagram header fields and the data section layout.
Key Facts
UDP Source PortA 16-bit number identifying the sender’s application port.
UDP Destination PortA 16-bit number identifying the receiver’s application port.
UDP LengthA 16-bit field indicating the total size of the UDP datagram.
UDP ChecksumA 16-bit error-checking code for the header and data.
UDP DataThe payload or message carried by the UDP datagram.
Common Confusions
Believing UDP guarantees message delivery like TCP.
Believing UDP guarantees message delivery like TCP. UDP does not guarantee delivery or order; it simply sends datagrams without confirmation.
Thinking the checksum fixes errors automatically.
Thinking the checksum fixes errors automatically. The checksum only detects errors; it does not correct them.
Assuming the source port is always set.
Assuming the source port is always set. The source port can be zero if the sender does not expect a reply.
Summary
UDP datagrams have a simple header with source port, destination port, length, and checksum fields.
The data section carries the actual message without extra delivery guarantees.
UDP is fast and lightweight but does not ensure messages arrive or are error-free.