查询IP所在区段
来源:互联网 发布:mac下载软件怎么退出 编辑:程序博客网 时间:2024/05/16 15:55
查询IP所在区段
编程语言:C#
类 别:(网络应用,实用算法)
主要功能:查询一个IP所有的IP段.
关键:从Byte数组到ulong的转换出来的数字和 IPAddress.Address 返回值的是不一样的.
类 别:(网络应用,实用算法)
主要功能:查询一个IP所有的IP段.
关键:从Byte数组到ulong的转换出来的数字和 IPAddress.Address 返回值的是不一样的.
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
namespace IPUtility
{
class Program
{
static void Main(string[] args)
{
IPRangeManage irm = new IPRangeManage();
irm.Add(new IPRange("石家庄", "219.148.24.0", "219.148.63.255"));
irm.Add(new IPRange("石家庄", "222.222.0.0", "222.222.63.255"));
irm.Add(new IPRange("唐山", "219.148.64.0", "219.148.79.255"));
irm.Add(new IPRange("保定", "219.148.20.0", "219.148.23.255"));
Console.WriteLine(irm.Search("219.148.56.3").Name);
Console.ReadLine();
}
}
public class IPRange
{
private string _Name = string.Empty;
private ulong _BeginIP = 0;
private ulong _EndIP = Int32.MaxValue;
/**//// <summary>
/// IP段名称
/// </summary>
public string Name
{
get { return _Name; }
set { _Name = value; }
}
/**//// <summary>
/// 开始IP
/// </summary>
public ulong BeginIP
{
get { return _BeginIP; }
set { _BeginIP = value; }
}
/**//// <summary>
/// 结束IP
/// </summary>
public ulong EndIP
{
get { return _EndIP; }
set { _EndIP = value; }
}
/**//// <summary>
/// 此IP段的范围
/// </summary>
public ulong Range
{
get
{
return EndIP - BeginIP;
}
}
public IPRange(string name, string ipBegin, string ipEnd)
{
this.Name = name;
this.BeginIP = IP2A(ipBegin);
this.EndIP = IP2A(ipEnd);
}
public static ulong IP2A(string ip)
{
byte[] bytes = IPAddress.Parse(ip).GetAddressBytes();
ulong ret = 0;
foreach (byte b in bytes)
{
ret <<= 8;
ret |= b;
}
return ret;
}
public static int Compare(IPRange x, IPRange y)
{
if(x.Range == y.Range)
return 0;
else if(x.Range > y.Range)
return 1;
else return -1;
}
}
public class IPRangeManage
{
public IPRangeManage()
{ }
private List< IPRange> _IPRangeList = new List< IPRange>();
private bool _NeedSort = true;
public void Add(IPRange ipRange)
{
_IPRangeList.Add(ipRange);
_NeedSort = true;
}
private void Sort()
{
if (_NeedSort)
{
_IPRangeList.Sort(new Comparison<IPRange>(IPRange.Compare));
}
}
public IPRange Search(string ipString)
{
ulong ip = IPRange.IP2A(ipString);
this.Sort();
foreach (IPRange ir in _IPRangeList)
{
if (ir.BeginIP <= ip && ir.EndIP >= ip)
{
return ir;
}
}
return null;
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Net;
namespace IPUtility
{
class Program
{
static void Main(string[] args)
{
IPRangeManage irm = new IPRangeManage();
irm.Add(new IPRange("石家庄", "219.148.24.0", "219.148.63.255"));
irm.Add(new IPRange("石家庄", "222.222.0.0", "222.222.63.255"));
irm.Add(new IPRange("唐山", "219.148.64.0", "219.148.79.255"));
irm.Add(new IPRange("保定", "219.148.20.0", "219.148.23.255"));
Console.WriteLine(irm.Search("219.148.56.3").Name);
Console.ReadLine();
}
}
public class IPRange
{
private string _Name = string.Empty;
private ulong _BeginIP = 0;
private ulong _EndIP = Int32.MaxValue;
/**//// <summary>
/// IP段名称
/// </summary>
public string Name
{
get { return _Name; }
set { _Name = value; }
}
/**//// <summary>
/// 开始IP
/// </summary>
public ulong BeginIP
{
get { return _BeginIP; }
set { _BeginIP = value; }
}
/**//// <summary>
/// 结束IP
/// </summary>
public ulong EndIP
{
get { return _EndIP; }
set { _EndIP = value; }
}
/**//// <summary>
/// 此IP段的范围
/// </summary>
public ulong Range
{
get
{
return EndIP - BeginIP;
}
}
public IPRange(string name, string ipBegin, string ipEnd)
{
this.Name = name;
this.BeginIP = IP2A(ipBegin);
this.EndIP = IP2A(ipEnd);
}
public static ulong IP2A(string ip)
{
byte[] bytes = IPAddress.Parse(ip).GetAddressBytes();
ulong ret = 0;
foreach (byte b in bytes)
{
ret <<= 8;
ret |= b;
}
return ret;
}
public static int Compare(IPRange x, IPRange y)
{
if(x.Range == y.Range)
return 0;
else if(x.Range > y.Range)
return 1;
else return -1;
}
}
public class IPRangeManage
{
public IPRangeManage()
{ }
private List< IPRange> _IPRangeList = new List< IPRange>();
private bool _NeedSort = true;
public void Add(IPRange ipRange)
{
_IPRangeList.Add(ipRange);
_NeedSort = true;
}
private void Sort()
{
if (_NeedSort)
{
_IPRangeList.Sort(new Comparison<IPRange>(IPRange.Compare));
}
}
public IPRange Search(string ipString)
{
ulong ip = IPRange.IP2A(ipString);
this.Sort();
foreach (IPRange ir in _IPRangeList)
{
if (ir.BeginIP <= ip && ir.EndIP >= ip)
{
return ir;
}
}
return null;
}
}
}
- 查询IP所在区段
- 查询IP所在区段(转载)
- 查询IP所在区段(C#)
- 查询IP所在区段(C#)
- 查询IP区段
- 根据IP查询所在国家IP-2-Country
- IP地址排序、IP区段去重
- IP地址排序、IP区段去重
- 获取iP所在地址
- php根据ip查询所在地区(非常有用,赶集网就用到)
- Coreseek:区段查询及增量索引代替实时索引
- 根据IP取得所在地址
- 根据IP判断所在地区
- PHP获取IP所在地区
- php获取ip所在地址
- PHP获取IP所在地区
- 查询客户所在国家名称
- 网站所在服务器查询方法
- 处理死锁
- 一个小游戏
- [zz]【心存感恩】
- Linux操作系统的内核编译内幕详解
- Remoting异常处理:在客户端获取服务器内部错误
- 查询IP所在区段
- 三层C/S结构及其应用开发
- 为何使用N层架构?
- 感受开源底层技术的乐趣
- ASP.NET三层结构小例
- 一个基于三层架构的例子
- 初学者应该怎样学习复杂的编程语言?
- C++内存对象大会战
- 用JS实现的完美无限级联下拉菜单