Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bd07b31

Browse files
committed
Restructured into several subroutines.
1 parent 2197479 commit bd07b31

1 file changed

Lines changed: 57 additions & 19 deletions

File tree

Demo/sockets/mcast.py

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,23 @@
1414
import regsub
1515
from socket import *
1616
from SOCKET import *
17-
from IN import * # Local module, SGI specific!!!
17+
from IN import * # SGI specific!!! (Sorry)
1818

19-
sender = sys.argv[1:]
2019

21-
s = socket(AF_INET, SOCK_DGRAM)
20+
# Main program
21+
def main():
22+
flags = sys.argv[1:]
23+
#
24+
if flags:
25+
sender(flags[0])
26+
else:
27+
receiver()
28+
2229

23-
if sender:
24-
if sys.argv[1] == '-b':
30+
# Sender subroutine (only one per local area network)
31+
def sender(flag):
32+
s = socket(AF_INET, SOCK_DGRAM)
33+
if flag == '-b':
2534
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
2635
mygroup = '<broadcast>'
2736
else:
@@ -33,27 +42,56 @@
3342
## data = data + (1400 - len(data)) * '\0'
3443
s.sendto(data, (mygroup, MYPORT))
3544
time.sleep(1)
36-
else:
37-
# Allow multiple copies of this program on one machine
38-
s.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1) # (Not strictly needed)
3945

40-
# Bind the socket to my port
41-
s.bind('', MYPORT)
4246

43-
# Construct binary group address from MYGROUP converted to bytes
44-
bytes = eval(regsub.gsub('\.', ',', MYGROUP))
47+
# Receiver subroutine (as many as you like)
48+
def receiver():
49+
# Open and initialize the socket
50+
s = openmcastsock(MYGROUP, MYPORT)
51+
#
52+
# Loop, printing any data we receive
53+
while 1:
54+
data, sender = s.recvfrom(1500)
55+
while data[-1:] == '\0': data = data[:-1] # Strip trailing \0's
56+
print sender, ':', `data`
57+
58+
59+
# Open a UDP socket, bind it to a port and select a multicast group
60+
def openmcastsock(group, port):
61+
# Import modules used only here
62+
import regsub
63+
import socket
64+
import struct
65+
from SOCKET import *
66+
from IN import *
67+
#
68+
# Create a socket
69+
s = socket.socket(AF_INET, SOCK_DGRAM)
70+
#
71+
# Allow multiple copies of this program on one machine
72+
# (not strictly needed)
73+
s.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1)
74+
#
75+
# Bind it to the port
76+
s.bind('', port)
77+
#
78+
# Look up multicast group address in name server
79+
# (doesn't hurt if it is already in ddd.ddd.ddd.ddd format)
80+
group = socket.gethostbyname(group)
81+
#
82+
# Construct binary group address
83+
bytes = eval(regsub.gsub('\.', ',', group))
4584
grpaddr = 0
4685
for byte in bytes: grpaddr = (grpaddr << 8) | byte
47-
86+
#
4887
# Construct struct mreq from grpaddr and ifaddr
4988
ifaddr = INADDR_ANY
5089
mreq = struct.pack('ll', grpaddr, ifaddr)
51-
90+
#
5291
# Add group membership
5392
s.setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP, mreq)
93+
#
94+
return s
5495

55-
# Loop, printing any data we receive
56-
while 1:
57-
data, sender = s.recvfrom(1500)
58-
while data[-1:] == '\0': data = data[:-1] # Strip trailing \0's
59-
print sender, ':', `data`
96+
97+
main()

0 commit comments

Comments
 (0)