forked from SchneiderMarius/mouse-vs-ai-foraging-unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp_test_server.py
More file actions
52 lines (45 loc) · 1.54 KB
/
Copy pathtcp_test_server.py
File metadata and controls
52 lines (45 loc) · 1.54 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
#!/usr/bin/env python3
import socket
import struct
import random
import sys
def recvall(sock, n):
"""Read exactly n bytes from the socket or return None on EOF."""
data = b''
while len(data) < n:
packet = sock.recv(n - len(data))
if not packet:
return None
data += packet
return data
def main(host='127.0.0.1', port=12345):
# 1) Create, bind, and listen
srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
srv.bind((host, port))
srv.listen(1)
print(f"[SERVER] Listening on {host}:{port}…")
conn, addr = srv.accept()
print(f"[SERVER] Connection from {addr}")
try:
while True:
# 2) Read 4 bytes (Unity’s reward)
data = recvall(conn, 4)
if data is None:
print("[SERVER] Client disconnected")
break
reward = struct.unpack('<f', data)[0]
print(f"[SERVER] Got reward = {reward:.3f}")
# 3) Decide your action (here, random in [-1,1] for demo)
action = [random.uniform(-1,1) for _ in range(3)]
payload = b''.join(struct.pack('<f', a) for a in action)
# 4) Send back exactly 12 bytes
conn.sendall(payload)
print(f"[SERVER] Sent action = {[round(a,3) for a in action]}")
except KeyboardInterrupt:
print("\n[SERVER] Shutting down (keyboard interrupt).")
finally:
conn.close()
srv.close()
if __name__ == '__main__':
main()