Josh G
Josh G

Reputation: 14256

Finding all local network broadcast addresses

I'm building a device discovery system using a UDP broadcast message. I started out using UdpClient and IPAddress.Broadcast.

The solution worked for clients on the local machine, but not other clients on the local network.

Via this question, I discovered that Win 7 blocks broadcast messages. When I manually entered the local network broadcast address it worked great. Now I want to write some code that will iterate through all of the local network adapters (something like NetworkInterfaces.GetAllNetworkInterfaces()) and find the local network broadcast address for the network each adapter is connected to, if any.

Does this make sense? What's the best was to ping the local subnet that would be compatible with Win 7, IPv6, IPv4, etc. In other words, universally compatible.

Thanks!

Upvotes: 1

Views: 1409

Answers (1)

Sabre
Sabre

Reputation: 2400

Ok, something like...?

  foreach (NetworkInterface Interface in NetworkInterface.GetAllNetworkInterfaces())
        {
            if (Interface.SupportsMulticast)
            {
                IPInterfaceProperties IPProperties = Interface.GetIPProperties();
                foreach (IPAddressInformation  address in IPProperties.MulticastAddresses)
                {
                    Console.WriteLine(address.Address);
                }
            }
        }
    }

Upvotes: 4

Related Questions