UDP Port Numbers and Multiplexing
What you'll learn: How UDP uses 16-bit port numbers to enable multiple applications to share the same network connection on a single host.
The Core Challenge
Imagine your computer running several network applications simultaneously—a web browser, a video call, and a game. All these applications use your single network interface to send and receive data. How does the operating system know which incoming packet belongs to which application? This is the multiplexing problem.
Port Numbers: Application Addresses
UDP solves this with port numbers—16-bit identifiers (ranging from 0 to 65,535) that act like apartment numbers in a building. Your computer's IP address is the building address, and port numbers identify specific "apartments" (applications) inside.
When an application wants to use UDP, it binds to a specific port number. Well-known services use standard ports (DNS uses port 53, DHCP uses port 67/68), while client applications typically use ephemeral ports (temporary ports above 49,152).
Multiplexing and Demultiplexing
Multiplexing happens when your computer sends data: UDP takes messages from different applications, adds the source port number to each datagram header, and sends them out through the network interface.
Demultiplexing is the reverse: when UDP datagrams arrive, the OS examines the destination port number in the UDP header and delivers each datagram to the correct application listening on that port.
The combination of (destination IP, destination port) forms a socket address that uniquely identifies which application should receive the data. Unlike TCP, UDP doesn't track connections—it simply routes each datagram independently based on the destination port.
Real-World Analogy
Think of UDP ports like mailbox slots in an apartment building. The postal carrier (network) delivers mail to the building (IP address), then sorts each letter into the correct mailbox slot (port number) so each resident (application) gets their own mail.
Key Takeaway: UDP uses 16-bit port numbers to multiplex multiple applications over a single network interface, directing incoming datagrams to the correct application by matching the destination port number.