Question: please use/fill in the given code. dont give me any other code. import argparse, socket, sys, threading from threading import Thread MAX_BYTES = 65535 my_ip_addr



import argparse, socket, sys, threading from threading import Thread MAX_BYTES = 65535 my_ip_addr = None class MulticastSocket(object): def __init__(self, mcast_addr,mcast_port): self.running = True self.mcast_addr = mcast_addr self.mcast_port = mcast_port #@ create the UDP socket and assign it to sock sock.setsockopt(socket.SOL_SOCKET socket.SO_REUSEADDR, 1) sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2) try: sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) except AttributeError: pass # Some systems don't support SO_REUSEPORT self.sock = sock super(MulticastSocket, self) __init__0 print("made multicast socket") class Receiving MulticastSocket(Multicast Socket, Thread): def __init__(self, mcast_addr,mcast_port): super(ReceivingMulticast Socket, self) __init__(mcast_addr.me ast_port) global my_up_addr intf=my_ip_addr self.sock.bind("self.mcast_port)) self.sock.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, socket.inet_aton(self.mcast_addr) + socket.inet_aton(intf)) #@ call settimeout on self.sock with an argument of 1 print("made receiving multicast socket") def run(self): print('Receiving at ('.format(my_ip_addr)) while (self.running): try: #@ call recvfrom on self.sock and assign the result to the pair of data, address so that we can access the data later except socket.timeout: continue # by doing this, we can terminate within 1 second when running is set to False text = data.demanda later except socket.timeout: continue # by doing this, we can terminate within 1 second when running is set to False text = data.decode('ascii') print('The client at() says {r}"format(address, text)) class Sending MulticastSocket(MulticastSocket, Thread): def _init__(self,mcast_addr,mcast_port): super(SendingMulticastSocket, self)-_init__(mcast_addr.mca st_port) try: self.input_function = raw_input except: self.input_function = input self.sock.connect((self.mcast_addr,self.mcast_port)) print("made sending multicast socket") print("Sending to )'.format((self.mcast_addr,self.mcast_port))) det run(self) text = self.input_function("What data should we multicast?") #send datagram while more to send while len(text): data = text.encode('ascii') # send datagram #@ call send on self.sock and pass in the data to be # get next datagram to send text = self.input_function("What data should we multicast?") self.running = False #main if _name__ == '_main_: parser = argparse.ArgumentParser(description='Send and receive UDP Multicast") parser.add_argument('multicast_address, help='multicast address") parser.add_argument('my_ip_address, help='my ip address") parser.add_argument('-p: metavar="PORT, type=int, default=1060, help='UDP port (default 1060)") args = parser.parse_args() mcast_addr = args.multicast_address my_ip_addr = args.my_ip_address mcast_port = args.p # set up threads for both sending and receiving receiving_thread = Receiving MulticastSocket(mcast_addr,mcast_port) sending_thread = Sending MulticastSocket(mcast_addr,mcast_port) sent sent try: self.input_function = raw_nput except: self.input_function = input self.sock.connect((self.mcast_addr,self.mcast_port)) print("made sending multicast socket") print('Sending to {}'.format((self.mcast_addr self.mcast_port))) def run(self): text = self.input_function("What data should we multicast?") # send datagram while more to send while len(text): data = text.encode('ascii') # send datagram #@ call send on self.sock and pass in the data to be # get next datagram to send text = self.input_function("What data should we multicast?") self.running = False # main if _name__ =='_main_": parser = argparse. ArgumentParser(description='Send and receive UDP Multicast") parser.add_argument('multicast_address, help="multicast address') parser.add_argument('my_ip_address', help='my ip address") parser.add_argument(-p) , metavar='PORT', type=int, default=1060, help='UDP port (default 1060)") args = parser.parse_args() mcast_addr = args.multicast_address my_ip_addr = args.my_ip_address mcast_port = args.p # set up threads for both sending and receiving receiving_thread = ReceivingMulticastSocket(mcast_addr,mcast_port) sending_thread = Sending MulticastSocket(mcast_addr,mcast_port) print("created threads") receiving_thread.start() sending_thread.start() print("started threads") sending_thread.join() receiving_thread.running = False receiving_thread.join() print("joined threads") MODULE DIRECTIONS 3: UDP Find a multicast group (see the resources). Have two or more programs, if at all possible running on different machines, where one sends to the multicast group and the other receives and vice versus. DESCRIPTION It should be possible for you and a bunch of your classmates to all agree on the same In this assignment, students write code that can multicast group and port number and all be send or receive UDP multicast packets. on servers on the same network (or even the same server) and all chat together over This is the same as UDP broadcast, except the UDP multicast through the use of this pro- IP addresses are within the range of multicast IP gram. I encourage you to use the UDP discus- addresses, and other network code adjustments sion area to coordinate times/servers/multicast- are done in order to accomplish multicast UDP groups/port-numbers to make this happen so appropriately (see additional resources). you can test with each other. Fill in the relevant portions of the provided skele- ton file to complete the assignment and make it function as stated elsewhere in this docu- OBJECTIVES ment. There may be a penalty if the file that you sub- To demonstrate knowledge of UDP multicast by mit is not derived from the provided skeleton file. writing code to do so import argparse, socket, sys, threading from threading import Thread MAX_BYTES = 65535 my_ip_addr = None class MulticastSocket(object): def __init__(self, mcast_addr,mcast_port): self.running = True self.mcast_addr = mcast_addr self.mcast_port = mcast_port #@ create the UDP socket and assign it to sock sock.setsockopt(socket.SOL_SOCKET socket.SO_REUSEADDR, 1) sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2) try: sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) except AttributeError: pass # Some systems don't support SO_REUSEPORT self.sock = sock super(MulticastSocket, self) __init__0 print("made multicast socket") class Receiving MulticastSocket(Multicast Socket, Thread): def __init__(self, mcast_addr,mcast_port): super(ReceivingMulticast Socket, self) __init__(mcast_addr.me ast_port) global my_up_addr intf=my_ip_addr self.sock.bind("self.mcast_port)) self.sock.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, socket.inet_aton(self.mcast_addr) + socket.inet_aton(intf)) #@ call settimeout on self.sock with an argument of 1 print("made receiving multicast socket") def run(self): print('Receiving at ('.format(my_ip_addr)) while (self.running): try: #@ call recvfrom on self.sock and assign the result to the pair of data, address so that we can access the data later except socket.timeout: continue # by doing this, we can terminate within 1 second when running is set to False text = data.demanda later except socket.timeout: continue # by doing this, we can terminate within 1 second when running is set to False text = data.decode('ascii') print('The client at() says {r}"format(address, text)) class Sending MulticastSocket(MulticastSocket, Thread): def _init__(self,mcast_addr,mcast_port): super(SendingMulticastSocket, self)-_init__(mcast_addr.mca st_port) try: self.input_function = raw_input except: self.input_function = input self.sock.connect((self.mcast_addr,self.mcast_port)) print("made sending multicast socket") print("Sending to )'.format((self.mcast_addr,self.mcast_port))) det run(self) text = self.input_function("What data should we multicast?") #send datagram while more to send while len(text): data = text.encode('ascii') # send datagram #@ call send on self.sock and pass in the data to be # get next datagram to send text = self.input_function("What data should we multicast?") self.running = False #main if _name__ == '_main_: parser = argparse.ArgumentParser(description='Send and receive UDP Multicast") parser.add_argument('multicast_address, help='multicast address") parser.add_argument('my_ip_address, help='my ip address") parser.add_argument('-p: metavar="PORT, type=int, default=1060, help='UDP port (default 1060)") args = parser.parse_args() mcast_addr = args.multicast_address my_ip_addr = args.my_ip_address mcast_port = args.p # set up threads for both sending and receiving receiving_thread = Receiving MulticastSocket(mcast_addr,mcast_port) sending_thread = Sending MulticastSocket(mcast_addr,mcast_port) sent sent try: self.input_function = raw_nput except: self.input_function = input self.sock.connect((self.mcast_addr,self.mcast_port)) print("made sending multicast socket") print('Sending to {}'.format((self.mcast_addr self.mcast_port))) def run(self): text = self.input_function("What data should we multicast?") # send datagram while more to send while len(text): data = text.encode('ascii') # send datagram #@ call send on self.sock and pass in the data to be # get next datagram to send text = self.input_function("What data should we multicast?") self.running = False # main if _name__ =='_main_": parser = argparse. ArgumentParser(description='Send and receive UDP Multicast") parser.add_argument('multicast_address, help="multicast address') parser.add_argument('my_ip_address', help='my ip address") parser.add_argument(-p) , metavar='PORT', type=int, default=1060, help='UDP port (default 1060)") args = parser.parse_args() mcast_addr = args.multicast_address my_ip_addr = args.my_ip_address mcast_port = args.p # set up threads for both sending and receiving receiving_thread = ReceivingMulticastSocket(mcast_addr,mcast_port) sending_thread = Sending MulticastSocket(mcast_addr,mcast_port) print("created threads") receiving_thread.start() sending_thread.start() print("started threads") sending_thread.join() receiving_thread.running = False receiving_thread.join() print("joined threads") MODULE DIRECTIONS 3: UDP Find a multicast group (see the resources). Have two or more programs, if at all possible running on different machines, where one sends to the multicast group and the other receives and vice versus. DESCRIPTION It should be possible for you and a bunch of your classmates to all agree on the same In this assignment, students write code that can multicast group and port number and all be send or receive UDP multicast packets. on servers on the same network (or even the same server) and all chat together over This is the same as UDP broadcast, except the UDP multicast through the use of this pro- IP addresses are within the range of multicast IP gram. I encourage you to use the UDP discus- addresses, and other network code adjustments sion area to coordinate times/servers/multicast- are done in order to accomplish multicast UDP groups/port-numbers to make this happen so appropriately (see additional resources). you can test with each other. Fill in the relevant portions of the provided skele- ton file to complete the assignment and make it function as stated elsewhere in this docu- OBJECTIVES ment. There may be a penalty if the file that you sub- To demonstrate knowledge of UDP multicast by mit is not derived from the provided skeleton file. writing code to do so
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
