pythonpython-sockets

Socket server stops running upon client connecting


I'm building a chat room using socket library and threading. The server should let multiple clients connect while receiving data from each client and broadcasting it to all the other clients using threading.

After running the server, I run the client code but then my server stops running.

server.py

import socket
import threading

host = socket.gethostbyname(socket.gethostname())
port = 5000
client_conns = []

s = socket.socket()
s.bind((host, port))
s.listen(10)

def broadcast_msg(msg):
    for conn in client_conns:
        conn.send(msg)

def handle_client(conn, addr):
    while True:
        msg = conn.recv(4096).decode()
        broadcast_msg(msg)
        

while True:
    conn, address = s.accept()
    print(f"Connection from: {address}")
    clients.append(conn)

    threading._start_new_thread(handle_client, (conn, address))

client.py

import socket

def client():
    host = socket.gethostbyname(socket.gethostname())
    port = 5000

    s = socket.socket()
    s.connect((host, port))

    alias = input("Alias: ")

    while True:
        message = input(f"{alias}: ")
        message = alias + ": " + message
        s.send(message.encode())

        data = s.recv(4096).decode()
        print(data)

client()

Solution

  • I featured it out

    in your server you have variable client_conns but you add every connection to clients which is not declared

    your client code was pretty much ok in exception of not having exception handling:

    also for the line:

    threading._start_new_thread(handle_client, (conn, address))
    

    the function and methods that start with underline character are not meant for usage outside module

    also you have to encode data sent between sockets to bytes

    the fixed server and client codes are as followed:

    server.py:

    import socket
    import threading
    
    host = socket.gethostbyname(socket.gethostname())
    port = 5000
    client_conns = []
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((host, port))
    s.listen(10)
    
    def broadcast_msg(msg):
        for conn in client_conns:
            conn.send(bytes(msg, 'utf-8'))
    
    def handle_client(conn):
        while True:
            msg = conn.recv(4096).decode()
            broadcast_msg(msg)
            
    
    while True:
        conn, address = s.accept()
        print(f"Connection from: {address}")
        client_conns.append(conn)
        
        threading.Thread(target=handle_client, args=[conn]).start()
    

    client.py:

    import socket
    
    def client():
        host = socket.gethostbyname(socket.gethostname())
        port = 5000
    
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((host, port))
    
        alias = input("Alias: ")
    
        while True:
            message = input(f"{alias}: ")
            message = alias + ": " + message
            s.send(message.encode())
    
            data = s.recv(4096).decode()
            print(data)
    
    client()