1 from twisted.internet.protocol import DatagramProtocol
\r
2 from twisted.internet import reactor
\r
5 class MulticastPingPong(DatagramProtocol):
\r
7 def startProtocol(self):
\r
9 Called after protocol has started listening.
\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
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
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