Please answer in Phython only thank you Replace the user-specified number of messages to be sent with a variable numMsgs that you can define internally in your program. We will set numMsgs according to the experiments specified below. • Replace the user-specified message string with a string of the size defined by the experiments below. Please utilize the following example that allows us to create a string comprising of null values of a size specified using the variable numBytes. In the following example, we are creating a string called message of a 1000 bytes. We can similarly create a message string of 5000 and 10000 Bytes by changing the value of numBytes. numBytes = 1000 message = ‘\x00’ * numBytes To get the size of this message (needed later as a part of your code output), you may use the getsizeof utility from the system library in Python. Please import the system library as ‘import sys’. The following example shows how to get the size of the message we created earlier: sys.getsizeof(message)   Create and use variables, timeBefore and timeAfter, to measure the time BEFORE and AFTER each sendto( ) call sending a message to the server. Use the time( )function in Python to measure the time. This function returns the current time in seconds since the "epoch." The epoch is a reference point in time, and the specific epoch used by Python is the "Unix epoch," which is defined as 00:00:00 Coordinated Universal Time (UTC) on January 1, 1970. When time( ) function is called, it returns a floating-point number representing the number of seconds that have passed since January 1, 1970 (the Unix epoch) to the current time. We can call the time( ) function at multiple points in our program to find the time difference between these points. To use the time function, import the time library as ‘import time’. The following example shows how to store a time value in a variable timeBefore: timeBefore = time.time( ) • Create a variable RTT that measures the round trip time for each message within the message sending loop. • Create a variable SeqNum that reflects the sequence number of each message sent from client to the server. • For each message received from the server in response to a message sent from the client, display a message of the type: Message sequence number with size received from with round trip time seconds. Here’s a sample output from my client code: Message sequence 1 with size 5049 received from ('127.0.0.1', 12001) with RTT 0.0018 s. Message sequence 2 with size 5049 received from ('127.0.0.1', 12001) with RTT 0.0033 s. Message sequence 3 with size 5049 received from ('127.0.0.1', 12001) with RTT 0.0025 s. Message sequence 4 with size 5049 received from ('127.0.0.1', 12001) with RTT 0.0037 s. Message sequence 5 with size 5049 received from ('127.0.0.1', 12001) with RTT 0.0041 s. Here is my code from so far: Echo Client import socket def udp_echo_client():     # Create a UDP socket     client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)     server_address = ('localhost', 65432)     # Get user input     message = input("Enter a message to send to the server: ")     num_times = int(input("How many times would you like to send the message? "))     for _ in range(num_times):         # Send data to the server         sent = client_socket.sendto(message.encode(), server_address)         # Receive response from the server         data, server = client_socket.recvfrom(4096)         print(f"Received echo: '{data.decode()}' from {server}") if __name__ == "__main__":     udp_echo_client() Echo Server: import socket def udp_echo_server():     # Create a UDP socket     server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)     # Bind the socket to a specific address and port     server_address = ('localhost', 65432)     server_socket.bind(server_address)     print(f"Starting UDP echo server on {server_address}")     while True:         # Receive data and address from the client         data, address = server_socket.recvfrom(4096)         print(f"Received message: '{data.decode()}' from {address}")         # Echo the data back to the client         sent = server_socket.sendto(data, address)         print(f"Echoed back to {address}") if __name__ == "__main__":     udp_echo_server()

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Please answer in Phython only thank you

Replace the user-specified number of messages to be sent with a variable numMsgs that you
can define internally in your program. We will set numMsgs according to the experiments
specified below.
• Replace the user-specified message string with a string of the size defined by the
experiments below. Please utilize the following example that allows us to create a string
comprising of null values of a size specified using the variable numBytes.
In the following example, we are creating a string called message of a 1000 bytes. We can
similarly create a message string of 5000 and 10000 Bytes by changing the value of
numBytes.
numBytes = 1000
message = ‘\x00’ * numBytes
To get the size of this message (needed later as a part of your code output), you may use the
getsizeof utility from the system library in Python. Please import the system library as ‘import
sys’. The following example shows how to get the size of the message we created earlier:
sys.getsizeof(message)
 
Create and use variables, timeBefore and timeAfter, to measure the time BEFORE and AFTER
each sendto( ) call sending a message to the server.
Use the time( )function in Python to measure the time. This function returns the current time
in seconds since the "epoch." The epoch is a reference point in time, and the specific epoch
used by Python is the "Unix epoch," which is defined as 00:00:00 Coordinated Universal
Time (UTC) on January 1, 1970. When time( ) function is called, it returns a floating-point
number representing the number of seconds that have passed since January 1, 1970 (the
Unix epoch) to the current time. We can call the time( ) function at multiple points in our
program to find the time difference between these points.
To use the time function, import the time library as ‘import time’. The following example
shows how to store a time value in a variable timeBefore:
timeBefore = time.time( )
• Create a variable RTT that measures the round trip time for each message within the
message sending loop.
• Create a variable SeqNum that reflects the sequence number of each message sent from
client to the server.
• For each message received from the server in response to a message sent from the client,
display a message of the type: Message sequence number <seqNum> with size <message
size> received from <server address> with round trip time <RTT> seconds.
Here’s a sample output from my client code:
Message sequence 1 with size 5049 received from ('127.0.0.1', 12001) with RTT 0.0018 s.
Message sequence 2 with size 5049 received from ('127.0.0.1', 12001) with RTT 0.0033 s.
Message sequence 3 with size 5049 received from ('127.0.0.1', 12001) with RTT 0.0025 s.
Message sequence 4 with size 5049 received from ('127.0.0.1', 12001) with RTT 0.0037 s.
Message sequence 5 with size 5049 received from ('127.0.0.1', 12001) with RTT 0.0041 s.

Here is my code from so far:
Echo Client

import socket

def udp_echo_client():
    # Create a UDP socket
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    server_address = ('localhost', 65432)

    # Get user input
    message = input("Enter a message to send to the server: ")
    num_times = int(input("How many times would you like to send the message? "))

    for _ in range(num_times):
        # Send data to the server
        sent = client_socket.sendto(message.encode(), server_address)

        # Receive response from the server
        data, server = client_socket.recvfrom(4096)
        print(f"Received echo: '{data.decode()}' from {server}")

if __name__ == "__main__":
    udp_echo_client()

Echo Server:

import socket

def udp_echo_server():
    # Create a UDP socket
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    # Bind the socket to a specific address and port
    server_address = ('localhost', 65432)
    server_socket.bind(server_address)
    print(f"Starting UDP echo server on {server_address}")

    while True:
        # Receive data and address from the client
        data, address = server_socket.recvfrom(4096)
        print(f"Received message: '{data.decode()}' from {address}")

        # Echo the data back to the client
        sent = server_socket.sendto(data, address)
        print(f"Echoed back to {address}")

if __name__ == "__main__":
    udp_echo_server()

Expert Solution
Step 1: Algorithm:

Algorithm for UDP Echo Client:

1. Import the necessary libraries: socket, time, and sys.

2. Create a UDP socket using `socket.socket(socket.AF_INET, socket.SOCK_DGRAM)`.

3. Define the server's address as a tuple (hostname, port).

4. Define the number of messages to be sent (numMsgs) and the message size (numBytes).

5. Create a message of the specified size filled with null bytes: `message = b'\x00' * numBytes`.

6. Initialize a sequence number variable, SeqNum, to 1.

7. Loop to send a specified number of messages (numMsgs):
    a. Measure the time before sending the message using `time.time()` and store it in timeBefore.
    b. Send the message to the server using `client_socket.sendto(message, server_address)`.
    c. Receive the response from the server using `client_socket.recvfrom(4096)`.
    d. Measure the time after receiving the response using `time.time()` and store it in timeAfter.
    e. Calculate the round trip time (RTT) as RTT = timeAfter - timeBefore.
    f. Display a message including the sequence number, message size, server address, and RTT.
    g. Increment the sequence number (SeqNum) for the next message.

8. Close the client socket.

Algorithm for UDP Echo Server:

1. Import the necessary library: socket.

2. Create a UDP socket using `socket.socket(socket.AF_INET, socket.SOCK_DGRAM)`.

3. Bind the server socket to a specific address and port using a tuple (hostname, port).

4. Print a message indicating that the server is running.

5. Enter an infinite loop to continuously listen for incoming messages:
    a. Receive data and the client's address from the client using `server_socket.recvfrom(4096)`.
    b. Print the received message and the client's address.
    c. Echo the received data back to the client using `server_socket.sendto(data, address)`.

6. The server will keep running and echoing received messages back to the clients.

Note: You should save the client code in a file named `udp_echo_client.py` and the server code in a file named `udp_echo_server.py`. You can run the client and server separately in different terminal windows to simulate the communication.

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education