0

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.

17
  • 1
    When I replaced <broadcast> with my local broadcast address 192.168.1.255 it worked. Commented Nov 13, 2024 at 21:18
  • Related: Sending broadcast in Python, UDP-Broadcast on all interfaces Is it possible that your sender has multiple interfaces, and you are simply not braodcasting on the interface that the receiver is listening to? Commented Nov 13, 2024 at 21:34
  • Code as is worked for me running both on the same system and failed as expected when commenting out setsockopt. Commented Nov 13, 2024 at 21:36
  • @RemyLebeau 0.0.0.0 in the receiver should listen on all interfaces. Commented Nov 13, 2024 at 22:14
  • @Barmar yes, but sendto can't broadcast on all interfaces, so it's possible that it sends on an interface that is not connected to an interface that the receiver is listening on. That's why I ask. Commented Nov 14, 2024 at 1:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.