Question: Project Requirements In this project, you will gain hands-on experience on both TCP and UDP socket programming. You are required to write a program (STUDENT)
Project Requirements In this project, you will gain hands-on experience on both TCP and UDP socket programming. You are required to write a program (STUDENT) to interact with the provided program (ROBOT). You are allowed to use the ROBOT source code given (either Java or Python) in the project package to complete this project. All students should finish Steps 1 to 6. This project can be completed by a group of two students or alone. It is your responsibility to find your teammate if you choose to work as a group. You will need to demo your program to the TA as required by Step 6, and if you work alone, the TA will run the ROBOT program on his machine to help you demo your STUDENT program. EVERY student should turn in their own source code whether they work in a group or not. This project will be conducted over 3 lab sessions. - Lab #1 is on February 12, 2021, and will teach preliminaries about the lab tasks and programming basics in Python. - Lab #2 on February 19, 2021 is a working lab where you will continue working on the project or demonstrate the finished program to the TA if ready. - Lab #3 on February 26, 2021 is the last date to demonstrate your work. Each group should compile, run, and demonstrate their program in front of the TA. The TA may ask some questions related to the program, answer correctness will affect your grading of this lab. Steps: Your program has to interact with ROBOT according to the following steps: 1. Start the ROBOT using either Python or Java: a. python robot.py b. java robot ( you need to compile first using javac robot.java) 2. When the ROBOT is started, a message ROBOT IS STARTED will be printed, indicating that the ROBOT is now listening on TCP Port 3310. STUDENT has to connect to the ROBOT TCP Port 3310 and send your BlazerID via the connection established. 3. The ROBOT will then send a 5 char string ddddd to the STUDENT. STUDENT will need to create a TCP socket s_2 at port ddddd to accept a new connection. The ROBOT will initiate the new connection 1 second later after sending ddddd. Upon accepting the connection, a new socket s2 will be returned. 4. ROBOT will then send a 12 char string fffff,eeeee. to the STUDENT using the new connection. STUDENT needs to decode the message and create a UDP socket s3 to send a variable num ( 5 < num < 10) to ROBOT on port fffff. Then ROBOT will send a char string xxx with length num * 10 to STUDENT one second after receiving num and STUDENT will receive the string using s3 on port eeeee . ROBOT will send the string xxx 5 times, once every 1 second. STUDENT only needs to receive any one of them. 5. When the STUDENT received the char string xxx, it will send back the string to the ROBOT at UDP port fffff. Similar to the ROBOT, the string will be sent 5 times, once every 1 second. The ROBOT will check if the two strings are the same. 6. In this step, you need to run ROBOT and STUDENT on 2 different machines and make sure all the steps above can be successfully executed. You may need to use Wireshark on the machines for network level debugging if necessary, e.g. if there is a firewall on either of the machine and/or along their end-to-end path, the programs may fail. It is then your responsibility to fix it, e.g. by choosing a different pair of machines elsewhere and/or configure them properly. Demo your completed project to the TA.
# IERG3310 Projectimport socketimport randomimport timelistenPort = 3310socket.setdefaulttimeout(120)localhost = ''print("Robot started")print("")# Create a TCP socket to listen connectionprint("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)s" % locals()print("Waiting for connection on port", listenPort)# accept connections from outside, a new socket is constructeds1, address = listenSocket.accept()studentIP = address[0]print(" Client from %s at port %d connected" %(studentIP,address[1]))# Close the listen socket# Usually you can use a loop to accept new connectionslistenSocket.close()data = s1.recv(100)print("Student ID received: " + str(data))iTCPPort2Connect = random.randint(0,9999) + 20000print("Requesting 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 STUDENTiUDPPortRobot = random.randint(0,9999) + 20000iUDPPortStudent = random.randint(0,9999) + 20000print("Sending the UDP information: to ROBOT: <%d>, to STUDENT: <%d>..." %(iUDPPortRobot,iUDPPortStudent))ports_to_send = str(iUDPPortRobot)+","+str(iUDPPortStudent)s2.send(ports_to_send.encode())print("Done")
# Create a UDP socket to send and receive dataprint("Preparing to receive x...")addr = (localhost, iUDPPortRobot)s3 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)s3.bind(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(messageToTransmit.encode(),(studentIP,iUDPPortStudent)) 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):breakprint("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()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
