-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat-server.py
More file actions
56 lines (47 loc) · 1.71 KB
/
chat-server.py
File metadata and controls
56 lines (47 loc) · 1.71 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import socket
import threading
# Create a socket for the server
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Define the server address and port
server_address = ('127.0.0.1', 12345)
# Bind the server socket to the address and port
server_socket.bind(server_address)
# Listen for incoming connections
server_socket.listen(5)
print("Chat server is listening on", server_address)
# List to store connected clients
connected_clients = []
# Function to broadcast messages to all clients
def broadcast(message, client_socket):
for client in connected_clients:
if client != client_socket:
try:
client.send(message)
except:
# Remove the client if unable to send message
remove_client(client)
# Function to remove a client from the list
def remove_client(client_socket):
if client_socket in connected_clients:
connected_clients.remove(client_socket)
# Function to handle a client's connection
def client_handler(client_socket):
while True:
try:
message = client_socket.recv(1024)
if message:
print(message.decode('utf-8'))
broadcast(message, client_socket)
else:
# Remove the client if no data received
remove_client(client_socket)
except:
continue
# Accept and handle incoming connections
while True:
client_socket, client_address = server_socket.accept()
connected_clients.append(client_socket)
print(f"Connected to {client_address}")
# Create a thread to handle the client
client_thread = threading.Thread(target=client_handler, args=(client_socket,))
client_thread.start()