Question: Update the following code to avoid ( a ) plagiarism and ( b ) so the server can present the following questions to the clients

Update the following code to avoid (a) plagiarism and (b) so the server can present the following questions to the clients and the clients can ask the questions as many times as the clients want the server should always provide the answer without interruption and if the query's answer is not stored in the server, the server should provide invalid query. Update both the server and client program:
server.py
# import socket programming library
import socket
# import thread module
from _thread import *
import threading
print_lock = threading.Lock()
# thread function
def threaded(c):
while True:
# data received from client
data = c.recv(1024)
if not data:
print('Bye')
# lock released on exit
print_lock.release()
break
# reverse the given string from client
data = data[::-1]
# send back reversed string to client
c.send(data)
# connection closed
c.close()
def Main():
host =""
# reserve a port on your computer
# in our case it is 12345 but it
# can be anything
port =12345
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
print("socket binded to port", port)
# put the socket into listening mode
s.listen(5)
print("socket is listening")
# a forever loop until client wants to exit
while True:
# establish connection with client
c, addr = s.accept()
# lock acquired by client
print_lock.acquire()
print('Connected to :', addr[0],':', addr[1])
# Start a new thread and return its identifier
start_new_thread(threaded,(c,))
s.close()
if __name__=='__main__':
Main()
questions and answers to include in the server:
# Questions and their corresponding answers
QUESTIONS ={
1: ("What is the minimum and maximum length of a TCP header?",
"The minimum TCP header length is 20 bytes, and the maximum is 60 bytes."),
2: ("What is the purpose of the three-way handshake in TCP?",
"The three-way handshake establishes a reliable connection between the client and server."),
3: ("What is the difference between TCP and UDP?",
"TCP is connection-oriented and reliable, whereas UDP is connectionless and faster."),
4: ("What are the main flags in a TCP segment header, and what do they signify?",
"Flags include SYN, ACK, FIN, RST, and others, used for connection control."),
5: ("How does TCP ensure reliable data transfer?",
"TCP uses acknowledgments, retransmissions, and sequence numbers.")
}
client.py
# Import socket module
import socket
def Main():
# local host IP '127.0.0.1'
host ='127.0.0.1'
# Define the port on which you want to connect
port =12345
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# connect to server on local computer
s.connect((host,port))
# message you send to server
message = "shaurya says geeksforgeeks"
while True:
# message sent to server
s.send(message.encode('ascii'))
# message received from server
data = s.recv(1024)
# print the received message
# here it would be a reverse of sent message
print('Received from the server :',str(data.decode('ascii')))
# ask the client whether he wants to continue
ans = input('
Do you want to continue(y/n) :')
if ans =='y':
continue
else:
break
# close the connection
s.close()
if __name__=='__main__':
Main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!