Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Socket Programming Using Python: Provided below are both the server and client pingers. The Server codes are all set, However I am utterly lost on

Socket Programming Using Python:

Provided below are both the server and client pingers. The Server codes are all set, However I am utterly lost on what codes I should be providing with the client side where it says #PROVIDE CODE.

---------------------------------------------------

# 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)) print "UDPPingerServer running..." 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) print "Received message, " + str(message) + " from " + str(address[0]) + "." print "Converting to upper case..." # 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: print "Oops! I've dropped this message! " continue # Otherwise, the server responds serverSocket.sendto(message, address) print "Sending message, " + str(message) + " back to " + address[0] + "... " 

--------------------------------------------------------

# UDPPingerClient.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 and set initial timeout value argv = sys.argv host = argv[1] port = argv[2] timeout = 3 # 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 # PROVIDE CODE - data = ... # Display the message # PROVIDE CODE - print ... try: # Sent time RTTb = time.time() # Send the UDP packet with the ping message # PROVIDE CODE - clientsocket ... # Receive the server response # PROVIDE CODE - message, address = ... # Received time RTTa = time.time() # Display the server response as an output # PROVIDE CODE - print ... # Round trip time is the difference between sent and received time # PROVIDE CODE - print ... time.sleep(3) except: # Server does not response # Assume the packet is lost print("Request timed out. ") continue # Close the client socket # PROVIDE CODE - clientsocket 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Structured Search For Big Data From Keywords To Key-objects

Authors: Mikhail Gilula

1st Edition

012804652X, 9780128046524

Students also viewed these Databases questions

Question

What occurs in each stage of the audit life cycle?

Answered: 1 week ago