-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmul_server.py
34 lines (29 loc) · 981 Bytes
/
mul_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
import socket
import threading
host = socket.gethostname()
port = 12345
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((host, port))
server_socket.listen(5)
print("Waiting for connection...")
client_sockets = []
client_usernames = []
def broadcast(message):
for client_socket in client_sockets:
client_socket.send(message.encode())
def handle_client(client_socket):
while True:
try:
message =client_socket.recv(1024).decode()
broadcast(message)
except:
client_sockets.remove(client_socket)
client_socket.close()
break
while True:
client, address = server_socket.accept()
print(f"[*] Accepted connection from {address[0]}:{address[1]}")
client_sockets.append(client)
client_thread = threading.Thread(target=handle_client, args=(client,))
client_thread.start()