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

2016-02-19 13:46 29 1 收藏

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

【 tulaoshi.com - 编程语言 】

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

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

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

(本文来源于图老师网站,更多请访问https://www.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;
  }
  }
  }

  所有代码就这么多,是不是很简单啊?相信大家都能够看懂。

来源:https://www.tulaoshi.com/n/20160219/1605194.html

延伸阅读
1.打开VS.NET 2003. 2.新建一个WinForm Application.3.添加命名空间 using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Net;using System.Net.Sockets;using System.Text;using System.IO; 4.主要代码 为了防止...
.NET将关于多线程的功能定义在System.Threading名字空间中。因此,要使用多线程,必须先声明引用此名字空间(using System.Threading;)。 即使你没有编写多线程应用程序的经验,也可能听说过“启动线程”“杀死线程”这些词,其实除了这两个外,涉及多线程方面的还有诸如“暂停线程”“优先级”“挂起线程”“恢复线程”等等。下面将一个...
在我的上一篇文章《C#中使用XML读取XML》中和大家讨论了如何使用.NET Framework中提供的类在C#中读取XML以及读取的一些相关概念,那么今天就说一说如何在C#中编写XML文档,起初我觉得用编程的方式去编写XML简直就是自讨苦吃,后来想想还是觉得挺有用的,我想Microsoft那班家伙能编出这些类来应该不是仅仅为了向比尔i盖茨交差吧!至于它的用...
1.首先引入System.Runtime.InteropServices   using System.Runtime.InteropServices; 2.在类内部声明两个API函数,它们的位置和类的成员变量等同.   [System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函数 public static extern bool RegisterHotKey( IntPtr hWnd, // handle to wind...
    最近在网上不断的看到有人问如何用BCB的TServerSocket和TClientSocket进行编程的问题,所以决定把我的一些编程经验告诉给大家,让大家能够尽快的把握他们的用法。 首先要讲一下他们的一些设置(属性): TServerSocket 的几个属性 Active          &...

经验教程

788

收藏

50
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部