I am taking a networking course and i dont seem to understand the question and d
ID: 3847982 • Letter: I
Question
I am taking a networking course and i dont seem to understand the question and dont know how to do this so i am trying to learn how this code is suppose to be written.
In this programming assignment, you will write a client pingprogram in Python. Your client will send a simple ping message to a server, receive a correspondingpong message back from the server, and determine the delay between when the client sent the ping message and received the pong message. Ihis delay is calledthe Round l rip Time (RTT You should use UDP socketto transmit the ping message Remember that you cannot use the well-known port numbers such as 80, 40, and 21 Pick a relatively high port (i.e., something between 2000 and 65000). You should be able to test your program by running both the client and the server programs on the same machine. You can run two instances of the IDE that you use. one for the client code and one for the server code. Help for Python 3 You can use the time function of the time library to get the current time In order to find the elapsed time between two instructions you can run the time function before and after those two instruction.Explanation / Answer
#SERVER CODE FOR EXCHANGE OF MESSAGES BETWEEN CLIENT AND SERVER
import random
from socket import *
import time
def main():
# Create a UDP socket
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind((”, 12000))
while True:
rand = random.randint(0, 10)
# Receive the client packet along with the address it is coming from
message, address = serverSocket.recvfrom(1024)
# Capitalize the message from the client
message = message.upper()
# If rand is less is than 4, we consider the packet lost and do not respond
if rand < 4:
continue
# Otherwise, the server responds
time.sleep(.05)
serverSocket.sendto(message, address)
pass
if __name__ == ‘__main__’:
main()
#CLIENT CODE
from socket import *
from datetime import datetime
def main():
serverName = ‘localhost’
serverPort = 12000
c = 0;
message = raw_input(‘Input Lowercase Message’)
while c < 10:
c = c +1
mainSocket = socket(AF_INET,SOCK_DGRAM) # create socket
try:
mainSocket.settimeout(1.0) # timeout after 1 second
startTime = datetime.now() # start time is current time at declaration
mainSocket.sendto(message,(serverName, serverPort))
modifiedMessage, serverAddress = mainSocket.recvfrom(1024)
endTime = datetime.now()
except timeout: # if timeout
print ‘PING ‘ +str(counter)+’ ‘+ str(startTime)+ ‘: Request timed out!’
mainSocket.close() # close socket
else:
print ‘PING ‘ +str(counter)+’ ‘+ str(startTime)+’: Returned: ‘ + modifiedMessage + ‘ after ‘+ str(endTime-startTime)
mainSocket.close()
pass
if __name__ == ‘__main__’:
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.