你如何得到默认的网络适配器的主机的广播地址? C# [英] How do you get host's broadcast address of the default network adapter? C#
问题描述
让我们说,我想在我的子网发送一个UDP消息每台主机(然后在我的子网接收来自任何主机的UDP消息):
在现在我做的:
ip地址广播= IPAddress.Parse(192.168.1.255);
但我当然希望这是dinamically完成事件的子网是192.168不同。 1/24。我试着:
ip地址广播= IPAddress.Broadcast;
但IPAddress.Broadcast表示不能被用来发送消息255.255.255.255(它抛出异常)...所以:
我如何获得本地网卡的广播地址(或课程的网络掩码)
这是最后的解决方案,我想出了
公共ip地址getBroadcastIP( )
{
ip地址maskIP = getHostMask();
ip地址主机IP = getHostIP();
如果(maskIP == NULL ||主机IP == NULL)
返回NULL;
字节[] = complementedMaskBytes新的字节[4];
字节[] = broadcastIPBytes新的字节[4];
的for(int i = 0;我4;;我++)
{
complementedMaskBytes [I] =(字节)〜(maskIP.GetAddressBytes()的ElementAt(我));
broadcastIPBytes [I] =(字节)((hostIP.GetAddressBytes()的ElementAt(ⅰ))| complementedMaskBytes [I]);
}
返回新的ip地址(broadcastIPBytes);
}
私有ip地址getHostMask()
{
的NetworkInterface [] =接口NetworkInterface.GetAllNetworkInterfaces();
的foreach(NetworkInterface的接口在接口)
{
ip地址主机IP = getHostIP();
UnicastIPAddressInformationCollection UnicastIPInfoCol = Interface.GetIPProperties()UnicastAddresses。
的foreach(UnicastIPAddressInformation UnicatIPInfo在UnicastIPInfoCol)
{
如果(UnicatIPInfo.Address.ToString()== hostIP.ToString())
{
返回UnicatIPInfo.IPv4Mask;
}
}
}
返回NULL;
}
私有ip地址getHostIP()
{
的foreach(ip地址在IP(Dns.GetHostEntry(Dns.GetHostName()))。AddressList),其中
{
如果(ip.AddressFamily == AddressFamily.InterNetwork)
返回的IP;
}
返回NULL;
}
如果您获得本地IP和子网,它应该是计算没有问题。
像这样的事情可能?
使用系统;
使用System.Net.NetworkInformation;
公共类测试
{
公共静态无效的主要()
{
的NetworkInterface [] =接口NetworkInterface.GetAllNetworkInterfaces();
的foreach(NetworkInterface的接口在接口)
{
如果(Interface.NetworkInterfaceType == NetworkInterfaceType.Loopback)继续;
如果(Interface.OperationalStatus = OperationalStatus.Up!)继续;
Console.WriteLine(Interface.Description);
UnicastIPAddressInformationCollection UnicastIPInfoCol = Interface.GetIPProperties()UnicastAddresses。
的foreach(UnicastIPAddressInformation UnicatIPInfo在UnicastIPInfoCol)
{
Console.WriteLine(\tIP地址是{0},UnicatIPInfo.Address);
Console.WriteLine(\tSubnet面膜是{0},UnicatIPInfo.IPv4Mask);
}
}
}
}
< A HREF =http://stackoverflow.com/questions/1470792/how-to-calculate-the-ip-range-when-the-ip-address-and-the-netmask-is-given>如何计算IP范围给出的IP地址和子网掩码是什么时候?应该给你它的其余部分。
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;
}
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.
这篇关于你如何得到默认的网络适配器的主机的广播地址? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!