0

I've been working on a fully function Fallout Terminal and so far have incorporated many features, one of which is a chat room to chat between others running the script. I improvised a small chat room and it works perfectly, until after around 10 messages back and forth, the messages stop being received and/or sent, and end up freezing the program in a loop of pure 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:
            charp(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()

8
  • I can't get this code to fail. I alternately send many messages and they are all received. I can exit by sending EXIT. Commented Jun 28, 2024 at 17:22
  • I think what might be happening is that one side is trying to send an empty message (just press return when prompted for input) which actually doesn't send anything. That advances the state of one side but not the other so they end up both waiting forever to read data from the other. Commented Jun 28, 2024 at 17:25
  • @quamrana Okay, thank you. Maybe i need to restart my computer, its not acting right. Commented Jun 28, 2024 at 17:26
  • The "protocol" here is extremely inflexible and fragile. There's no way for one side to send two messages in a row and there's the very real possibility of deadlock. Commented Jun 28, 2024 at 17:26
  • Also, it's never a good idea to tempt the programming gods by saying that your code runs perfectly ;) Commented Jun 28, 2024 at 17:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.