- Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathserver.py
More file actions
Latest commit
58 lines (45 loc) · 1.27 KB
/
Copy pathserver.py
File metadata and controls
58 lines (45 loc) · 1.27 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
importsocket
from_threadimport*
importsys
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server='localhost'
port=5555
server_ip=socket.gethostbyname(server)
try:
s.bind((server, port))
exceptsocket.errorase:
print(str(e))
s.listen(2)
print("Waiting for a connection")
currentId="0"
pos= ["0:50,50", "1:100,100"]
defthreaded_client(conn):
globalcurrentId, pos
conn.send(str.encode(currentId))
currentId="1"
reply=''
whileTrue:
try:
data=conn.recv(2048)
reply=data.decode('utf-8')
ifnotdata:
conn.send(str.encode("Goodbye"))
break
else:
print("Recieved: "+reply)
arr=reply.split(":")
id=int(arr[0])
pos[id] =reply
ifid==0: nid=1
ifid==1: nid=0
reply=pos[nid][:]
print("Sending: "+reply)
conn.sendall(str.encode(reply))
except:
break
print("Connection Closed")
conn.close()
whileTrue:
conn, addr=s.accept()
print("Connected to: ", addr)
start_new_thread(threaded_client, (conn,))