09.11.2016 Views

Foundations of Python Network Programming 978-1-4302-3004-5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 2 ■ UDP<br />

• SO_DONTROUTE: Only be willing to send packets that are addressed to hosts on<br />

subnets to which this computer is connected directly. My laptop, for example, at<br />

this moment would be willing to send packets to the networks 127.0.0.0/8 and<br />

192.168.5.0/24 if this socket option were set, but would not be willing to send<br />

them anywhere else.<br />

• SO_TYPE: When passed to getsockopt(), this returns to you regardless <strong>of</strong> whether a<br />

socket is <strong>of</strong> type SOCK_DGRAM and can be used for UDP, or it is <strong>of</strong> type SOCK_STREAM<br />

and instead supports the semantics <strong>of</strong> TCP (see Chapter 3).<br />

The next chapter will introduce some further socket options that apply specifically to TCP sockets.<br />

Broadcast<br />

If UDP has a superpower, it is its ability to support broadcast: instead <strong>of</strong> sending a packet to some<br />

specific other host, you can point it at an entire subnet to which your machine is attached and have the<br />

physical network card broadcast the packet so that all attached hosts see it without its having to be<br />

copied separately to each one <strong>of</strong> them.<br />

Now, it should be immediately mentioned that broadcast is considered passé these days, because a<br />

more sophisticated technique called “multicast” has been developed, which lets modern operating<br />

systems take better advantage <strong>of</strong> the intelligence built into many networks and network interface<br />

devices. Also, multicast can work with hosts that are not on the local subnet, which is what makes<br />

broadcast unusable for many applications! But if you want an easy way to keep something like gaming<br />

clients or automated scoreboards up-to-date on the local network, and each client can survive the<br />

occasional dropped packet, then UDP broadcast is an easy choice.<br />

Listing 2–4 shows an example <strong>of</strong> a server that can receive broadcast packets and a client that can<br />

send them. And if you look closely, you will see that there is pretty much just one difference between this<br />

listing and the techniques we were using in previous listings: before using this socket object, we are<br />

using its setsockopt() method to turn on broadcast. Aside from that, the socket is used quite normally<br />

by both server and client.<br />

Listing 2–4. UDP Broadcast<br />

#!/usr/bin/env python<br />

# <strong>Foundations</strong> <strong>of</strong> <strong>Python</strong> <strong>Network</strong> <strong>Programming</strong> - Chapter 2 - udp_broadcast.py<br />

# UDP client and server for broadcast messages on a local LAN<br />

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)<br />

s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)<br />

MAX = 65535<br />

PORT = 1060<br />

if 2

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!