Learning Python Network Programming

(Sean Pound) #1

Network Programming and Python


Most operating systems contain a copy of this list of services and their assigned port
numbers. On Linux, this is usually found at /etc/services, and on Windows this is
usually found at c:\windows\system32\drivers\etc\services. The complete list
can also be viewed online at http://www.iana.org/assignments/port-numbers.


TCP and UDP packet headers may also include a source port number. This is
optional for UDP, but mandatory for TCP. The source port number tells the receiving
application on the server where it should send replies to when sending data back
to the client. Applications can specify the source port that they wish to use, or if a
source port has not been specified for TCP, then one is assigned randomly by the
operating system when the packet is sent. Once the OS has a source port number, it
assigns it to the calling application and starts listening on it for a reply. If a reply is
received on that port, then the received data is passed to the sending application.


So, both TCP and UCP provide an end-to-end transport for the application data
through the provision of ports, and both of them employ the Internet Protocol
to get the data to the destination device. Now, let's look at their features.


UDP

UDP is documented as RFC 768. It is deliberately uncomplicated: it provides no
services other than those that we described in the previous section. It just takes
the data that we want to send, packetizes it with the destination port number
(and optional source port number), and hands it off to the local Internet Protocol
implementation for delivery. Applications on the receiving end see the data in the
same discrete chunks in which it was packetized.


Both IP and UDP are what are called connectionless protocols. This means that they
attempt to deliver their packets on a best effort basis, but if something goes wrong,
then they will just shrug their metaphorical shoulders and move on to delivering
the next packet. There is no guarantee that our packets will reach their destinations,
and no error notification if a delivery fails. If the packets do make it, then there is no
guarantee that they will do so in the same order as they were sent. It's up to a higher
layer protocol or the sending application to determine if the packets have arrived
and whether to handle any problems. These are protocols in the fire-and-forget style.


The typical applications of UDP are internet telephony and video streaming.
DNS queries are also transported using UDP.


We'll now look at UDP's more dependable sibling, TCP, and then discuss the
differences, and why applications may choose to use one or the other.

Free download pdf