Question: With the server and client code below, i cant to get them communicate across the network with the ping server and ping client running on
With the server and client code below, i cant to get them communicate across the network with the ping server and ping client running on different machines. please have pic of the output of the program to prove it works.
# UDPPingerServer.py # We will need the following module to generate randomized lost packets import random 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(('', 12000))
while True: # Generate random number in the range of 0 to 10 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 serverSocket.sendto(message, address)
# UDPingerClient.py
# We will need the following modules for system and time information
import sys, time
from socket import *
# Get the server hostname and port as command line arguments
argv = sys.argv
host = argv[1]
port = argv[2]
# Set initial timeout value
timeout = 2 # number of seconds before timeout
# Create UDP client socket
# Note the use of SOCK_DGRAM for UDP datagram packet
clientsocket = socket(AF_INET, SOCK_DGRAM)
# Set socket timeout
clientsocket.settimeout(timeout)
# Command line argument is a string, change the port into integer
port = int(port)
# Sequence number of the ping message
ptime = 0
# Ping for 10 times
while ptime < 10:
ptime += 1
# Format the message to be sent
data = "Pinging: " + str(ptime) + ", " + time.asctime()
# Display the message
print ("Ping Message from Client: " + data)
try:
# Sent time
RTTb = time.time()
# Send the UDP packet with the ping message
clientsocket.sendto(data,(host, port))
# Receive the server response
message, address = clientsocket.recvfrom(1024)
# Received time
RTTa = time.time()
# Display the server response as an output
print ("Reply from " + address[0] + ": " + message)
# Round trip time is the difference between sent and received time
print ("RTT: " + str(RTTa - RTTb))
except:
# Server does not response
# Assume the packet is lost
print ("Request timed out. ")
continue
# Close the client socket
clientsocket.close()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
