查詢IP所在區段(轉載)

using System;
  2using System.Collections.Generic;
  3using System.Text;
  4using System.Net;
  5
  6namespace IPUtility
  7{
  8    class Program
  9    {
 10        static void Main(string[] args)
 11        {
 12            IPRangeManage irm = new IPRangeManage();
 13            irm.Add(new IPRange("石家莊", "219.148.24.0", "219.148.63.255"));
 14            irm.Add(new IPRange("石家莊", "222.222.0.0", "222.222.63.255"));
 15            irm.Add(new IPRange("唐山", "219.148.64.0", "219.148.79.255"));
 16            irm.Add(new IPRange("保定", "219.148.20.0", "219.148.23.255"));
 17
 18            Console.WriteLine(irm.Search("219.148.56.3").Name);
 19
 20            Console.ReadLine();
 21
 22        }

 23
 24
 25    }

 26
 27    public class IPRange
 28    {
 29        private string _Name = string.Empty;
 30
 31        private ulong _BeginIP = 0;
 32
 33        private ulong _EndIP = Int32.MaxValue;
 34
 35        /**//// <summary>
 36        /// IP段名稱
 37        /// </summary>

 38        public string Name
 39        {
 40            get { return _Name; }
 41            set { _Name = value; }
 42        }

 43
 44        /**//// <summary>
 45        /// 開始IP
 46        /// </summary>

 47        public ulong BeginIP
 48        {
 49            get { return _BeginIP; }
 50            set { _BeginIP = value; }
 51        }

 52
 53        /**//// <summary>
 54        /// 結束IP
 55        /// </summary>

 56        public ulong EndIP
 57        {
 58            get { return _EndIP; }
 59            set { _EndIP = value; }
 60        }

 61
 62
 63        /**//// <summary>
 64        /// 此IP段的範圍
 65        /// </summary>

 66        public ulong Range
 67        {
 68            get
 69            {
 70                return EndIP - BeginIP;
 71            }

 72        }

 73
 74        public IPRange(string name, string ipBegin, string ipEnd)
 75        {
 76            this.Name = name;
 77            this.BeginIP = IP2A(ipBegin);
 78            this.EndIP = IP2A(ipEnd);
 79        }

 80
 81
 82        public static ulong IP2A(string ip)
 83        {
 84            byte[] bytes = IPAddress.Parse(ip).GetAddressBytes();
 85            ulong ret = 0;
 86
 87            foreach (byte b in bytes)
 88            {
 89                ret <<= 8;
 90                ret |= b;
 91            }

 92
 93            return ret;
 94
 95        }

 96
 97
 98        public static int Compare(IPRange x, IPRange y)
 99        {
100            if(x.Range == y.Range)
101                return 0;
102            else if(x.Range > y.Range)
103                return 1;
104            else return -1;
105        }

106
107    }

108
109    public class IPRangeManage
110    {
111        public IPRangeManage()
112        { }
113
114        private List< IPRange> _IPRangeList = new List< IPRange>();
115        private bool _NeedSort = true;
116
117        public void Add(IPRange ipRange)
118        {
119            _IPRangeList.Add(ipRange);
120            _NeedSort = true;
121        }

122
123        private void Sort()
124        {
125            if (_NeedSort)
126            {
127                _IPRangeList.Sort(new Comparison<IPRange>(IPRange.Compare));
128            }

129        }

130
131        public IPRange Search(string ipString)
132        {
133            ulong ip = IPRange.IP2A(ipString);
134
135            this.Sort();
136
137            foreach (IPRange ir in _IPRangeList)
138            {
139                if (ir.BeginIP <= ip && ir.EndIP >= ip)
140                {
141                    return ir;
142                }

143            }

144            return null;
145        }

146    }

147}

148
 
發佈了37 篇原創文章 · 獲贊 4 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章