根据IP获取详细地址,可以用来做用户识别.进入地区站

本文介绍了一个使用C#实现的纯真IP数据库解析类库,该类库通过二分法快速定位并解析出IP地址对应的国家、省份及城市等详细信息。
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace QianBianWanHua_Lib
{
    
///   <summary>   
    
///   根据IP获取详细国家,省分,城市,地区的详细信息 
    
///   段小勇
    
///   </summary>   
                
    
//辅助类,用于保存IP索引信息   
    public class CZ_INDEX_INFO
    {
        
public UInt32 IpSet;
        
public UInt32 IpEnd;
        
public UInt32 Offset;

        
public CZ_INDEX_INFO()
        {
            IpSet 
= 0;
            IpEnd 
= 0;
            Offset 
= 0;
        }
    }

    
//读取纯真IP数据库类   
    public class AddressWithIP
    {
        
protected bool bFilePathInitialized;
        
protected string FilePath;
        
protected FileStream FileStrm;
        
protected UInt32 Index_Set;
        
protected UInt32 Index_End;
        
protected UInt32 Index_Count;
        
protected UInt32 Search_Index_Set;
        
protected UInt32 Search_Index_End;
        
protected CZ_INDEX_INFO Search_Set;
        
protected CZ_INDEX_INFO Search_Mid;
        
protected CZ_INDEX_INFO Search_End;

        
public AddressWithIP()
        {
            bFilePathInitialized 
= false;
        }

        
public AddressWithIP(string dbFilePath)
        {
            bFilePathInitialized 
= false;
            SetDbFilePath(dbFilePath);
        }

        
//使用二分法查找索引区,初始化查找区间   
        public void Initialize()
        {
            Search_Index_Set 
= 0;
            Search_Index_End 
= Index_Count - 1;
        }

        
//关闭文件   
        public void Dispose()
        {
            
if (bFilePathInitialized)
            {
                bFilePathInitialized 
= false;
                FileStrm.Close();
                
//FileStrm.Dispose();   
            }

        }


        
public bool SetDbFilePath(string dbFilePath)
        {
            
if (dbFilePath == "")
            {
                
return false;
            }

            
try
            {
                FileStrm 
= new FileStream(dbFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            }
            
catch
            {
                
return false;
            }
            
//检查文件长度   
            if (FileStrm.Length < 8)
            {
                FileStrm.Close();
                
//FileStrm.Dispose();   
                return false;
            }
            
//得到第一条索引的绝对偏移和最后一条索引的绝对偏移   
            FileStrm.Seek(0, SeekOrigin.Begin);
            Index_Set 
= GetUInt32();
            Index_End 
= GetUInt32();

            
//得到总索引条数   
            Index_Count = (Index_End - Index_Set) / 7 + 1;
            bFilePathInitialized 
= true;

            
return true;

        }

        
public string GetAddressWithIP(string IPValue)
        {
            
if (!bFilePathInitialized)
                
return "";

            Initialize();

            UInt32 ip 
= IPToUInt32(IPValue);

            
while (true)
            {

                
//首先初始化本轮查找的区间   

                
//区间头   
                Search_Set = IndexInfoAtPos(Search_Index_Set);
                
//区间尾   
                Search_End = IndexInfoAtPos(Search_Index_End);

                
//判断IP是否在区间头内   
                if (ip >= Search_Set.IpSet && ip <= Search_Set.IpEnd)
                    
return ReadAddressInfoAtOffset(Search_Set.Offset);


                
//判断IP是否在区间尾内   
                if (ip >= Search_End.IpSet && ip <= Search_End.IpEnd)
                    
return ReadAddressInfoAtOffset(Search_End.Offset);

                
//计算出区间中点   
                Search_Mid = IndexInfoAtPos((Search_Index_End + Search_Index_Set) / 2);

                
//判断IP是否在中点   
                if (ip >= Search_Mid.IpSet && ip <= Search_Mid.IpEnd)
                    
return ReadAddressInfoAtOffset(Search_Mid.Offset);

                
//本轮没有找到,准备下一轮   
                if (ip < Search_Mid.IpSet)
                    
//IP比区间中点要小,将区间尾设为现在的中点,将区间缩小1倍。   
                    Search_Index_End = (Search_Index_End + Search_Index_Set) / 2;
                
else
                    
//IP比区间中点要大,将区间头设为现在的中点,将区间缩小1倍。   
                    Search_Index_Set = (Search_Index_End + Search_Index_Set) / 2;
            }
            
return "";

        }

        
private string ReadAddressInfoAtOffset(UInt32 Offset)
        {
            
string country = "";
            
string area = "";
            UInt32 country_Offset 
= 0;
            
byte Tag = 0;
            
//跳过4字节,因这4个字节是该索引的IP区间上限。   
            FileStrm.Seek(Offset + 4, SeekOrigin.Begin);

            
//读取一个字节,得到描述国家信息的“寻址方式”   
            Tag = GetTag();

            
if (Tag == 0x01)
            {

                
//模式0x01,表示接下来的3个字节是表示偏移位置   
                FileStrm.Seek(GetOffset(), SeekOrigin.Begin);

                
//继续检查“寻址方式”   
                Tag = GetTag();
                
if (Tag == 0x02)
                {
                    
//模式0x02,表示接下来的3个字节代表国家信息的偏移位置   
                    
//先将这个偏移位置保存起来,因为我们要读取它后面的地区信息。   
                    country_Offset = GetOffset();
                    
//读取地区信息(注:按照Luma的说明,好像没有这么多种可能性,但在测试过程中好像有些情况没有考虑到,   
                    
//所以写了个ReadArea()来读取。   
                    area = ReadArea();
                    
//读取国家信息   
                    FileStrm.Seek(country_Offset, SeekOrigin.Begin);
                    country 
= ReadString();
                }
                
else
                {
                    
//这种模式说明接下来就是保存的国家和地区信息了,以'

 

调用方法:

 

    protected void Page_Load(object sender, EventArgs e)
    {
        QianBianWanHua_Lib.AddressWithIP _addresswithip 
= new QianBianWanHua_Lib.AddressWithIP();
        
bool _bSucess = false;
        _bSucess 
= _addresswithip.SetDbFilePath(Server.MapPath(ConfigurationManager.AppSettings["IPDataBaseUrl"]));
        
if(_bSucess)
        {
            
string _strName = _addresswithip.GetAddressWithIP("211.147.251.220");
            Response.Write(_strName);
        }
        
else
        {
            Response.Write(
"FileError");
        }
    }

 

使用的是"纯真IP数据库类 "每五天更新一次

本模块代码是针对在 2011 年在 优快云 论坛个发布的“最新 NET 读取纯真IP数据库代码(C#)”源码,了一次升级,这次升级不是简单的修补,是本人精心的重写,现在只需要 5 分哦,您值得拥有!该源代码不同于网上的代码,网上代码基本可分为两大类,第一类直接使用文件流,通过移动文件流指针(即更改 Stream.Position 属性值)搜索 IP 地址对应的信息,此类代码问题是:其一移动文件指针效率是比较低的(给 Position 赋值),多线程并发时,会重复打开多个文件效率更加底下;第二类是把文件直接加载内存中,通过这种缓冲,速度是提升了,但并没有为多线程环境优化,多线程并发时(如:Web 中每位访客,都是一根线程),意味会重复的读取文件,重复的创建缓存,浪费内存空间。 该源代码特点是考虑到了多线程应用环境(如:Web 每个会话,都是一根线程),设计了缓存对象 QQWryCache 用于管理缓存,用 QQCacheStream 流读取缓存数据。在多线程应用环境中,假设 10 根线程访问同一个纯真 IP 数据库时,只会开辟 1 份缓存,给多根线程共享,避免了不必要的内存浪费。 注1:本模块代码,保证所有静态方法都是线程安全的,但不保证所有实例方法都是线程安全的。 注2:每根线程访问缓存时,请通过 QQWryCache.GetCache 静态方法获取缓存对象。 注3:多根线程获取到的缓存对象,通常都是同一对象,该对象已经考虑了线程同步,不必担心线程安全问题。 /* >>> 使用完全缓存(缓存整个文件,约 8.8MB),调用方法如下: */ QQWryCache cache = QQWryCache.GetCache("qqwry.dat", true); Stream stream = cache.GetCacheStream(); QQWrySearcher searcher = new QQwryScanner(stream); QQWryLocation location = searcher.Query("IP 地址"); Console.WritleLine("Country = {0}, Location = {1}", location.Country, location.Location); /* 完全缓冲, * 缓存一旦初始化完毕,就会自动关闭文件, * 所以不再依赖于文件,因此可以不用关闭缓冲流, * 下面调用 Close 方法,其实没有实际意义,但也不会引发异常。 */ stream.Close(); /* >>> 使用索引缓存(仅缓存索引部分,约 3MB),调用方法如下: <<>> 直接使用文件流(不使用缓存),调用方法如下: <<>> 遍历 IP 数据库。 <<< */ QQWryCache cache = QQWryCache.GetCache("qqwry.dat", true); Stream stream = cache.GetCacheStream(); QQWrySearcher searcher = new QQWrySearcher(stream); // 用 for 循环遍历 for(int i = 0; i < searcher.Count; i++) { QQWryIpLocation item = searcher[i]; Console.WritleLine("Country = {0}, Location = {1}", location.Country, location.Location); } // 用 foreach 循环遍历 foreach(QQWryIpLocation item in searcher) { QQWryIpLocation item = searcher[i]; Console.WritleLine("Country = {0}, Location = {1}", location.Country, location.Location); }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值