I improvised a small chat room and it works perfectly, until after around 10 messages back and forth, the messages stop being received, and end up being freezing the program in a loop of nothingness. I’m not sure why or how to fix this. Can anyone help?
the ‘return_menu()’ doesn’t change anything, it simply just takes it out of the loop back to a homepage, doesn’t effect the code of the message system in any way.
Definitions
import socket
import time
def charp(userInput, speed):
for char in userInput:
print(char, end='')
sys.stdout.flush()
time.sleep(speed)
e()
def e():
print("")
Server Code
def server_host():
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("localhost", 9999))
server.listen()
client, addr = server.accept()
charp("| Save user's name as: ", 0.05)
msgServer = input("")
e()
while True:
msg = client.recv(1024).decode('utf-8')
if msg == 'EXIT':
client.send("EXIT".encode('utf-8'))
break
else:
char`your text`p(msgServer + ": " + msg, 0.05)
client.send(input("You: ").encode('utf-8'))
client.close()
server.close()
return_menu()
Client code
def client_host():
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(("localhost", 9999))
charp("| Save user's name as: ", 0.05)
msgClient = input("")
e()
while True:
client.send(input("You: ").encode('utf-8'))
msg = client.recv(1024).decode('utf-8')
if msg == "EXIT":
client.send("EXIT".encode('utf-8'))
break
else:
charp(msgClient + ": " + msg, 0.05)
client.close()
return_menu()
Tao Connors is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.