HOME FAQ DOCS DOWNLOAD

### Navigation

- index
- next |
- previous |
- Twisted 15.1.0 documentation »
- Twisted Core »
- Developer Guides »

Go to the latest version of this document.

# UDP Networking

## Overview

Unlike TCP, UDP has no notion of connections. A UDP socket can receive datagrams from any server on the network and send datagrams to any host on the network. In addition, datagrams may arrive in any order, never arrive at all, or be duplicated in transit.

Since there are no connections, we only use a single object, a protocol, for each UDP socket. We then use the reactor to connect this protocol to a UDP transport, using the twisted.internet.interfaces.IReactorUDP reactor API.

## DatagramProtocol

The class where you actually implement the protocol parsing and handling will usually be descended from twisted.internet.protocol.DatagramProtocol or from one of its convenience children. The `DatagramProtocol` class receives datagrams and can send them out over the network. Received datagrams include the address they were sent from. When sending datagrams the destination address must be specified.

Here is a simple example:

    from twisted.internet.protocol import DatagramProtocol
    from twisted.internet import reactor

    class Echo(DatagramProtocol):

        def datagramReceived(self, data, (host, port)):
            print "received %r from %s:%d" % (data, host, port)
            self.transport.write(data, (host, port))

    reactor.listenUDP(9999, Echo())
    reactor.run()

As you can see, the protocol is registered with the reactor. This means it may be persisted if it’s added to an application, and thus it has startProtocol and stopProtocol methods that will get called when the protocol is connected and disconnected from a UDP socket.

The protocol’s `transport` attribute will implement the twisted.internet.interfaces.IUDPTransport interface. Notice that the `host` argument should be an IP address, not a hostname. If you only have the hostname use `reactor.resolve()` to resolve the address (see twisted.internet.interfaces.IReactorCore.resolve).

## Adopted Datagram Ports

It is also possible to add an existing `SOCK_DGRAM` file descriptor to the reactor using the adoptDatagramPort API.

Here is a simple example:

`adopt_datagram_port.py`

    import socket

    from twisted.internet.protocol import DatagramProtocol
    from twisted.internet import reactor

    class Echo(DatagramProtocol):
        def datagramReceived(self, data, (host, port)):
            print "received %r from %s:%d" % (data, host, port)
            self.transport.write(data, (host, port))

    portSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    # Make the port non-blocking and start it listening.
    portSocket.setblocking(False)
    portSocket.bind(('127.0.0.1', 9999))

    # Now pass the port file descriptor to the reactor
    port = reactor.adoptDatagramPort(
        portSocket.fileno(), socket.AF_INET, Echo())

    # The portSocket should be cleaned up by the process that creates it.
    portSocket.close()

    reactor.run()

Note

- You must ensure that the socket is non-blocking before passing its file descriptor to adoptDatagramPort.
- adoptDatagramPort cannot (currently) detect the family of the adopted socket so you must ensure that you pass the correct socket family argument.
- The reactor will not shutdown the socket. It is the responsibility of the process that created the socket to shutdown and clean up the socket when it is no longer needed.

## Connected UDP

A connected UDP socket is slightly different from a standard one as it can only send and receive datagrams to/from a single address. However this does not in any way imply a connection as datagrams may still arrive in any order and the port on the other side may have no one listening. The benefit of the connected UDP socket is that it may provide notification of undelivered packages. This depends on many factors (almost all of which are out of the control of the application) but still presents certain benefits which occasionally make it useful.

Unlike a regular UDP protocol, we do not need to specify where to send datagrams and are not told where they came from since they can only come from the address to which the socket is ‘connected’.

    from twisted.internet.protocol import DatagramProtocol
    from twisted.internet import reactor

    class Helloer(DatagramProtocol):

        def startProtocol(self):
            host = "192.168.1.1"
            port = 1234

            self.transport.connect(host, port)
            print "now we can only send to host %s port %d" % (host, port)
            self.transport.write("hello") # no need for address

        def datagramReceived(self, data, (host, port)):
            print "received %r from %s:%d" % (data, host, port)

        # Possibly invoked if there is no server listening on the
        # address to which we are sending.
        def connectionRefused(self):
            print "No one listening"

    # 0 means any port, we don't care in this case
    reactor.listenUDP(0, Helloer())
    reactor.run()

Note that `connect()`, like `write()` will only accept IP addresses, not unresolved hostnames. To obtain the IP of a hostname use `reactor.resolve()` , e.g.:

    from twisted.internet import reactor

    def gotIP(ip):
        print "IP of 'example.com' is", ip
        reactor.callLater(3, reactor.stop)

    reactor.resolve('example.com').addCallback(gotIP)
    reactor.run()

Connecting to a new address after a previous connection or making a connected port unconnected are not currently supported, but likely will be in the future.

## Multicast UDP

Multicast allows a process to contact multiple hosts with a single packet, without knowing the specific IP address of any of the hosts. This is in contrast to normal, or unicast, UDP, where each datagram has a single IP as its destination. Multicast datagrams are sent to special multicast group addresses (in the IPv4 range 224.0.0.0 to 239.255.255.255), along with a corresponding port. In order to receive multicast datagrams, you must join that specific group address. However, any UDP socket can send to multicast addresses.

`MulticastServer.py`

    from twisted.internet.protocol import DatagramProtocol
    from twisted.internet import reactor

    class MulticastPingPong(DatagramProtocol):

        def startProtocol(self):
            """
            Called after protocol has started listening.
            """
            # Set the TTL>1 so multicast will cross router hops:
            self.transport.setTTL(5)
            # Join a specific multicast group:
            self.transport.joinGroup("228.0.0.5")

        def datagramReceived(self, datagram, address):
            print "Datagram %s received from %s" % (repr(datagram), repr(address))
            if datagram == "Client: Ping":
                # Rather than replying to the group multicast address, we send the
                # reply directly (unicast) to the originating port:
                self.transport.write("Server: Pong", address)

    # We use listenMultiple=True so that we can run MulticastServer.py and
    # MulticastClient.py on same machine:
    reactor.listenMulticast(8005, MulticastPingPong(),
                            listenMultiple=True)
    reactor.run()

As with UDP, with multicast there is no server/client differentiation at the protocol level. Our server example is very simple and closely resembles a normal listenUDP protocol implementation. The main difference is that instead of `listenUDP`, listenMulticast is called with the port number. The server calls joinGroup to join a multicast group. A `DatagramProtocol` that is listening with multicast and has joined a group can receive multicast datagrams, but also unicast datagrams sent directly to its address. The server in the example above sends such a unicast message in reply to the multicast message it receives from the client.

`MulticastClient.py`

    from twisted.internet.protocol import DatagramProtocol
    from twisted.internet import reactor

    class MulticastPingClient(DatagramProtocol):

        def startProtocol(self):
            # Join the multicast address, so we can receive replies:
            self.transport.joinGroup("228.0.0.5")
            # Send to 228.0.0.5:8005 - all listeners on the multicast address
            # (including us) will receive this message.
            self.transport.write('Client: Ping', ("228.0.0.5", 8005))

        def datagramReceived(self, datagram, address):
            print "Datagram %s received from %s" % (repr(datagram), repr(address))

    reactor.listenMulticast(8005, MulticastPingClient(), listenMultiple=True)
    reactor.run()

Note that a multicast socket will have a default TTL (time to live) of 1. That is, datagrams won’t traverse more than one router hop, unless a higher TTL is set with setTTL. Other functionality provided by the multicast transport includes setOutgoingInterface and setLoopbackMode – see IMulticastTransport for more information.

## Broadcast UDP

Broadcast allows a different way of contacting several unknown hosts. Broadcasting via UDP sends a packet out to all hosts on the local network by sending to a magic broadcast address (`"<broadcast>"`). This broadcast is filtered by routers by default, and there are no “groups” like multicast, only different ports.

Broadcast is enabled by passing `True` to setBroadcastAllowed on the port. Checking the broadcast status can be done with getBroadcastAllowed on the port.

For a complete example of this feature, see `udpbroadcast.py`.

## IPv6

UDP sockets can also bind to IPv6 addresses to support sending and receiving datagrams over IPv6. By passing an IPv6 address to listenUDP‘s `interface` argument, the reactor will start an IPv6 socket that can be used to send and receive UDP datagrams.

`ipv6_listen.py`

    from twisted.internet.protocol import DatagramProtocol
    from twisted.internet import reactor

    class Echo(DatagramProtocol):
        def datagramReceived(self, data, addr):
            print "received %r from %s" % (data, addr)
            self.transport.write(data, addr)

    reactor.listenUDP(8006, Echo(), interface='::')
    reactor.run()