Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Networking : struct.pack() and struct.unpack() python 3 I have a below code... Would any one help me to fix these section in my code for

Networking : struct.pack() and struct.unpack() python 3

I have a below code... Would any one help me to fix these section in my code for following requirement:

The ping message contains 2 4-byte integers and must be encoded in network-byte order as follows:

  • 4-byte message type with an integer value of 1 or 2

    • Message type = 1 for a ping request (message from client to server)

    • Message type =2 for a ping response (message from server to client)

(by using the struct.pack() and struct.unpack() functions to encode integers into a sequence of bytes in network byte order)

================

UDPPinglerClient.py

import socket from socket import AF_INET, SOCK_DGRAM import time print 'Running'

serverName = '127.0.0.1' #server set as localhost clientSocket = socket.socket(AF_INET,SOCK_DGRAM) #create the socket clientSocket.settimeout(1) #sets the timeout at 1 sec sequence_number = 1 #variable to keep track of the sequence number rtt=[] # list to store the rtt values

while sequence_number<=10: start=time.time() #the current time message = ('PING %d %d' % (sequence_number, start)) #client message clientSocket.sendto(message,(serverName, 11046))#client sends a message to the server try: message, address = clientSocket.recvfrom(1024) #recieves message from server elapsed = (time.time()-start) # calculates the rtt rtt.append(elapsed) print message print 'Round Trip Time is:' + str(elapsed) + ' seconds' except socket.timeout: #if no reply within 1 second print message print 'Request timed out' sequence_number+=1 #incremented by 1 if sequence_number > 10: #closes the socket after 10 packets mean=sum(rtt, 0.0)/ len(rtt) print '' print 'Maximum RTT is:' + str(max(rtt)) + ' seconds' print 'Minimum RTT is:' + str(min(rtt)) + ' seconds' print 'Average RTT is:' + str(mean)+ ' seconds' print 'Packet loss rate is:' + str((10-len(rtt))*10)+ ' percent' clientSocket.close()

UDPPinglerServer.py

# 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(('', 11046))

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)

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

Logistics Lifeline Supply Chain Strategies

Authors: Ehsan Sheroy

1st Edition

7419377502, 978-7419377503

More Books

Students also viewed these Databases questions