Question

In Python, make changes in the client code, so that the client allows the user to...

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”.  Please comment on what each line does.

You are required to have files:

The modified UDP client code. (preferred file name: UDPClient.py)

The modified TCP client code. (preferred file name: TCPClient.py)

Python Socket Programming (using Python 3)

If we use UDP sockets, we have the following code:

Python code for the UDP client:

UDP client

from socket import *

serverName = 'localhost'

serverPort = 12000

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()

TCP client

from socket import *

serverName = 'localhost'

serverPort = 12000

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()

TCP clients to interact correctly with the given TCP server, your clients should also use a non-persistent implementation to send multiple requests to the server. This means that you should use a separate TCP connection to send each request from the user; if the user has multiple requests to send (which is an important requirement you need to implement), you should constantly open and close new TCP connections.

modified UDP client code

modified TCP client code

0 0
Add a comment Improve this question Transcribed image text
Answer #1

UDP Client:

Code:

# Import socket
from socket import *

# Server address
serverName = 'localhost'
# Server port number
serverPort = 12000

# Loop until user give Quit command
while True:
# Creating client socket
clientSocket = socket(AF_INET, SOCK_DGRAM)
# Get a lower case sentence from user
message = input("Input a lowercase sentence: ")
# If the input is Quit, then end the program
if message is "Quit":
break
# Otherwise send the message to server
clientSocket.sendto(message.encode(), (serverName, serverPort))
# Receive response from server
modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
# Print the server response
print(modifiedMessage.decode())
# Close client socket
clientSocket.close()

Code screenshot:

# Import socket from socket import * # Server address serverName = localhost # Server port number server Port = 12000 # Loo

Output:

RESTART: C:\Users\Retheesh\Desktop\UDPClient.py Input a lowercase sentence: hello HELLO Input a lowercase sentence: Thai H

TCP Client:

Code:

# Import socket
from socket import *

# Server address
serverName = 'localhost'
# Server port number
serverPort = 12000

# Loop until give Quit command
while True:
# Creating client socket
clientSocket = socket(AF_INET, SOCK_STREAM)
# Connecting with server
clientSocket.connect((serverName, serverPort))
# Get a lowercase sentence from user
sentence = input("Input a lowercase sentence: ")
# If inpt is Quit, then end the program
if sentence is "Quit":
break
# Otherwise sending the message to server
clientSocket.send(sentence.encode())
# Receiving message from server
modifiedSentence = clientSocket.recv(1024)
# Print the message from server
print(modifiedSentence.decode())
# Closing socket
clientSocket.close()

Code screenshot:

# Import socket from socket import * # Server address serverName = localhost # Server port number server Port = 12000 # Loo

Output:

===== ===== RESTART: C:\Users\Retheesh\Desktop\TCPClient.py Input a lowercase sentence: hello HELLO Input a lowercase sente

Additional Information:

UDP server code:

import socket

bufferSize = 1024

# Create a datagram socket
UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
#UDPServerSocket.close()
# Bind to address and ip
UDPServerSocket.bind(("127.0.0.1", 12001))

print("UDP server up and listening")

# Listen for incoming datagrams
while(True):
bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)
message = bytesAddressPair[0]
address = bytesAddressPair[1]
clientMsg = "Message from Client:{}".format(message)
clientIP = "Client IP Address:{}".format(address)
print(clientMsg)
print(clientIP)

bytesToSend = str.encode(message.upper())
  
# Sending a reply to client
# UDPServerSocket.sendto(bytesToSend, address)
UDPServerSocket.sendto(bytesToSend, address)

UDP server code screenshot:

import socket buffer Size = 1024 # Create a datagram socket UDPServerSocket = socket.socket (family=socket. AF_INET, type=soc

TCP server code:

import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)

# Bind the socket to the port
sock.bind(('localhost', 12000))

# Listen for incoming connections
sock.listen(1)

while True:
# Wait for a connection
print >>sys.stderr, 'waiting for a connection'
connection, client_address = sock.accept()

try:
print >>sys.stderr, 'connection from', client_address

# Receive the data in small chunks and retransmit it
while True:
data = connection.recv(1024)
print >>sys.stderr, 'received "%s"' % data
if data:
print >>sys.stderr, 'sending data back to the client'
connection.sendall(data.upper())
else:
print >>sys.stderr, 'no more data from', client_address
break
  
finally:
# Clean up the connection
connection.close()

TCP server code screenshot:

import socket import sys # Create a TCP/IP socket sock = socket.socket (family=socket.AF_INET, type=socket. SOCK_STREAM) # Bi

Add a comment
Know the answer?
Add Answer to:
In Python, make changes in the client code, so that the client allows the user to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use Python TCP socket to implement an application with client-server architecture. In this application, client must...

    Use Python TCP socket to implement an application with client-server architecture. In this application, client must read a line of characters from its input and send it to the server. The server must remove all non-alphanumeric characters from the input and send the modified data to the client. The client must receive the modified data and displays it on its screen.

  • Q7 The following Client side Java code can send a message to the server side via...

    Q7 The following Client side Java code can send a message to the server side via UDP socket. The client side Java code and the server side Java code are running in two different hosts respectively. The server side host name is “MyFileServer”. The server side receives the message and converts all the letters in the message received to uppercase, then sends the modified message back to the client side. Please read the code carefully, and fill out the blanks...

  • The following Client side Java code can send a message to the server side via UDP...

    The following Client side Java code can send a message to the server side via UDP socket. The client side Java code and the server side Java code are running in two different hosts respectively. The server side host name is “MyFileServer”. The server side receives the message and converts all the letters in the message received to uppercase, then sends the modified message back to the client side. Please read the code carefully, and fill out the blanks with...

  • Here is the description of the client and server programs that you need to develop in C using TCP: Suppose we have a simple student query system, where the server keeps student's info in an array...

    Here is the description of the client and server programs that you need to develop in C using TCP: Suppose we have a simple student query system, where the server keeps student's info in an array of struct student_info ( char abc123171 char name [101 double GPA; To simplify the tasks, the server should create a static array of 10 students with random abc123, name, and GPA in the server program. Then the server waits for clients. When a client...

  • Implement your first Networking application that uses the Client-Server model. Using Python and either TCP or...

    Implement your first Networking application that uses the Client-Server model. Using Python and either TCP or UDP Protocol, the application should do the following: 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 4. Client receives modified data and displays line on its screen

  • want code in C: and please answer the question! Goal: To improve the client-server model introduced...

    want code in C: and please answer the question! Goal: To improve the client-server model introduced in the class Requirements: The server should be able to accept requests from multiple clients and communicate with them. .Each client should be able to communicate multiple rounds of messages with the server. .Whenever the server receives a message from a client, it replies with "I got your message To implement the server with multithreading. .A client program should terminate its communication with the...

  • implement the follwing code using command promp or Eclipse or any other program you are familiar with. keep the name of...

    implement the follwing code using command promp or Eclipse or any other program you are familiar with. keep the name of the classes exatcly the same as requested for testing purpose. please follow each instruction provided below Objective of this assignment o get you famililar with developing and implementing TCP or UDP sockets. What you need to do: I. Implement a simple TCP Client-Server application 2. and analyze round trip time measurements for each of the above applications 3. The...

  • Overview UDP is a transport layer protocol that provides no reliability. This project aims to show...

    Overview UDP is a transport layer protocol that provides no reliability. This project aims to show how to implement extra features for the protocol in the application layer. The project will develop a new version of UDP protocol called UDPplus which provides reliable connection oriented between a client and a UDPplus sever. The UDPplus is implemented at application layer using UDP protocol only. Basic Scenario The UPDplus is a simple bank inquiry application where the client sends full name and...

  • To develop a client/server application using TCP sockets and the C programming language that is capable...

    To develop a client/server application using TCP sockets and the C programming language that is capable of supporting multiple concurrent service requests from different clients. PROBLEM You are to use the Ubuntu operating system as well as both the client and the server programs. You are to modify your server program to process requests from more than one client concurrently. This means different clients may request either the same service or a total different one. The services supported by your...

  • Project Description In this project, you will be developing a multithreaded Web server and a simple...

    Project Description In this project, you will be developing a multithreaded Web server and a simple web client. The Web server and Web client communicate using a text-based protocol called HTTP (Hypertext Transfer Protocol). Requirements for the Web server The server is able to handle multiple requests concurrently. This means the implementation is multithreaded. In the main thread, the server listens to a specified port, e.g., 8080. Upon receiving an HTTP request, the server sets up a TCP connection to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT