How do you get host's broadcast address of the default network adapter? C#

半城伤御伤魂 提交于 2020-01-02 03:13:10

问题


Let's say that I want to send an udp message to every host in my subnet (and then receive an udp message from any host in my subnet):

at the moment I do:

IPAddress broadcast = IPAddress.Parse("192.168.1.255");

but of course I want this to be done dinamically in the event that the subnet is different from 192.168.1/24. I've tried with:

IPAddress broadcast = IPAddress.Broadcast;

but IPAddress.Broadcast represents "255.255.255.255" which can't be used to send messages (it throws an exception)...so:

how do I get the local network adapter broadcast address (or netmask of course)?

THIS IS THE FINAL SOLUTION I CAME UP WITH

public IPAddress getBroadcastIP()
{
    IPAddress maskIP = getHostMask();
    IPAddress hostIP = getHostIP();

    if (maskIP==null || hostIP == null)
        return null;

    byte[] complementedMaskBytes = new byte[4];
    byte[] broadcastIPBytes = new byte[4];

    for (int i = 0; i < 4; i++)
    {
        complementedMaskBytes[i] =  (byte) ~ (maskIP.GetAddressBytes().ElementAt(i));
        broadcastIPBytes[i] = (byte) ((hostIP.GetAddressBytes().ElementAt(i))|complementedMaskBytes[i]);
    }

    return new IPAddress(broadcastIPBytes);

}


private IPAddress getHostMask()
{

    NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces();

    foreach (NetworkInterface Interface in Interfaces)
    {

        IPAddress hostIP = getHostIP();

        UnicastIPAddressInformationCollection UnicastIPInfoCol = Interface.GetIPProperties().UnicastAddresses;

        foreach (UnicastIPAddressInformation UnicatIPInfo in UnicastIPInfoCol)
        {
            if (UnicatIPInfo.Address.ToString() == hostIP.ToString())
            {
                return UnicatIPInfo.IPv4Mask;
            }
        }
    }

    return null;
}

private IPAddress getHostIP()
{
    foreach (IPAddress ip in (Dns.GetHostEntry(Dns.GetHostName())).AddressList)
    {
        if (ip.AddressFamily == AddressFamily.InterNetwork)
            return ip;
    }

    return null;
}

回答1:


If you get the local IP and subnet, it should be no problem to calculate.

Something like this maybe?

using System;
using System.Net.NetworkInformation;

public class test
{
    public static void Main()
    {
        NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces();
        foreach(NetworkInterface Interface in Interfaces)
        {
            if(Interface.NetworkInterfaceType == NetworkInterfaceType.Loopback) continue;
            if (Interface.OperationalStatus != OperationalStatus.Up) continue;
            Console.WriteLine(Interface.Description);
            UnicastIPAddressInformationCollection UnicastIPInfoCol = Interface.GetIPProperties().UnicastAddresses;
            foreach(UnicastIPAddressInformation UnicatIPInfo in UnicastIPInfoCol)
            {
                Console.WriteLine("\tIP Address is {0}", UnicatIPInfo.Address);
                Console.WriteLine("\tSubnet Mask is {0}", UnicatIPInfo.IPv4Mask);
            }
        }
    }
}

How to calculate the IP range when the IP address and the netmask is given? Should give you the rest of it.



来源:https://stackoverflow.com/questions/18551686/how-do-you-get-hosts-broadcast-address-of-the-default-network-adapter-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!