I'm implementing a UDP communication system where a sender broadcasts a packet, and a receiver (Python code) listens for it. The packet is captured by Wireshark on the receiver host, but the Python socket is not receiving the broadcasted UDP packet when the sender and receiver are on different hosts in the same network.
When I run both sender and receiver on localhost, the packet is received. However, when sending from one host to another over the network, the receiver socket doesn't capture it, even though Wireshark shows the packet on the network on the receiving host.
What I’ve tried:
- I verified that the Python socket is correctly bound to the right port (37020) and IP address (0.0.0.0 for all interfaces).
- The socket has the broadcast option enabled (socket.SO_BROADCAST).
- The packet is being broadcast using the address 255.255.255.255.
- Firewalls and security software are not the problem; I even tried turning off the firewall completely to rule out any potential blocking of UDP traffic on port 37020.
- The issue arises when sending the packet from one host to another on the same network. When both the sender and receiver are on localhost, the packet is received correctly.
Receiver:
import socket def start_udp_server(): with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as server_socket: server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) server_socket.bind(('0.0.0.0', 37020)) # Binding to all interfaces print("Server listening on port 37020...") while True: message, addr = server_socket.recvfrom(4096) # Listen for incoming packets print(f"Received message: {message} from {addr}") Sender:
import socket def send_broadcast(): with socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) as sock: sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) message = "Test message" sock.sendto(message.encode(), ('<broadcast>', 37020)) What I checked:
- Binding: The Python socket is bound to the correct address and port.
- Broadcast: The socket is set to allow broadcasting.
- Network: Both sender and receiver are on the same subnet, and the broadcast address 255.255.255.255 should work. The issue is only present across hosts; on localhost, the broadcast packet is received.
Questions:
Why is the UDP broadcast packet captured by Wireshark but not received by the Python socket when the sender and receiver are on different hosts in the same network?
What could be the issue?
How can I debug it further?