计算一个IP是否在一个IP段内
static void Main(string[] args)
{
#region 验证一个IP是否在指定的IP段内
//验证一个IP是否在指定的IP段内
//long ip = getIpNum("192.168.2.22");
//Dictionary<long, long> dicips = new Dictionary<long, long>();
//dicips.Add(getIpNum("60.162.200.0"), getIpNum("60.162.201.255"));
//dicips.Add(getIpNum("192.168.1.0"), getIpNum("192.169.1.255"));
//int aa = 1;
//foreach (var item in dicips)
//{
// if (isInner(ip, item.Key, item.Value))
// {
// aa = 0;
// }
//}
//Console.Write("结果:" + aa);
#endregion
}
private static bool isInner(long clientIp, long begin, long end)
{
return (clientIp >= begin) && (clientIp <= end);
}
private static long getIpNum(String ipAddress)
{
String[] ip = ipAddress.Split(new char[] { '.' });
long a = int.Parse(ip[0]);
long b = int.Parse(ip[1]);
long c = int.Parse(ip[2]);
long d = int.Parse(ip[3]);
return a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;
}