Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The UDP client sends 20 messages to the UDP server. The UDP server replies to the client whenever 5 messages are received (or delivered), so

  1. The UDP client sends 20 messages to the UDP server.

  2. The UDP server replies to the client whenever 5 messages are received (or delivered), so the client receives only 5 messages from the server.

  3. The UDP server does not reply if any of 5 messages is not delivered.

*

############################## # UDP Echo Clinet ############################### import socket import sys

# Create a UDP socket sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) server_address = ('localhost', 10000)

# for checking a number of messages count = 0

try: while True: message = sys.stdin.readline().replace(' ','') # Send data sys.stderr.write('sending "%s" ' % message) sent = sock.sendto(message.encode(), server_address) count = count + 1 if message.lower() == 'exit': break

# Receive response sys.stderr.write('waiting to receive ') data, server = sock.recvfrom(4096) sys.stderr.write('received "%s" ' % data.decode())

finally: sys.stderr.write('closing socket ') sock.close()

############################### # UDP Echo Server ############################### import socket import sys

# Create a datagram socket sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Bind the socket to the port (port number: 10000) server_address = ('localhost', 10000) sys.stderr.write('starting up on %s port %s ' % server_address) sock.bind(server_address)

# for checking a number of messages count = 0

while True: sys.stderr.write('waiting to receive message ') data, address = sock.recvfrom(4096) sys.stderr.write('received %s bytes from %s ' % (len(data), address)) sys.stderr.write(data.decode()) if data: if data.lower() == 'exit': break else: sent = sock.sendto(data, address) sys.stderr.write(' sent %s bytes back to %s ' % (sent, address))

sock.close()

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

Students also viewed these Databases questions