In this programming assignment, you will write a client ping program in Python.
ID: 3752742 • Letter: I
Question
In this programming assignment, you will write a client ping program in Python. Your client will send a simple ping message to a server, receive a corresponding pong message back from the server, and determine the delay between when the client sent the ping message and received the pong message. This delay is called the Round Trip Time (RTT). The functionality provided by the client and server is similar to the functionality provided by standard ping program available in modern operating systems. However, standard ping programs use the Internet Control Message Protocol (ICMP) Here we will create a nonstandard (but simple!) UDP-based ping program. Your ping program is to send 10 ping messages to the target server over UDP. For each message, your client is to determine and print the RTT when the correspon- ding pong message is returned. Because UDP is an unreliable protocol, a packet sent by the client or server may be lost. For this reason, the client cannot wait indefinitely for a reply to a ping message. You should have the client wait up to one second for a reply from the server; if no reply is received, the client should assume that the packet was lost and print a message accordingly. In this assignment, you will be given the complete code for the server (avail- able in the companion Web site). Your job is to write the client code, which will be very similar to the server code. It is recommended that you first study carefully the server code. You can then write your client code, liberally cutting and pasting lines from the server code.
Explanation / Answer
Answer:
import random
import time
import socket
from socket import *
#Create a UDP socket
# Notice the use of SOCK_DGRAM for UDP packets
serverSocket = socket(AF_INET, SOCK_DGRAM)# Assign IP address and port number to socket
serverSocket.bind(('127.0.0.1', 8000))
while True:
# Receive the client packet along with the address it is coming from
message, address = serverSocket.recvfrom(1024)
# Generate a random number (for introducing random packet drops)
rand = random.randint(0, 9)
#If rand is less is than 1, we consider the packet lost and do not respond
if rand < 1:
continue
# Otherwise, the server responds with a delay
time.sleep(float(random.gauss(200,25))/1000.0)
serverSocket.sendto(message, address)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.