智动软件 - 站长优化推广网站好帮手
帐号: 密码: 注册会员
模糊 标题

C#读取纯真IP数据库方法

时间:2008-07-29 Tag: 点击:统计中..



使用方法:
BDQQ.Data.QQWry qq=new BDQQ.Data.QQWry("d:\\QQWry.Dat");
BDQQ.Data.IPLocation ip=qq.SearchIPLocation("127.0.0.1");//这里添写IP地址
Console.WriteLine(ip.country);//国家
Console.WriteLine(ip.area);//地区
以下是基础类文件,和IP详细数据格式类 本文来自智动软件zdwork.cn

//根据LumaQQ改写而成.
using System;
using System.IO;
using System.Text;
namespace BDQQ.Data zdwork.cn
{

智动软件


/**//// <summary>

copyright 智动软件


/// QQWry 的摘要说明。
/// </summary>

public class QQWry
{ zdwork.cn
//第一种模式
第一种模式
private const byte REDIRECT_MODE_1 = 0x01; 智动软件
//第二种模式
第二种模式
private const byte REDIRECT_MODE_2 = 0x02; copyright 智动软件
//每条记录长度
每条记录长度
private const int IP_RECORD_LENGTH = 7;

内容来自zdwork.cn


//数据库文件
数据库文件
private FileStream ipFile;
private const string unCountry = "未知国家";
zdwork.cn

private const string unArea = "未知地区";
//索引开始位置
索引开始位置
private long ipBegin;
//索引结束位置

copyright 智动软件


索引结束位置
private long ipEnd;
//IP地址对象 本文来自智动软件zdwork.cn
IP地址对象
private IPLocation loc;
//存储文本内容

本文来自智动软件zdwork.cn


存储文本内容
private byte[] buf;
//存储3字节
内容来自zdwork.cn

存储3字节
private byte[] b3;
//存储4字节 copyright 智动软件
存储4字节
private byte[] b4;
//构造函数

智动软件


构造函数 www.zdwork.cn
public QQWry( string ipfile )
{ copyright 智动软件
buf
= new byte[100];
b3
= new byte[3];
b4
= new byte[4];
www.zdwork.cn

try
{

copyright 智动软件


ipFile
= new FileStream( ipfile,FileMode.Open );
}

catch( Exception ex )
{
本文来自智动软件zdwork.cn

throw new Exception( ex.Message );
}

ipBegin
= readLong4(0);
ipEnd
= readLong4(4);
loc
= new IPLocation(); 智动软件
}


//根据IP地址搜索
根据IP地址搜索 智动软件
public IPLocation SearchIPLocation( string ip )
{

智动软件


//将字符IP转换为字节
string[] ipSp = ip.Split('.');
if( ipSp.Length != 4 )
{ 本文来自智动软件zdwork.cn
throw new ArgumentOutOfRangeException( "不是合法的IP地址!" );
}

byte[] IP = new byte[4];
for( int i = 0; i < IP.Length ; i++ ) 智动软件
{ 内容来自zdwork.cn
IP[i]
= (byte)(Int32.Parse( ipSp[i] ) & 0xFF) ;
}

IPLocation local
= null;
long offset = locateIP( IP );
if( offset != -1 ) copyright 智动软件
{
copyright 智动软件

local
= getIPLocation( offset );
}

if( local == null )
{ copyright 智动软件
local
= new IPLocation();
local.area
= unArea;
local.country
= unCountry;
}

return local;
}

智动软件


//取得具体信息
取得具体信息
zdwork.cn

private IPLocation getIPLocation( long offset )
{ copyright 智动软件
ipFile.Position
= offset + 4;
//读取第一个字节判断是否是标志字节
byte one = (byte)ipFile.ReadByte();
if( one == REDIRECT_MODE_1 ) zdwork.cn
{ 内容来自zdwork.cn
//第一种模式
//读取国家偏移
long countryOffset = readLong3();
//转至偏移处
ipFile.Position = countryOffset; copyright 智动软件
//再次检查标志字节
byte b = (byte)ipFile.ReadByte();
if( b == REDIRECT_MODE_2 )
{ 智动软件
loc.country
= readString( readLong3() );
ipFile.Position
= countryOffset + 4;
}

else
loc.country
= readString( countryOffset );
//读取地区标志
copyright 智动软件

loc.area = readArea( ipFile.Position );
}

else if( one == REDIRECT_MODE_2 )
{ 智动软件
//第二种模式
loc.country = readString( readLong3() );
loc.area
= readArea( offset + 8 );
}

else
{ 智动软件
//普通模式
loc.country = readString( --ipFile.Position );
loc.area
= readString( ipFile.Position );
}

return loc;
}
zdwork.cn


//取得地区信息
取得地区信息

copyright 智动软件


private string readArea( long offset )
{
智动软件

ipFile.Position
= offset;
byte one = (byte)ipFile.ReadByte();
if( one == REDIRECT_MODE_1 || one == REDIRECT_MODE_2 )
{ zdwork.cn
long areaOffset = readLong3( offset + 1 );
if( areaOffset == 0 )
return unArea;
else

www.zdwork.cn


{ zdwork.cn
return readString( areaOffset );
}

}

else
{ 本文来自智动软件zdwork.cn
return readString( offset );
}

}


//读取字符串
读取字符串 智动软件
private string readString( long offset )
{ www.zdwork.cn
ipFile.Position
= offset;
int i = 0;
for(i = 0, buf[i]=(byte)ipFile.ReadByte();buf[i] != (byte)(0);buf[++i]=(byte)ipFile.ReadByte()); copyright 智动软件
if( i > 0 )
return Encoding.Default.GetString( buf,0,i );
else
return "";
}

本文来自智动软件zdwork.cn



//查找IP地址所在的绝对偏移量
查找IP地址所在的绝对偏移量 内容来自zdwork.cn
private long locateIP( byte[] ip )
{ www.zdwork.cn
long m = 0;
int r;
//比较第一个IP项
readIP( ipBegin, b4 );
r
= compareIP( ip,b4);
if( r == 0 ) 内容来自zdwork.cn
return ipBegin;
else if( r < 0 )
return -1;
//开始二分搜索
for( long i = ipBegin,j=ipEnd; i<j; )

zdwork.cn


{

智动软件


m
= this.getMiddleOffset( i,j );
readIP( m,b4 );
r
= compareIP( ip,b4 );
if( r > 0 )
i
= m;
else if( r < 0 )
copyright 智动软件

{
本文来自智动软件zdwork.cn

if( m == j )
{ 本文来自智动软件zdwork.cn
j
-= IP_RECORD_LENGTH;
m
= j;
}

else
{

内容来自zdwork.cn


j
= m;
}

}

else
return readLong3( m+4 );
}

m
= readLong3( m+4 ); 本文来自智动软件zdwork.cn
readIP( m,b4 );
r
= compareIP( ip,b4 );
if( r <= 0 )
return m;
else
return -1; zdwork.cn
}


//读出4字节的IP地址
读出4字节的IP地址 智动软件
private void readIP( long offset, byte[] ip )
{ 本文来自智动软件zdwork.cn
ipFile.Position
= offset;
ipFile.Read( ip,
0,ip.Length );
byte tmp = ip[0];
ip[
0] = ip[3];
ip[
3] = tmp; www.zdwork.cn
tmp
= ip[1];
ip[
1] = ip[2];
ip[
2] = tmp;
}


//比较IP地址是否相同 智动软件
比较IP地址是否相同
private int compareIP( byte[] ip, byte[] beginIP )
{ 内容来自zdwork.cn
for( int i = 0; i < 4; i++ )
{

本文来自智动软件zdwork.cn


int r = compareByte( ip[i],beginIP[i] );
if( r != 0 )
return r;
}

return 0;

本文来自智动软件zdwork.cn


}


//比较两个字节是否相等
比较两个字节是否相等
private int compareByte( byte bsrc, byte bdst )
{ 本文来自智动软件zdwork.cn
if( ( bsrc&0xFF ) > ( bdst&0xFF ) )
return 1;
else if( (bsrc ^ bdst) == 0 ) zdwork.cn
return 0;
else
return -1;
}


//根据当前位置读取4字节

www.zdwork.cn


根据当前位置读取4字节 copyright 智动软件
private long readLong4( long offset )
{ www.zdwork.cn
long ret = 0;
ipFile.Position
= offset;
ret
|= ( ipFile.ReadByte() & 0xFF );
ret
|= ( ( ipFile.ReadByte() << 8 ) & 0xFF00 ); www.zdwork.cn
ret
|= ( ( ipFile.ReadByte() << 16 ) & 0xFF0000 );
ret
|= ( ( ipFile.ReadByte() << 24 ) & 0xFF000000 );
return ret; www.zdwork.cn
}


//根据当前位置,读取3字节
根据当前位置,读取3字节 zdwork.cn
private long readLong3( long offset )
{

内容来自zdwork.cn


long ret = 0;
ipFile.Position
= offset;
ret
|= ( ipFile.ReadByte() & 0xFF );
ret
|= ( (ipFile.ReadByte() << 8 ) & 0xFF00 );
zdwork.cn

ret
|= ( (ipFile.ReadByte() << 16 ) & 0xFF0000 );
return ret;
}


//从当前位置读取3字节 www.zdwork.cn
从当前位置读取3字节
private long readLong3()

本文来自智动软件zdwork.cn


{ zdwork.cn
long ret = 0;
ret
|= ( ipFile.ReadByte() & 0xFF );
ret
|= ( (ipFile.ReadByte() << 8 ) & 0xFF00 ); 本文来自智动软件zdwork.cn
ret
|= ( (ipFile.ReadByte() << 16 ) & 0xFF0000 );
return ret;
}


//取得begin和end之间的偏移量

内容来自zdwork.cn


取得begin和end之间的偏移量
private long getMiddleOffset( long begin, long end )
{ zdwork.cn
long records = ( end - begin ) / IP_RECORD_LENGTH;
records
>>= 1;
if( records == 0 )
records
= 1; 本文来自智动软件zdwork.cn
return begin + records * IP_RECORD_LENGTH;
}

}
//class QQWry
public class IPLocation
{ 内容来自zdwork.cn
public String country;
public String area;
public IPLocation()
{

内容来自zdwork.cn


country
= area = "";
}

public IPLocation getCopy()
{
copyright 智动软件

IPLocation ret
= new IPLocation();
ret.country
= country;
ret.area
= area;
return ret;
}

}

}
copyright 智动软件


www.zdwork.cn
(责任编辑:智动软件)
顶一下
(0)
0%
踩一下
(0)
0%

推荐内容

热门内容

相关内容


关于我们 | 联系我们 | 代理合作 | 意见及建议

本站免费提供刷IP软件,另有邮件搜索软件等,需要定制软件或有建议可联系我们。联系方式:QQ:896186342 Email:zdwork@qq.com 滇ICP备08001410号.