Question: CREATE PYTHON CODE TO RESPOND TO THIS ROBOT SERVER (ROBOT.py) Call it STUDENT.py.. pls leave notes so others can understand whats going on. Thanks you

CREATE PYTHON CODE TO RESPOND TO THIS ROBOT SERVER (ROBOT.py) Call it STUDENT.py.. pls leave notes so others can understand whats going on. Thanks you so much for your help!
listenPort = 3310
socket.setdefaulttimeout(120)
localhost = ""
print("Robot started")
print("")
# Create a TCP socket to listen connection
print("Creating TCP socket...")
listenSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listenSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listenSocket.bind((localhost, listenPort))
listenSocket.listen(5)
print ("Done")
print(" TCP socket created, ready for listening and accepting connection...")
print("waiting for connection on port", listenPort)
#accept connections from outside, a new socket is constructed
s1, address = listenSocket.( )
StudentIP = address [0]
print("\Client from %s at port %d connected" %(studentIP, address[1]))
listenSocket.close( )
data = s1.recv(100)
print("Student ID received: " + str(data) )
iTCPPort2Connect = random.randint (0, 9999) + 20000
print("Requestiing STUDENT to accept TCP <%d>..." %iTCPPort2Connect)
s1.send(str(iTCPPort2Connect).encode() )
print("Done")
time.sleep(1)
print(" Connecting to the STUDENT s1 <%d>..." %iTCPPort2Connect)
#Connect to the server (student s2)
s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s2.connect((studentIP, iTCPPort2Connect))
#Send the ports required to STUDENT
iUDPPortRobot = random.randint(0,9999) + 20000
iUDPPortStudent = random.randomt(0,9999) + 20000
print("Sending the UDP information: to ROBOT : <%d>..." %
ports_to_send = str (iUDPPortRobot) +","+str (iUDPPortStudent)
print ("Done")
#Create a UDP socket to send and receive data
print("Preparing to receive x...")
addr = (localhost, iUDPPortRobot)
s3 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s3bind(addr)
x, addr = s3.recvfrom(1)
print("Get x = %d" % (int(x)))
time.sleep(1)
print("Sending UDP packets:")
messageToTransmit = ""
for i in range(0,int(x) * 2):
messageToTransmit += str(random.randint(0,9999)) .zfill(5)
print("Message to transmit: " +messageToTransmit)
for i in range(0,5):
s3.sendto(messageToTtabsmit.encode()(studentIP, iUDPPortStudenr))
time.sleep(1)
print("UDP packet %d sent" %(i+1))
print(" Receiving UDP packet: ")
while True: #remove potentially duplicate msg
data, addr = s3.recvfrom(int(x) * 10)
data = data.decode()
if int(data) != int(x):
break
print("Received: ", data)
if messageToTransmit ==data:
print(" The two strings are the same. ")
else:
print(" The two strings are not the same.")
s1.close()
s2.close()
s3.close()
exit()
PLS write neatly!!!!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!