Foundations of Python Network Programming

(WallPaper) #1
17

Chapter 2

UDP


The previous chapter described modern network hardware as supporting the transmission of short messages called
packets, which are usually no larger than a few thousand bytes. How can these tiny individual messages be combined
to form the conversations that take place between a web browser and server or between an e-mail client and your
ISP’s mail server?
The IP protocol is responsible only for attempting to deliver each packet to the correct machine. Two additional
features are usually necessary if separate applications are to maintain conversations, and it is the job of the protocols
built atop IP to provide these features.


•    The many packets traveling between two hosts need to be labeled so that the web packets
can be distinguished from e-mail packets and so that both can be separated from any other
network conversations in which the machine is engaged. This is called multiplexing.

•    All of the damage that can occur to a stream of packets traveling separately from one host
to another needs to be repaired. Missing packets need to be retransmitted until they arrive.
Packets that arrive out of order need to be reassembled into the correct order. Finally,
duplicate packets need to be discarded so that no information in the data stream gets
repeated. This is known as providing a reliable transport.

This book dedicates a chapter to each of the two major protocols used atop IP.
The first, the User Datagram Protocol (UDP), is documented in this chapter. It solves only the first of the two
problems outlined previously. It provides port numbers, as described in the next section, so that the packets destined
for different services on a single machine can be properly demultiplexed. Nevertheless, network programs using UDP
must still fend for themselves when it comes to packet loss, duplication, and ordering.
The second, the Transmission Control Protocol (TCP), solves both problems. It both incorporates port numbers
using the same rules as UDP and offers ordered and reliable data streams that hide from applications the fact that
the continuous stream of data has in fact been chopped into packets and then reassembled at the other end. You will
learn about using TCP in Chapter 3.
Note that a few rare and specialized applications, such as multimedia being shared among all hosts on a LAN, opt
for neither protocol and choose instead to create an entirely new IP-based protocol that sits alongside TCP and UDP
as a new way of having conversations across an IP network. This not only is unusual but, being a low-level operation,
is unlikely to be written in Python, so you will not explore protocol engineering in this book. The closest approach
made to raw packet construction atop IP in this book is the “Building and Examining Packets” section near the end of
Chapter 1, which builds raw ICMP packets and receives an ICMP reply.
I should admit up front that you are unlikely to use UDP in any of your own applications. If you think UDP is a
great fit for your application, you might want to look into message queues (see Chapter 8). Nonetheless, the exposure
that UDP gives you to raw packet multiplexing is an important step to take before you can be ready to learn about TCP
in Chapter 3.

Free download pdf