首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Dotnet频道
中国IT教育
Google
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> C# >> 正文

利用C#编写查询IP所在区段

编程语言:C#
类    别:(网络应用,实用算法)
主要功能:查询一个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;
        }
    }
}

【责编:Peng】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·WEB程序开发--ASP.NET和PHP、JSP究竟学哪个?
 ·五步带你入门XML
 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 今日更新
 社区讨论
 博客论点
 频道精选
 Dotnet频道相关导航