5

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:

  1. Server sending to 127.255.255.255:54321 and clients listening to 0.0.0.0:54321.
  2. Server sending to 127.255.255.255:54321 and clients listening to 127.0.0.1:54321.
  3. 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.

jscott
  • 24,484
  • 8
  • 79
  • 100
chrisapotek
  • 585
  • 2
  • 6
  • 17

2 Answers2

10

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.

sch
  • 560
  • 4
  • 13
  • Hey sch, thanks for the info, but my problem is that my loopback does NOT have the broadcast flag turned on (lo0: flags=8049 mtu 16384_. Is there a way to fix that? – chrisapotek Aug 26 '12 at 17:24
  • Neither does mine, but I'm still able to have one process talk to many. See also my recent edit. – sch Aug 26 '12 at 17:37
  • I don't see those addresses when I run the ip command. :( I need to set them up somehow. Not sure how => http://serverfault.com/questions/421389/how-to-add-a-broadcast-address-to-loopback-with-ifconfig – chrisapotek Aug 26 '12 at 17:47
1

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.

gravyface
  • 13,957
  • 19
  • 68
  • 100
  • It did NOT work. :( :( :( See my edit in the question. – chrisapotek Aug 26 '12 at 16:55
  • 1
    What 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. – Univ426 Aug 26 '12 at 17:12
  • You are very right. I am using OS/X and lo0 has no broadcast flag => lo0: flags=8049 mtu 16384. Is there a fix for that? – chrisapotek Aug 26 '12 at 17:16