diff --git a/UdpListener.py b/UdpListener.py index 3ea4f51..ded2a38 100644 --- a/UdpListener.py +++ b/UdpListener.py @@ -1,13 +1,19 @@ import socket +import struct def start_server(ip, port): - server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - server_socket.bind((ip, port)) + server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) + server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # Enable port reusage + server_socket.bind(('', port)) # Bind to the port, not the multicast IP print(f"UDP server listening on {ip}:{port}") + + # Join the multicast group + mreq = struct.pack("4sl", socket.inet_aton(ip), socket.INADDR_ANY) + server_socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) while True: data, addr = server_socket.recvfrom(1024) # buffer size is 1024 bytes print(f"received message: {data} from {addr}") if __name__ == "__main__": - start_server("localhost", 8888) + start_server("239.1.2.3", 1234) diff --git a/UdpMocker.py b/UdpMocker.py index f70d683..3561f05 100644 --- a/UdpMocker.py +++ b/UdpMocker.py @@ -2,8 +2,8 @@ import socket import time # UDP Configuration -ip = "localhost" # Change this to the IP address of your computer or the network address to multicast -port = 8888 # Ensure this matches the port number in your Unity script +ip = "239.1.2.3" # Change this to the IP address of your computer or the network address to multicast +port = 1234 # Ensure this matches the port number in your Unity script # Create the UDP socket sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)