Please compare the UDPServer.py program and the TCPServer.py program. Why is it that the UPD program needs to specify th

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Please compare the UDPServer.py program and the TCPServer.py program. Why is it that the UPD program needs to specify th

Post by answerhappygod »

Please compare the UDPServer.py program and the TCPServer.py
program. Why is it that the
UPD program needs to specify the client address when sending the
capitalized sentence and
the TCP program does not need to specify the client address when
sending the capitalized
sentence?
UDPServer.py:
from socket import *
# Create a UDP socket
serverSocket = socket(AF_INET, SOCK_DGRAM)
# Bind the socket to the local IP and port 12345
serverIP = '127.0.0.1'
serverPort = 12345
serverAddress = (serverIP, serverPort);
serverSocket.bind(serverAddress)
print('The server is ready to receive')
while True:
# Receive a byte string up to 2048 bytes long from the socket
# The sender address is attached with the message
messageBytes, clientAddress = serverSocket.recvfrom(2048)
clientIP, clientPort = clientAddress
# decode the byte string to a string object using the UTF-8
format
message = messageBytes.decode("utf-8")
print(f'message from {clientIP}:{clientPort} = {message}')
# Send a message encoded in the UTF-8 format through the
socket
modifiedMessage = message.upper()
serverSocket.sendto(modifiedMessage.encode("utf-8"),
clientAddress)
TCPServer.py:
from socket import *
# Create a TCP welcoming socket
welcomeSocket = socket(AF_INET, SOCK_STREAM)
# Bind it to the local IP and port 12345
serverIP = '127.0.0.1'
serverPort = 12345
serverAddress = (serverIP, serverPort)
welcomeSocket.bind(serverAddress)
# Welcome socket begins listening for incoming TCP requests
# Allows up to 1 pending requests for connection in the queue
welcomeSocket.listen(1)
print('The server is ready to receive')
while True:
# server waits on accept() for incoming requests
# a new connection socket is created on return
connectionSocket, clientAddress = welcomeSocket.accept()
clientIP, clientPort = clientAddress
# receive a byte string from the connection socket
messageBytes = connectionSocket.recv(1024)
message = messageBytes.decode("utf-8")
print(f'message from {clientIP}:{clientPort} = {message}')
# send a byte string to the connection socket
modifiedMessage = message.upper()
connectionSocket.send(modifiedMessage.encode("utf-8"))
# close the connection socket to this client
connectionSocket.close()
# the welcoming socket is still open for new clients
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply