[英]Comparing Ip value for determine whether a specific range
是否有任何方法可以将ip地址与特定范围内的地址进行比较。
IPAddress[] ips;
ips = Dns.GetHostAddresses("www.xyz.com");
Console.WriteLine("GetHostAddresses({0}) returns:", "www.xyz.com");
foreach (IPAddress ip in ips)
{
Console.WriteLine(" {0}", ip);
}
Console.ReadLine();
ips变量存储ip值。 我想比较10.100.12.21和10.255.15.30。 如何比较其他类型的ips? 在比较该IP范围之后,转换为ips值以加倍。 还是其他想法?
使用等于:
static void Main(string[] args)
{
IPAddress a, b;
a = new IPAddress(new byte[] { 10, 100, 12, 21 });
b = new IPAddress(new byte[] { 10, 100, 12, 21 });
Console.WriteLine("Value is {0}", a.Equals(b));
Console.ReadLine();
}
自己的实现,请尝试以下操作:
int[] maxIP = new int[] { 10, 255, 15, 30 };
int[] minIP = new int[] { 10, 100, 12, 21 };
char[] sep = new char[] { '.' };
var ip = "10.100.16.21";
string[] splitted = ip.Split(sep);
for (int i = 0; i < splitted.Length; i++)
{
if (int.Parse(splitted[i]) > maxIP[i])
{
Console.WriteLine("IP greather than max");
break;
}
else if (int.Parse(splitted[i]) < minIP[i])
{
Console.WriteLine("IP less than min");
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.