C# 如何动态获取本地IP广播地址#

C# 如何动态获取本地IP广播地址#,c#,network-programming,broadcast,C#,Network Programming,Broadcast,我的第一个问题是,解决这个问题的一个办法是避免 IPAddress.Broadcast 所以我写了一个函数来确定本地广播: private IPAddress get_broadcast() { try { string ipadress; IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); // get a list of all lo

我的第一个问题是,解决这个问题的一个办法是避免

IPAddress.Broadcast
所以我写了一个函数来确定本地广播:

    private IPAddress get_broadcast()
    {
        try
        {
            string ipadress;
            IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); // get a list of all local IPs
            IPAddress localIpAddress = ipHostInfo.AddressList[0]; // choose the first of the list
            ipadress = Convert.ToString(localIpAddress); // convert to string
            ipadress = ipadress.Substring(0, ipadress.LastIndexOf(".")+1); // cuts of the last octet of the given IP 
            ipadress += "255"; // adds 255 witch represents the local broadcast
            return IPAddress.Parse(ipadress); 
        }
        catch (Exception e)
        {
            errorHandler(e);
            return IPAddress.Parse("127.0.0.1");// in case of error return the local loopback
        }
    }
但这只适用于/24网络我经常在/24(在家)和/16(在工作)网络之间切换。所以硬编码的子网掩码不符合我的要求

那么,有没有什么好方法可以不用“IPAddress.broadcast”来确定本地广播呢?

公共静态IPAddress GetBroadcastAddress(这个IPAddress地址,IPAddress子网掩码)
{
字节[]ipAdressBytes=地址。GetAddressBytes();
byte[]subnetMaskBytes=subnetMask.GetAddressBytes();
if(ipAdressBytes.Length!=subnetMaskBytes.Length)
抛出新ArgumentException(“IP地址和子网掩码的长度不匹配”);
byte[]broadcastAddress=新字节[ipAdressBytes.Length];
for(int i=0;i
从此处采取的解决方案:

公共静态IPAddress GetBroadcastAddress(此IPAddress地址,IPAddress子网掩码)
{
字节[]ipAdressBytes=地址。GetAddressBytes();
byte[]subnetMaskBytes=subnetMask.GetAddressBytes();
if(ipAdressBytes.Length!=subnetMaskBytes.Length)
抛出新ArgumentException(“IP地址和子网掩码的长度不匹配”);
byte[]broadcastAddress=新字节[ipAdressBytes.Length];
for(int i=0;i
从此处采取的解决方案:

IPAddress GetBroadCastIP(IPAddress主机,IPAddress掩码)
{
字节[]IPBytes=新字节[4];
byte[]hostBytes=host.GetAddressBytes();
字节[]maskBytes=mask.GetAddressBytes();
对于(int i=0;i<4;i++)
{
broadcastIPBytes[i]=(字节)(主机字节[i]|(字节)~maskBytes[i]);
}
返回新的IPAddress(广播IPBytes);
}
IPAddress GetBroadCastIP(IPAddress主机,IPAddress掩码)
{
字节[]IPBytes=新字节[4];
byte[]hostBytes=host.GetAddressBytes();
字节[]maskBytes=mask.GetAddressBytes();
对于(int i=0;i<4;i++)
{
broadcastIPBytes[i]=(字节)(主机字节[i]|(字节)~maskBytes[i]);
}
返回新的IPAddress(广播IPBytes);
}

我知道这很旧,但我不喜欢循环,所以这里还有一个解决方案:

    public static IPAddress GetBroadcastAddress(UnicastIPAddressInformation unicastAddress)
    {
       return GetBroadcastAddress(unicastAddress.Address, unicastAddress.IPv4Mask);
    }

    public static IPAddress GetBroadcastAddress(IPAddress address, IPAddress mask)
    {
        uint ipAddress = BitConverter.ToUInt32(address.GetAddressBytes(), 0);
        uint ipMaskV4 = BitConverter.ToUInt32(mask.GetAddressBytes(), 0);
        uint broadCastIpAddress = ipAddress | ~ipMaskV4;

        return new IPAddress(BitConverter.GetBytes(broadCastIpAddress));
    }

我知道这很旧,但我不喜欢循环,所以这里还有一个解决方案:

    public static IPAddress GetBroadcastAddress(UnicastIPAddressInformation unicastAddress)
    {
       return GetBroadcastAddress(unicastAddress.Address, unicastAddress.IPv4Mask);
    }

    public static IPAddress GetBroadcastAddress(IPAddress address, IPAddress mask)
    {
        uint ipAddress = BitConverter.ToUInt32(address.GetAddressBytes(), 0);
        uint ipMaskV4 = BitConverter.ToUInt32(mask.GetAddressBytes(), 0);
        uint broadCastIpAddress = ipAddress | ~ipMaskV4;

        return new IPAddress(BitConverter.GetBytes(broadCastIpAddress));
    }

谢谢,但是我需要子网掩码和类似的问题。我怎样才能得到我的IP的子网掩码?谢谢,但为此我需要子网掩码和类似的问题。如何获取我的IP的子网掩码?
    public static IPAddress GetBroadcastAddress(UnicastIPAddressInformation unicastAddress)
    {
       return GetBroadcastAddress(unicastAddress.Address, unicastAddress.IPv4Mask);
    }

    public static IPAddress GetBroadcastAddress(IPAddress address, IPAddress mask)
    {
        uint ipAddress = BitConverter.ToUInt32(address.GetAddressBytes(), 0);
        uint ipMaskV4 = BitConverter.ToUInt32(mask.GetAddressBytes(), 0);
        uint broadCastIpAddress = ipAddress | ~ipMaskV4;

        return new IPAddress(BitConverter.GetBytes(broadCastIpAddress));
    }