本文將通過一個(gè)實(shí)例來向大家講解如何使用C#來編寫一個(gè)具備查詢IP段功能的小程序。
主要功能:查詢一個(gè)IP所有的IP段.
關(guān)鍵:從Byte數(shù)組到ulong的轉(zhuǎn)換出來的數(shù)字和 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;
/**////
/// IP段名稱
///
public string Name
{
get { return _Name; }
set { _Name = value; }
}
/**////
/// ?始IP
///
public ulong BeginIP
{
get { return _BeginIP; }
set { _BeginIP = value; }
}
/**////
/// ?束IP
///
public ulong EndIP
{
get { return _EndIP; }
set { _EndIP = value; }
}
/**////
/// 此IP段的范?
///
public ulong Range
{
get
{
return EndIP - BeginIP;
主要功能:查詢一個(gè)IP所有的IP段.
關(guān)鍵:從Byte數(shù)組到ulong的轉(zhuǎn)換出來的數(shù)字和 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;
/**////
/// IP段名稱
///
public string Name
{
get { return _Name; }
set { _Name = value; }
}
/**////
/// ?始IP
///
public ulong BeginIP
{
get { return _BeginIP; }
set { _BeginIP = value; }
}
/**////
/// ?束IP
///
public ulong EndIP
{
get { return _EndIP; }
set { _EndIP = value; }
}
/**////
/// 此IP段的范?
///
public ulong Range
{
get
{
return EndIP - BeginIP;