0

i have this server code:

import socket 
import threading

HEADER = 64
PORT = 5050
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!DISCONNECT"
conn_list = []
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)

def handle_client(conn, addr):
    print(f"[NEW CONNECTION] {addr} connected.")

    connected = True
    while connected:
        msg_length = conn.recv(HEADER).decode(FORMAT)
        if msg_length:
            msg_length = int(msg_length)
            print(f"length: {msg_length}")
            msg = conn.recv(msg_length).decode(FORMAT)
            print(f"msg: {msg}")
            if msg == DISCONNECT_MESSAGE:
                connected = False

            print(f"[{addr}] {msg}")
            for client in conn_list:
                client.send(f"{msg}".encode(FORMAT))

        

def start():
    server.listen()
    print(f"[LISTENING] Server is listening on {SERVER}")
    while True:
        conn, addr = server.accept()
        if conn not in conn_list:
            conn_list.append(conn)
        thread = threading.Thread(target=handle_client, args=(conn, addr))
        thread.start()
        print(f"[ACTIVE CONNECTIONS] {threading.activeCount() - 1}")


print("[STARTING] server is starting...")
start()

and i have this client code:

import socket

HEADER = 64
PORT = 5050
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!DISCONNECT"
SERVER = "address"
ADDR = (SERVER, PORT)

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)

def send(msg):
    message = msg.encode(FORMAT)
    msg_length = len(message)
    send_length = str(msg_length).encode(FORMAT)
    send_length += b' ' * (HEADER - len(send_length))
    client.send(send_length)
    client.send(message)
    print(client.recv(2048).decode(FORMAT))

while True:
    message = input("enter the message: ")
    send(message)

my goal with this is to have a sort of chatroom where a client can type something which gets sent to the server and then the server sends it to all connected clients. my approach for this was having a list of connections and everytime a new client is connected to the server add the connection to the list and then when the server recieves a message from the client it loops through each connection which then gets sent to back to all the connections stored in the connection list. the output im getting is that only the person that actually wrote the message can see it on his terminal while the other clients cant and the server does show each message every client sends. would like some help and ideas on how to make this work.

Punzicul
  • 51
  • 1
  • 5
  • Have a look at this -- it sounds to me like you need a multicast address. https://stackoverflow.com/questions/10692956/what-does-it-mean-to-bind-a-multicast-udp-socket – mzimmers May 03 '22 at 22:50
  • at this moment client receiver some message from server after it sends message to server. But client would need second thread only to receiver new messages from server. OR main thread would have to run loop which periodically send message from client to server and server would send new message in response – furas May 04 '22 at 00:47

0 Answers0