forked from makazis/Placeholdeer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtpl_server.py
46 lines (40 loc) · 1.32 KB
/
mtpl_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
import socket
import threading
# Server settings
HOST = '127.0.0.1' # Localhost
PORT = 12345 # Port to listen on
# List to store connected clients
clients = []
# Function to handle each client
def handle_client(client_socket, addr):
print(f"New connection: {addr}")
while True:
try:
message = client_socket.recv(1024).decode('utf-8')
if not message:
break
print(f"Received from {addr}: {message}")
broadcast(f"{addr}: {message}", client_socket)
except ConnectionResetError:
break
print(f"Connection closed: {addr}")
clients.remove(client_socket)
client_socket.close()
# Function to broadcast messages to all clients
def broadcast(message, sender_socket):
for client in clients:
if client != sender_socket:
client.sendall(message.encode('utf-8'))
# Main server function
def start_server():
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, PORT))
server.listen(5)
print(f"Server started on {HOST}:{PORT}")
while True:
client_socket, addr = server.accept()
clients.append(client_socket)
thread = threading.Thread(target=handle_client, args=(client_socket, addr))
thread.start()
if __name__ == "__main__":
start_server()