Programming Assignment 1: UDP Ping Client and Server Using UDP sockets, you will
ID: 3881557 • Letter: P
Question
Programming Assignment 1: UDP Ping Client and Server Using UDP sockets, you will write a client and server program that enables the client to determine the round-trip time (RTT) to the server. To determine the RTT delay, the client records the time on sending a ping request to the server, and then records the time on receiving a ping response from the server . The difference in the two times is the RTT. The ping message contains 2 4-byte integers and must be encoded in network-bite order as follows: 4-byte message type with an integer value of 1 or 2 . o Message type 1 for a ping request (message from client to server) o Message type 2 for a ping response (message from server to client) . 4-byte sequence number with a unique integer value starting from 13. In the ping the server should echo back the client's sequence number response, The client program should take the following input parameters: IP address of server IP port of server The client program will read in the above input parameters, and send 10 ping requests consecutively, waiting for a response each time, to the server running at the specified IP address and port. After each response is received, the client calculates and prints the RTT for the message. If no response is received within a certain amount of time (one second), the client notes that the request timed out and then sends the next request up to the maximum. The program output should print out trace information when data is sent and received, and account for error conditions. Trace output must include: .At start of output, print a message indicating the IP address and port of the server being pinged For each ping response received, print RTT along with sequence number of ping message For no ping response, print "Message timed out" along with sequence number of the ping message .After completion, print the following statistics (similar to output of UNIX ping utility): Number of packets sent, received, lost (% loss rate) Min, Max, Average RTT for all acknowledged ping packets o oExplanation / Answer
A simple server-client program :
Server :
A server has a bind() method which binds it to a specific ip and port so that it can listen to incoming requests on that ip and port.The listen() method allows the server to listen to incoming connections. The accept method initiates a connection with the client and the close method closes the connection with the client.
# first of all import the socket library
import socket
# next create a socket object
s = socket.socket()
print "Socket successfully created"
# reserve a port on your computer
port = 12345
# Next bind to the port by inputting the IP address and IP port
s.bind(('', port))
print "socket binded to %s" %(port)
# put the socket into listening mode
s.listen(10)
print "socket is listening"
# a loop until we encounter an error
while True:
# Establish connection with client.
c, addr = s.accept()
print 'Got connection from', addr
# send a thank you message to the client.
c.send('Thank you for connecting')
# Close the connection with the client
c.close()
Client :
Now we need something with which a server can interact.
# Import socket module
import socket
# Create a socket object
s = socket.socket()
# Define the port on which you want to connect
port = 12345
# connect to the server on local computer by inputting the IP address and IP port
s.connect(('127.0.0.1', port))
# receive data from the server
print s.recv(1024)
# close the connection
s.close()
# start the server:
$ python server.py
Socket successfully created
socket binded to 12345
socket is listening
Got connection from ('127.0.0.1', 52617)
# start the client:
$ python client.py
Code to calculate RTT :
# Function to calculate the RTT
import time
import requests
def RTT(url):
# time when the signal is sent
t1 = time.time()
r = requests.get(url)
# time when signal is received
t2 = time.time()
# total time taken
tim = str(t2-t1)
print("Time in seconds :" + tim)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.