-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChatServer.py
More file actions
73 lines (51 loc) · 1.72 KB
/
Copy pathChatServer.py
File metadata and controls
73 lines (51 loc) · 1.72 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import socket
import threading
import pickle
LISTENER_LIMIT = 4
HOST = "127.0.0.1"
chat_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# PORT=6666
PORT = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((HOST, PORT))
except socket.error as e:
str(e)
s.listen(LISTENER_LIMIT)
print("Waiting for a connection, Server Started")
server_socket = None
clients = []
chat_room = "Chat Room"
def threaded_client(conn, currentplayer):
clients.append(conn)
username=conn.recv(1024).decode("utf-8")
conn.sendall(username.encode())
send_message(conn, f"Welcome to the {chat_room}, {username}!")
while True:
try:
message = conn.recv(1024).decode("utf-8")
if message:
print(f"Received message from {username}: {message}")
broadcast_message(f"{username}: {message}", sender_socket=conn)
else:
clients.remove(conn)
conn.close()
# currentplayer-=1
broadcast_message(f"{username} has left the {chat_room}")
break
except ConnectionResetError:
clients.remove(conn)
conn.close()
broadcast_message(f"{username} has left the {chat_room}")
break
def broadcast_message(message, sender_socket=None):
for client in clients:
client.sendall(message.encode())
def send_message(client_socket, message):
client_socket.sendall(message.encode())
currentPlayer = 0
while True:
conn, addr = s.accept()
print("Connected to:", addr)
print("Accepted connection from", addr[0] + ":" + str(addr[1]))
threading.Thread(target=threaded_client, args=(conn, currentPlayer)).start()