-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
42 lines (35 loc) · 1.26 KB
/
server.py
File metadata and controls
42 lines (35 loc) · 1.26 KB
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
import socket
import threading
HOST = '127.0.0.1' # Server IP address
PORT = 5000 # Port to listen on
clients = [] # List to store connected clients
def handle_client(client_socket):
"""Handle messages from a client and broadcast to others."""
while True:
try:
# Receive data from the client
data = client_socket.recv(1024)
if not data:
break
# Broadcast data to all other clients
for client in clients:
if client != client_socket:
client.send(data)
except:
break
# Remove client from list when disconnected
clients.remove(client_socket)
client_socket.close()
def start_server():
"""Start the server and accept incoming connections."""
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()
print(f"New connection from {addr}")
clients.append(client_socket)
threading.Thread(target=handle_client, args=(client_socket,)).start()
if __name__ == "__main__":
start_server()