TrickyTaco11
TrickyTaco11

Reputation: 44

Messaging system in python using socket

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

Upvotes: 0

Views: 90

Answers (0)

Related Questions