1992-09-08 23:19:55 +02:00
|
|
|
# Send/receive UDP multicast packets (SGI)
|
|
|
|
# After /usr/people/4Dgifts/examples/network/mcast.c
|
|
|
|
# Usage:
|
|
|
|
# mcast -s (sender)
|
1992-09-24 12:33:16 +02:00
|
|
|
# mcast -b (sender, using broadcast instead multicast)
|
1992-09-08 23:19:55 +02:00
|
|
|
# mcast (receivers)
|
|
|
|
|
|
|
|
MYPORT = 8123
|
1992-09-24 12:33:40 +02:00
|
|
|
MYGROUP = '225.0.0.250'
|
1992-09-08 23:19:55 +02:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import struct
|
1992-09-24 12:33:40 +02:00
|
|
|
import regsub
|
1992-09-08 23:19:55 +02:00
|
|
|
from socket import *
|
|
|
|
from SOCKET import *
|
1992-09-24 12:33:40 +02:00
|
|
|
from IN import * # Local module, SGI specific!!!
|
1992-09-08 23:19:55 +02:00
|
|
|
|
1992-09-24 12:33:16 +02:00
|
|
|
sender = sys.argv[1:]
|
1992-09-08 23:19:55 +02:00
|
|
|
|
|
|
|
s = socket(AF_INET, SOCK_DGRAM)
|
|
|
|
|
|
|
|
if sender:
|
1992-09-24 12:33:16 +02:00
|
|
|
if sys.argv[1] == '-b':
|
|
|
|
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
|
|
|
|
mygroup = '<broadcast>'
|
|
|
|
else:
|
1992-09-24 12:33:40 +02:00
|
|
|
mygroup = MYGROUP
|
1992-09-24 12:33:16 +02:00
|
|
|
ttl = struct.pack('b', 1) # Time-to-live
|
|
|
|
s.setsockopt(IPPROTO_IP, IP_MULTICAST_TTL, ttl)
|
1992-09-08 23:19:55 +02:00
|
|
|
while 1:
|
1992-09-24 12:33:16 +02:00
|
|
|
data = `time.time()`
|
|
|
|
## data = data + (1400 - len(data)) * '\0'
|
1992-09-08 23:19:55 +02:00
|
|
|
s.sendto(data, (mygroup, MYPORT))
|
1992-09-24 12:33:16 +02:00
|
|
|
time.sleep(1)
|
1992-09-08 23:19:55 +02:00
|
|
|
else:
|
|
|
|
# Bind the socket to my port
|
|
|
|
s.bind('', MYPORT)
|
|
|
|
|
|
|
|
# Allow multiple copies of this program on one machine
|
|
|
|
s.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1) # (Not strictly needed)
|
|
|
|
|
1992-09-24 12:33:40 +02:00
|
|
|
# Ugly: construct binary group address from MYGROUP converted to bytes
|
|
|
|
bytes = eval(regsub.gsub('\.', ',', MYGROUP))
|
1992-09-08 23:19:55 +02:00
|
|
|
grpaddr = 0
|
1992-09-24 12:33:40 +02:00
|
|
|
for byte in bytes: grpaddr = (grpaddr << 8) | byte
|
1992-09-08 23:19:55 +02:00
|
|
|
|
|
|
|
# Construct struct mreq from grpaddr and ifaddr
|
|
|
|
ifaddr = INADDR_ANY
|
|
|
|
mreq = struct.pack('ll', grpaddr, ifaddr)
|
|
|
|
|
|
|
|
# Add group membership
|
|
|
|
s.setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP, mreq)
|
|
|
|
|
|
|
|
# Loop, printing any data we receive
|
|
|
|
while 1:
|
|
|
|
data, sender = s.recvfrom(1500)
|
|
|
|
while data[-1:] == '\0': data = data[:-1] # Strip trailing \0's
|
|
|
|
print sender, ':', `data`
|