查询IP所在区段

编程语言:C#
类    别:(网络应用,实用算法)
主要功能:查询一个IP所有的IP段.
关键:从Byte数组到ulong的转换出来的数字和 IPAddress.Address 返回值的是不一样的.

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.Net;
None.gif
None.gif
namespace  IPUtility
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IPRangeManage irm 
= new IPRangeManage();
InBlock.gif            irm.Add(
new IPRange("石家庄""219.148.24.0""219.148.63.255"));
InBlock.gif            irm.Add(
new IPRange("石家庄""222.222.0.0""222.222.63.255"));
InBlock.gif            irm.Add(
new IPRange("唐山""219.148.64.0""219.148.79.255"));
InBlock.gif            irm.Add(
new IPRange("保定""219.148.20.0""219.148.23.255"));
InBlock.gif
InBlock.gif            Console.WriteLine(irm.Search(
"219.148.56.3").Name);
InBlock.gif
InBlock.gif            Console.ReadLine();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class IPRange 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private string _Name = string.Empty;
InBlock.gif
InBlock.gif        
private ulong _BeginIP = 0;
InBlock.gif
InBlock.gif        
private ulong _EndIP = Int32.MaxValue;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// IP段名称
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _Name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _Name = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 开始IP
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public ulong BeginIP
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _BeginIP; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _BeginIP = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 结束IP
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public ulong EndIP
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _EndIP; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _EndIP = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 此IP段的范围
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public ulong Range
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return EndIP - BeginIP;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public IPRange(string name, string ipBegin, string ipEnd)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.Name = name;
InBlock.gif            
this.BeginIP = IP2A(ipBegin);
InBlock.gif            
this.EndIP = IP2A(ipEnd);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public static ulong IP2A(string ip)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
byte[] bytes = IPAddress.Parse(ip).GetAddressBytes();
InBlock.gif            
ulong ret = 0;
InBlock.gif
InBlock.gif            
foreach (byte b in bytes)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ret 
<<= 8;
InBlock.gif                ret 
|= b;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return ret;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public static int Compare(IPRange x, IPRange y)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(x.Range == y.Range)
InBlock.gif                
return 0;
InBlock.gif            
else if(x.Range > y.Range)
InBlock.gif                
return 1;
InBlock.gif            
else return -1;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class IPRangeManage
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public IPRangeManage()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{ }
InBlock.gif
InBlock.gif        
private List< IPRange> _IPRangeList = new List< IPRange>();
InBlock.gif        
private bool _NeedSort = true;
InBlock.gif
InBlock.gif        
public void Add(IPRange ipRange)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _IPRangeList.Add(ipRange);
InBlock.gif            _NeedSort 
= true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Sort()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (_NeedSort)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _IPRangeList.Sort(
new Comparison<IPRange>(IPRange.Compare));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public IPRange Search(string ipString)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ulong ip = IPRange.IP2A(ipString);
InBlock.gif
InBlock.gif            
this.Sort();
InBlock.gif
InBlock.gif            
foreach (IPRange ir in _IPRangeList)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (ir.BeginIP <= ip && ir.EndIP >= ip)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return ir;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return null;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值