1

I'm using the following c# code to send out a UDP broadcast. It connects to a Python app which listens for these broadcasts.

UdpClient udpOut = new UdpClient();
udpBeacon = Encoding.ASCII.GetBytes("hello");
udpOut.EnableBroadcast = true;
IPEndPoint udpEp = new IPEndPoint(IPAddress.Broadcast, 7223);
udpOut.Send(udpBeacon, udpBeacon.Length, udpEp);

It works great when both client and server are running on localhost, but I failed to get it to work between two machines connected to WiFi on the same subnet. After some experimentation, this turned out to be because VirtualBox had installed a virtual network adapter and that was all the broadcast was being sent over.

My question is: How do I modify this code to send on all local interfaces? Do I have to manually iterate through Dns.GetHostEntry(Dns.GetHostName()).AddressList and start a connection on each interface?

Chris Rae
  • 5,627
  • 2
  • 36
  • 51
  • Possible duplicate of [Broadcasting UDP message to all the available network cards](http://stackoverflow.com/questions/1096142/broadcasting-udp-message-to-all-the-available-network-cards) – Ilian Mar 03 '17 at 00:38
  • 1
    @IlianPinzon: I'm not 100% sure your proposed duplicate is the right one. That appears to be a question about ensuring that the broadcast is _received by_ all installed adapters, while the above seems to be about ensuring that the broadcast is _sent by_ all installed adapters. Depending on what the OP is actually doing, the two might be equivalent in consequence, but the question is too vaguely written to really know for sure. – Peter Duniho Mar 03 '17 at 00:56
  • The accepted answer on the linked question says "This is actually trickier that it sounds because if you have more than one interface the broadcasts will not always go out all of the interfaces. To get around this I created this class."... So I think it tries to solve how to send on multiple interfaces. – Ilian Mar 03 '17 at 01:18
  • (This is my question) I did see the other question before I posted this; he seems to be solving the problem of sending broadcast packets to known IP addresses (and following their appropriate routing tables). My problem is that I don't know all of the IPs I'm sending to, I just want to broadcast on all my available subnets. @PeterDuniho - would be more than happy to reword if you can help me with what's missing. – Chris Rae Mar 03 '17 at 17:28

0 Answers0