import socket from datetime import datetime # Track connected devices dynamically # No manual IP configuration needed! connected_devices = {} # {ip: last_seen_timestamp} 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) source_ip = addr[0] timestamp = datetime.now() # Register this device connected_devices[source_ip] = timestamp # Get list of other active devices (excluding localhost and source) other_devices = [ip for ip in connected_devices.keys() if ip != source_ip and not ip.startswith("127.")] # Forward to all other connected devices for target_ip in other_devices: sock_from_A.sendto(data, (target_ip, 5000)) # Also forward to app.py tracking listener on port 5002 # Prepend source IP so app.py can identify which player sent the data tagged_data = f"SOURCE_IP:{source_ip}|".encode('utf-8') + data sock_from_A.sendto(tagged_data, ("127.0.0.1", 5002)) # Logging if len(other_devices) > 0: label = f"{source_ip} → {', '.join(other_devices)}" else: label = f"{source_ip} (no other devices connected)" # Uncomment for verbose logging: # print(f"[{timestamp.strftime('%H:%M:%S')}] {label}") except Exception as e: print(f"Error in relay: {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.")