-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_server.py
50 lines (41 loc) · 1.58 KB
/
message_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
40
41
42
43
44
45
46
47
48
49
50
import socket
import threading
# Dictionary to store connected clients and their sockets
clients = {}
# Function to handle a client connection
def handle_client(client_socket):
client_name = client_socket.recv(1024).decode()
clients[client_name] = client_socket
while True:
try:
data = client_socket.recv(1024)
if not data:
break
message = data.decode()
send_message_to_other_clients(client_name, message)
except Exception as e:
print(f"Error handling client {client_name}: {str(e)}")
break
del clients[client_name]
print(f"Connection to {client_name} closed")
client_socket.close()
# Function to send a message to other connected clients
def send_message_to_other_clients(sender_name, message):
for name, socket in clients.items():
if name != sender_name:
try:
socket.send(f"{sender_name}: {message}".encode())
except Exception as e:
print(f"Error sending message to {name}: {str(e)}")
# Create a socket object
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to a specific address and port
server_address = ('localhost', 12345)
server_socket.bind(server_address)
# Listen for incoming connections (up to 5 clients in the queue)
server_socket.listen(5)
print("Server is listening for incoming connections...")
while True:
client_socket, _ = server_socket.accept()
client_thread = threading.Thread(target=handle_client, args=(client_socket,))
client_thread.start()