-
Notifications
You must be signed in to change notification settings - Fork 205
/
chat_server.py
40 lines (34 loc) · 1.15 KB
/
chat_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import socket
import threading
def handle_client(client_socket):
"""Handle communication with the connected client."""
while True:
try:
message = client_socket.recv(1024).decode('utf-8')
if not message:
break
print(f"Received: {message}")
broadcast(message, client_socket)
except:
break
client_socket.close()
def broadcast(message, client_socket):
"""Send the message to all connected clients."""
for client in clients:
if client != client_socket:
try:
client.send(message.encode('utf-8'))
except:
client.close()
clients.remove(client)
# Set up the server
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('0.0.0.0', 12345)) # Listen on all interfaces, port 12345
server.listen(5)
print("Server started, waiting for connections...")
clients = []
while True:
client_socket, addr = server.accept()
print(f"Accepted connection from {addr}")
clients.append(client_socket)
threading.Thread(target=handle_client, args=(client_socket,)).start()