Can I test broadcast packets on a single machine?

First window:

socat -u udp-recv:12345,reuseaddr -

Second window:

socat -u udp-recv:12345,reuseaddr -

Third window

socat - udp-sendto:127.255.255.255:12345,broadcast

Then enter a few lines of text in the 3rd window and see if you're getting anything in the two other ones.

Replace "socat" with "strace -fe network socat" to see what system calls are actually being made (assuming you're on Linux, other unices have equivalents sometimes called tusc, struss or dtruss). socat is opensource and binary packages are available for most operating systems.

socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP) = 3
setsockopt(3, SOL_SOCKET, SO_BROADCAST, [1], 4) = 0
sendto(3, "qwe\n", 4, 0, {sa_family=AF_INET, sin_port=htons(12345), sin_addr=inet_addr("127.255.255.255")}, 16) = 4

Above, the receiving "clients" bind to the INADDR_ANY address. What I found and am not sure why, is that if you bind to an address on the loopback subnet, you're not seeing the packets coming in.

See also:

$ ip route show table local dev lo scope link
broadcast 127.0.0.0  proto kernel  src 127.0.0.1
broadcast 127.255.255.255  proto kernel  src 127.0.0.1

If the clients bind to 127.0.0.0 or 127.255.255.255 and the server sends to that same address (with SO_BROADCAST), then it works as well.


Sending broadcast traffic to 127.255.255.255 should work, but obviously test it out (and Wireshark/tcpdump is your friend here).

Obviously your clients need to be listening on the loopback device too.