1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# Send/receive UDP multicast packets (SGI)
# After /usr/people/4Dgifts/examples/network/mcast.c
# Usage:
# mcast -s (sender)
# mcast -b (sender, using broadcast instead multicast)
# mcast (receivers)
MYPORT = 8123
MYGROUP = '225.0.0.250'
import sys
import time
import struct
import regsub
from socket import *
from SOCKET import *
from IN import * # Local module, SGI specific!!!
sender = sys.argv[1:]
s = socket(AF_INET, SOCK_DGRAM)
if sender:
if sys.argv[1] == '-b':
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
mygroup = '<broadcast>'
else:
mygroup = MYGROUP
ttl = struct.pack('b', 1) # Time-to-live
s.setsockopt(IPPROTO_IP, IP_MULTICAST_TTL, ttl)
while 1:
data = `time.time()`
## data = data + (1400 - len(data)) * '\0'
s.sendto(data, (mygroup, MYPORT))
time.sleep(1)
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)
# Ugly: construct binary group address from MYGROUP converted to bytes
bytes = eval(regsub.gsub('\.', ',', MYGROUP))
grpaddr = 0
for byte in bytes: grpaddr = (grpaddr << 8) | byte
# 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`
|