Files
charade_experiment/experiment-scripts/server.py
2025-09-17 13:19:48 +02:00

52 lines
1.6 KiB
Python

import socket
import threading
from datetime import datetime
from time import sleep
CONTROL_ADDR = ("127.0.0.1", 5001)
# TODO: Adjust the following addresses so they match the IP addresses of the
# VR headsets.
# In our case the IP addresses were:
# - for player 1: 10.42.0.38
# - for player 2: 10.42.0.72
#
# The ports are hardcoded to 5001 inside the Unity application, so you
# shouldn't change those.
#
# Note: For this to work the VR headsets must be connected to the same network
# as this server.
DEVICE1_ADDR = ("10.42.0.38", 5001)
DEVICE2_ADDR = ("10.42.0.72", 5001)
sock_from_A = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock_from_A.bind(("0.0.0.0", 5000))
def forward(source_socket):
while True:
try:
data, addr = source_socket.recvfrom(1024 * 16)
target_ip = DEVICE1_ADDR[0] if addr[0] == DEVICE2_ADDR[0] else DEVICE2_ADDR[0]
label = "A→B" if addr == DEVICE1_ADDR else "B→A"
if addr != DEVICE1_ADDR and addr != DEVICE2_ADDR:
label = f"unknown {addr}"
sock_from_A.sendto(data, (target_ip, 5000))
timestamp = datetime.now().strftime("%H:%M:%S")
# Logging
#if next(counter) % 20 == 0:
# if addr[0] != DEVICE2_ADDR[0]:
# print(f"[{timestamp}] {label}: {data.decode()}")
# print('sent to ', (target_ip, 5000))
except Exception as e:
print(f"Fehler {label}: {e}")
print("UDP Relay läuft. Strg+C zum Beenden.")
try:
forward(sock_from_A)
except KeyboardInterrupt:
sock_from_A.close()
print("\nUDP Relay beendet.")