The code below simulates a really busy server that accepts sentences as requests and returns capitalized sentences as responses. This code is also available as a text attachment, and must not be altered.
import random
from time import sleep
from socket import *
BUSY_PERCENT = 50
serverPort = 12000
def get_busy():
random_number = random.randint(0, 100)
if random_number < BUSY_PERCENT:
print(“Getting busy …”)
sleep(1)
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind((’’, serverPort))
print(’The server is ready to receive requests’)
while True:
get_busy()
message, clientAddress = serverSocket.recvfrom(2048)
print(’Received request:n’, message.decode(), ’nfrom client:n’, clientAddress)
modifiedMessage = message.decode().upper()
serverSocket.sendto(modifiedMessage.encode(), clientAddress)
get_busy()
You need to implement three clients that do the following:
- It sends 20 messages to the server; the contents of these messages should be as follows (one message per line):
ping message number 0
ping message number 1
ping message number 2
…
ping message number 19
The messages are sent sequentially, i.e., one after the other.
- After sending each message, the client waits up to 1 second for a response from the server. If a response is received within this time, it prints the response. Otherwise, it retries after a delay. The three clients will have three delay strategies:
- No delay. The request is resent immediately without any backoff.
- Exponential backoff. In this strategy, the delay for attempt 0 (initial attempt) is 0, for attempt 1 (first retry) it is a random number of seconds selected from {0, 1}, for attempt 2 it is a random number of seconds selected from {0, 1, 2, 3}, for attempt 3 it is a random number of seconds selected from {0, 1, 2, 3, 4, 5, 6, 7}, and so on.
- Linear backoff. In this strategy, the delay for attempt 0 is 0, for attempt 1 it is a random number of seconds selected from {0, 1}, for attempt 2 it is a random number of seconds selected from {0, 1, 2}, for attempt 3 it is a random number of seconds selected from {0, 1, 2, 3}, for attempt 4 it is a random number of seconds selected from {0, 1, 2, 3, 4} and so on.
- In each case, the client gives up and moves on to the next request at the end of 10 attempts.
- At the end, the client prints out the average number of attempts per message. The attached file SampleOutput.txt shows what a sample run of this client should produce.
REQUIRED
1. Three client programs, named NoBackoffClient.py, ExponentialBackoffClient.py and LinearBackoffClient.py.