Question: Socket Programming in Python : Make changes in the client code, so that the client allows the user to continue to send multiple requests until
Socket Programming in Python: Make changes in the client code, so that the client allows the user to continue to send multiple requests until the user types in Quit. This means that the client process should not exit after the user sends one request and receives one response. Instead, the client process should be able to receive subsequent inputs from the user. You need to have a loop in the client code, so that it can accept the user request until the user explicitly types in Quit.
You are required to have three files:
The modified UDP client code. (preferred file name: UDPClient.py or UDPClient.java)
The modified TCP client code. (preferred file name: TCPClient.py or TCPClient.java)
The project report, which must include the tests you have run to verify that your code meets the requirements. You can paste what you got in the console or some screenshots.
In this project, you dont need to change anything on the server side. Please run the given code on your machine to understand the code and the original application, before you make changes to the code. Make sure your code works without errors before submission.
The behaviors of the application are as follows:
1. client reads a line of characters (data) from its keyboard and sends data to server.
2. server receives the data and converts characters to uppercase.
3. server sends modified data to client; continue to run to serve other clients.
4. client receives modified data and displays line on its screen and then exits.
We assume that both the client and the server run on the same machine.
Python Socket Programming (using Python 3)
If we use UDP sockets, we have the following code:
Python code for the UDP client:
from socket import *
serverName = 'localhost'
serverPort = 5000
clientSocket = socket(AF_INET, SOCK_DGRAM)
message = input("Input a lowercase sentence: ")
clientSocket.sendto(message.encode(), (serverName, serverPort))
modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
print(modifiedMessage.decode())
clientSocket.close()
Python code for the UDP server:
from socket import *
serverPort = 5000
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('', serverPort))
print("The server is ready to receive.")
while True:
message, clientAddress = serverSocket.recvfrom(2048)
modifiedMessage = message.decode().upper()
print("message to be sent back: ")
print(modifiedMessage)
serverSocket.sendto(modifiedMessage.encode(), clientAddress)
If we use TCP sockets, we have the following code:
Python code for the TCP client
from socket import *
serverName = 'localhost'
serverPort = 5000
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName, serverPort))
sentence = input("Input a lowercase sentence: ")
clientSocket.send(sentence.encode())
modifiedSentence = clientSocket.recv(1024)
print(modifiedSentence.decode())
clientSocket.close()
Python code for the TCP server
from socket import *
serverPort = 5000
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('', serverPort))
serverSocket.listen(1)
print("The server is ready to receive.")
while True:
connectionSocket, addr = serverSocket.accept()
sentence = connectionSocket.recv(1024).decode()
capitalizedSentence = sentence.upper()
connectionSocket.send(capitalizedSentence.encode())
connectionSocket.close()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
