Can I test broadcast packets on a single machine?
Solution 1
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.
Solution 2
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.
Related videos on Youtube
chrisapotek
I enjoy playing with technology and programming. Hope I can help you.
Updated on September 18, 2022Comments
-
chrisapotek over 1 year
I want to have a server sending broadcast UDP packets and two other clients, in the same machine, receiving them. Can I do that somehow? What IP address would I use?
@gravyface gave me hope but I tried:
- Server sending to 127.255.255.255:54321 and clients listening to 0.0.0.0:54321.
- Server sending to 127.255.255.255:54321 and clients listening to 127.0.0.1:54321.
- server sending to 127.255.255.255:54321 and clients listening to 127.255.255.255:54321.
None of them worked! :(
OBS: I am using REUSE_ADDR and SO_BROADCAST options.
-
chrisapotek over 11 yearsIt did NOT work. :( :( :( See my edit in the question.
-
Univ426 over 11 yearsWhat OS are you running? You might confirm that you have a broadcast address for the loop back set, there are cases when lo is brought up with no broadcast set.
-
chrisapotek over 11 yearsYou are very right. I am using OS/X and lo0 has no broadcast flag => lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384. Is there a fix for that?
-
chrisapotek over 11 yearsHey sch, thanks for the info, but my problem is that my loopback does NOT have the broadcast flag turned on (lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384_. Is there a way to fix that?
-
sandeep seervi over 11 yearsNeither does mine, but I'm still able to have one process talk to many. See also my recent edit.
-
chrisapotek over 11 yearsI don't see those addresses when I run the ip command. :( I need to set them up somehow. Not sure how => serverfault.com/questions/421389/…