I'm trying to implement a UDP communication system where a sender broadcasts a packet, and a receiver (Python code) listens for it. The packet is successfully captured by Wireshark on the receiver host, but my Python socket is not receiving the broadcasted UDP packet.
I verified that the Python socket is correctly bound to the right port (37020) and IP address (0.0.0.0 for all interfaces).
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 socket isn't receiving the broadcasted packet, even though Wireshark can see it on the network.
Receiver:
import socket
def start_udp_server():
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as server_socket:
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’ve 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 network, and the broadcast address 255.255.255.255 should work.
<broadcast>with my local broadcast address192.168.1.255it worked.setsockopt.0.0.0.0in the receiver should listen on all interfaces.