-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
66 lines (46 loc) · 1.52 KB
/
main.py
File metadata and controls
66 lines (46 loc) · 1.52 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
import socket
import threading
HOST = "192.168.56.1"
PORT = 9999
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, PORT))
server.listen()
clients = []
nicknames = []
def broadcast(message):
for client in clients:
client.send(message)
def handle(client):
while True:
try:
message = client.recv(1024)
sender_index = clients.index(client)
sender_nickname = nicknames[sender_index]
print(f"{sender_nickname} says: {message.decode('utf-8')}")
broadcast(message)
except:
handle_disconnect(client)
break
def handle_disconnect(client):
index = clients.index(client)
client.close()
nickname = nicknames[index]
clients.remove(client)
nicknames.remove(nickname)
print(f"{nickname} has disconnected.")
broadcast(f"{nickname} has disconnected.\n".encode('utf-8'))
def receive():
while True:
client, address = server.accept()
print(f"Connected with {str(address)}")
client.send("NICK".encode("utf-8"))
nickname = client.recv(1024).decode("utf-8")
nicknames.append(nickname)
clients.append(client)
print(f"{nickname} has connected to the server!")
broadcast(f"{nickname} has connected to the server!\n".encode('utf-8'))
client.send("Connected to the server".encode("utf-8"))
thread = threading.Thread(target=handle, args=(client,))
thread.start()
print("Server running...")
receive()