-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
47 lines (38 loc) · 1.32 KB
/
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
47
import threading
import socket
host = 'localhost'
port = 9999
#Create the server
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("", port))
server.listen()
clients = []
#Send message to client
def broadcast(coordinants, client): #Send coordinate list to client
for player in clients:
if player != client:
print("Sending data. . .")
player.send(coordinants)
return
#Handle client connections
def handleClient(client):
while True:
try:
message = client.recv(1024) #Message comes from client, set max bytes as 1024
broadcast(message, client) #Send info to other player
print("Sending message. . .")
except socket.error:
clients.remove(client)
break #End loop
#Main function to get connection
def recieve():
while True:
if len(clients) < 3: #Only accept 2 players at once!
print("Listening for connections. . .")
client, address = server.accept() #Waiting for any connection
clients.append(client)
print(f"Connection is established with player {len(clients)}.")
thread = threading.Thread(target = handleClient, args=(client,)) #New thread for client
thread.start()
if __name__ == "__main__":
recieve()