-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
55 lines (42 loc) · 1.18 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
48
49
50
51
52
53
54
55
import socket
from _thread import *
from player import Player
import pickle
server = "127.0.0.1"
port = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((server, port))
except socket.error as e:
str(e)
s.listen(2)
print("Waiting for a connection, Server Started")
players = [Player(0,0,50,50,(255,0,0)), Player(100,100, 50,50, (0,0,255))]
def threaded_client(conn, player):
conn.send(pickle.dumps(players[player]))
reply = ""
while True:
try:
data = pickle.loads(conn.recv(2048))
players[player] = data
if not data:
print("Disconnected")
break
else:
if player == 1:
reply = players[0]
else:
reply = players[1]
print("Received: ", data)
print("Sending : ", reply)
conn.sendall(pickle.dumps(reply))
except:
break
print("Lost connection")
conn.close()
currentPlayer = 0
while True:
conn, addr = s.accept()
print("Connected to:", addr)
start_new_thread(threaded_client, (conn, currentPlayer))
currentPlayer += 1