使用C#编写查询IP段功能的程序

丨虚无吞炎丶

丨虚无吞炎丶

2016-02-19 13:46

生活已是百般艰难,为何不努力一点。下面图老师就给大家分享使用C#编写查询IP段功能的程序,希望可以让热爱学习的朋友们体会到设计的小小的乐趣。

  本文将通过一个实例来向大家讲解如何使用C#来编写一个具备查询IP段功能的小程序。

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

  主要功能:查询一个IP所有的IP段.

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

  关键:从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;
  /**//// 
  /// 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;
  }
  }
  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.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;
  }
  }
  }

  所有代码就这么多,是不是很简单啊?相信大家都能够看懂。
展开更多 50%)
分享

猜你喜欢

使用C#编写查询IP段功能的程序

编程语言 网络编程
使用C#编写查询IP段功能的程序

用Visual C#编写屏幕保护程序

编程语言 网络编程
用Visual C#编写屏幕保护程序

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

Visual C#中用WMI编写网络程序

编程语言 网络编程
Visual C#中用WMI编写网络程序

用C#编写获取远程IP,MAC的方法

电脑网络
用C#编写获取远程IP,MAC的方法

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

利用Delphi 2005 编写C#应用程序

编程语言 网络编程
利用Delphi 2005 编写C#应用程序

Visual C# 2005中编写Socket网络程序

编程语言 网络编程
Visual C# 2005中编写Socket网络程序

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

用 C# 编程实现读写Binary

用 C# 编程实现读写Binary

完美替换html代码

完美替换html代码
下拉加载更多内容 ↓