server.py:
import socket
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ''
port = 50000
serverSocket.bind((host, port))
#===================================================================================================
#===================================================================================================
credit_breakdown = [
[ imagine some list ]
]
core_credits = [
[ sample list ]
]
prereqs = [
[ sample list ]
]
profs = [
[sample],
]
path = [
[sample]
]
path_credit = [
[ sample],
]
elecs = [
[ sample ]
]
monsoon = [
[sample ]
]
spring = [
[ sample ]
]
def servers():
data = clientSocket.recv(1024)
if not data:
print("Data not recieved!")
x = int(data.decode('utf-8'))
return x
while True:
serverSocket.listen(4)
print("Server is waiting for a connection ....")
clientSocket, address = serverSocket.accept()
print(f"connection from: {address}")
x = servers()
#=========================================================================================================
if ( x == 1 ):
response_message = f"A minimum of 150 credits are required to complete BSc Hons"
clientSocket.send(response_message.encode('utf-8'))
elif ( x == 2 ):
response_message = "\n".join(map(str, credit_breakdown))
clientSocket.send(response_message.encode('utf-8'))
elif ( x == 3 ):
response_message = "\n".join(map(str, core_credits))
clientSocket.send(response_message.encode('utf-8'))
elif ( x == 4 ) :
#req = clientSocket.recv(1024).decode('utf-8')
#req = int(req)
response_message = "\n".join(map(str, core_credits)) #prereqs[req-1]))
clientSocket.send(response_message.encode('utf-8'))
elif ( x == 5 ) :
response_message = "\n".join(map(str, path))
clientSocket.send(response_message.encode('utf-8'))
elif ( x == 6 ) :
response_message = "\n".join(map(str, path_credit))
clientSocket.send(response_message.encode('utf-8'))
elif ( x == 7 ) :
prof = clientSocket.recv(1024).decode('utf-8')
prof = int(prof)
response_message = "\n".join(map(str, profs[prof-1]))
clientSocket.send(response_message.encode('utf-8'))
elif ( x == 8 ) :
response_message = "\n".join(map(str, elecs))
clientSocket.send(response_message.encode('utf-8'))
elif ( x == 9 ) :
response_message = "\n".join(map(str, monsoon))
clientSocket.send(response_message.encode('utf-8'))
elif ( x == 10 ) :
response_message = "\n".join(map(str, spring))
clientSocket.send(response_message.encode('utf-8'))
clientSocket.close()
serverSocket.close()
client.py:
import socket
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverHost = ""
serverPort = 50000
clientSocket.connect((serverHost,serverPort))
def clients():
x = int(input("Enter an integer: "))
clientSocket.send(str(x).encode('utf-8'))
return x
while True:
menu = '''
======================================================
MENU --- SELECT THE INTEGER CORRESPONDING TO THE QUESTION YOU WANT TO ASK:
======================================================
1. What is the total number of CS credits required to complete the 4-year BSc Hons degree in CS?
2. Could you provide a breakdown of credits for a CS Degree?
3. What are the core courses and their credits?
4. I want to know the pre-requisites for a course
5. What is an example path for a CS Major?
6. How many core CS credits will I be covering each sem in this example?
7. Who all are the faculty of the core courses in Monsoon 2023?
8. What are the elective courses in Monsoon 2023?
9. What all courses are offered in Monsoon?
10. What all courses are offered in Spring?
11. Exit
======================================================
'''
print(menu)
x = clients()
if x == 1:
result = clientSocket.recv(1024).decode('utf-8')
print(f"{result}")
elif x == 2:
result = clientSocket.recv(1024).decode('utf-8')
print(f"Here is the credit breakdown for a CS major \n{result}")
elif x == 3:
result = clientSocket.recv(1024).decode('utf-8')
print(f"Here are all the core courses along with their credits \n{result}")
elif x == 4:
#req = input("Enter the integer of the core course you want to know the pre-requisite for : ")
#clientSocket.send(req.encode('utf-8'))
response_message = clientSocket.recv(1024).decode('utf-8')
print(f"PreRequisite(s) : \n{response_message}")
elif x == 5:
result = clientSocket.recv(1024).decode('utf-8')
print(f"Here is an example path for CS Majors \n{result}")
elif x == 6:
result = clientSocket.recv(1024).decode('utf-8')
print(f"Here are the Core Credits per sem for the above exmaple \n{result}")
elif x == 7:
prof = input("Enter the integer of the core course you want to know the professor for : ")
clientSocket.send(prof.encode('utf-8'))
prof = clientSocket.recv(1024).decode('utf-8')
print(f"Professor : {prof}\n")
elif x == 8:
result = clientSocket.recv(1024).decode('utf-8')
print(f"Here are the Monsoon 2023 CS Elective Courses : \n{result}")
elif x == 9:
result = clientSocket.recv(1024).decode('utf-8')
print(f"Here are the Monsoon CS Courses : \n{result}")
elif x == 10:
result = clientSocket.recv(1024).decode('utf-8')
print(f"Here are the spring CS Courses : \n{result}")
elif x == 11:
break
else:
print("Invalid Input! Enter again\n")
clientSocket.close()
I want to let the user running this stay in an infinite loop until they want to exit. However when i run it and give an input, it only works for the first time. The second time i give it an input in the same run, it freezes, and then the third time it results in a broken pipe error. I know I am doing something wrong with sending and receiving, but I cannot figure it out. Please help