Imported Upstream version 12.1.0
[contrib/python-twisted.git] / doc / core / howto / listings / udp / MulticastServer.py
1 from twisted.internet.protocol import DatagramProtocol\r
2 from twisted.internet import reactor\r
3 \r
4 \r
5 class MulticastPingPong(DatagramProtocol):\r
6 \r
7     def startProtocol(self):\r
8         """\r
9         Called after protocol has started listening.\r
10         """\r
11         # Set the TTL>1 so multicast will cross router hops:\r
12         self.transport.setTTL(5)\r
13         # Join a specific multicast group:\r
14         self.transport.joinGroup("228.0.0.5")\r
15 \r
16     def datagramReceived(self, datagram, address):\r
17         print "Datagram %s received from %s" % (repr(datagram), repr(address))\r
18         if datagram == "Client: Ping":\r
19             # Rather than replying to the group multicast address, we send the\r
20             # reply directly (unicast) to the originating port:\r
21             self.transport.write("Server: Pong", address)\r
22 \r
23 \r
24 # We use listenMultiple=True so that we can run MulticastServer.py and\r
25 # MulticastClient.py on same machine:\r
26 reactor.listenMulticast(8005, MulticastPingPong(),\r
27                         listenMultiple=True)\r
28 reactor.run()\r