国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > 綜合 > 正文

用QQWry.Dat作數(shù)據(jù)源實(shí)現(xiàn)IP地址檢索[C#]

2024-07-21 02:18:26
字體:
供稿:網(wǎng)友
前幾天有一個(gè)朋友說一直想有一個(gè)這樣程序,可以把網(wǎng)絡(luò)上比較流行的qqwry.dat作為ip數(shù)據(jù)源來實(shí)現(xiàn)ip地址的查詢顯示。可是網(wǎng)絡(luò)上一直沒有出現(xiàn).net的版本。我想既然如此,那我就抽空寫一寫好了,先到google上go了一下,找到相關(guān)介紹文章,了解了其格式,然后又參考了其它語(yǔ)言版本的程序,一天多一點(diǎn)時(shí)間,終于把這個(gè)東西寫好了。不敢獨(dú)享,帖出來 /**//******************************************************************
** file name:ipscaner.cs
** copyright (c) 2004-2005 pptech studio(pptech.net)
** creater:rexsp(msn:[email protected])
** create date:2004-12-27 20:10:28
** modifier:
** modify date:
** description:to scan the ip location from qqwry.dat
** version: ipscaner 1.0.0
******************************************************************/
using system;
using system.io;
using system.collections;
using system.text;
using system.text.regularexpressions;
namespace pptech.website.businessrules
{
/**//// <summary>
/// to scan the ip location from qqwry.dat
/// </summary>
public class ipscaner
{
私有成員#region 私有成員
private string datapath;
private string ip;
private string country;
private string local;

private long firststartip=0;
private long laststartip=0;
private filestream objfs = null;
private long startip=0;
private long endip=0;
private int countryflag=0;
private long endipoff=0;
private string errmsg=null;
#endregion

構(gòu)造函數(shù)#region 構(gòu)造函數(shù)
public ipscaner()
{
//
// todo: 在此處添加構(gòu)造函數(shù)邏輯
//
}
#endregion

公共屬性#region 公共屬性
public string datapath
{
set{datapath=value;}
}
public string ip
{
set{ip=value;}
}
public string country
{
get{return country;}
}
public string local
{
get{return local;}
}
public string errmsg
{
get{return errmsg;}
}
#endregion

搜索匹配數(shù)據(jù)#region 搜索匹配數(shù)據(jù)
private int qqwry()
{
string pattern = @"(((/d{1,2})|(1/d{2})|(2[0-4]/d)|(25[0-5]))/.){3}((/d{1,2})|(1/d{2})|(2[0-4]/d)|(25[0-5]))";
regex objre = new regex(pattern);
match objma = objre.match(ip);
if(!objma.success)
{
this.errmsg="ip格式錯(cuò)誤";
return 4;
}

long ip_int = this.iptoint(ip);
int nret=0;
if(ip_int>=iptoint("127.0.0.0")&&ip_int<=iptoint("127.255.255.255"))
{
this.country="本機(jī)內(nèi)部環(huán)回地址";
this.local="";
nret=1;
}
else if((ip_int>=iptoint("0.0.0.0")&&ip_int<=iptoint("2.255.255.255"))||(ip_int>=iptoint("64.0.0.0")&&ip_int<=iptoint("126.255.255.255"))||(ip_int>=iptoint("58.0.0.0")&&ip_int<=iptoint("60.255.255.255")))
{
this.country="網(wǎng)絡(luò)保留地址";
this.local="";
nret=1;
}
objfs = new filestream(this.datapath, filemode.open, fileaccess.read);
try
{
//objfs.seek(0,seekorigin.begin);
objfs.position=0;
byte[] buff = new byte[8] ;
objfs.read(buff,0,8);
firststartip=buff[0]+buff[1]*256+buff[2]*256*256+buff[3]*256*256*256;
laststartip=buff[4]*1+buff[5]*256+buff[6]*256*256+buff[7]*256*256*256;
long recordcount=convert.toint64((laststartip-firststartip)/7.0);
if(recordcount<=1)
{
country="filedataerror";
objfs.close();
return 2;
}
long range=recordcount;
long rangb=0;
long recno=0;
while(rangb<range-1)
{
recno=(range+rangb)/2;
this.getstartip(recno);
if(ip_int==this.startip)
{
rangb = recno;
break;
}
if(ip_int>this.startip)
rangb=recno;
else
range=recno;
}
this.getstartip(rangb);
this.getendip();
if(this.startip<=ip_int&&this.endip>=ip_int)
{
this.getcountry();
this.local=this.local.replace("(我們一定要解放臺(tái)灣!!!)","");
}
else
{
nret=3;
this.country="未知";
this.local="";
}
objfs.close();
return nret;
}
catch
{
return 1;
}

}
#endregion

ip地址轉(zhuǎn)換成int數(shù)據(jù)#region ip地址轉(zhuǎn)換成int數(shù)據(jù)
private long iptoint(string ip)
{
char[] dot = new char[]{'.'};
string [] iparr = ip.split(dot);
if(iparr.length==3)
ip=ip+".0";
iparr=ip.split(dot);

long ip_int=0;
long p1=long.parse(iparr[0])*256*256*256;
long p2=long.parse(iparr[1])*256*256;
long p3=long.parse(iparr[2])*256;
long p4=long.parse(iparr[3]);
ip_int=p1+p2+p3+p4;
return ip_int;
}
#endregion

int轉(zhuǎn)換成ip#region int轉(zhuǎn)換成ip
private string inttoip(long ip_int)
{
long seg1=(ip_int&0xff000000)>>24;
if(seg1<0)
seg1+=0x100;
long seg2=(ip_int&0x00ff0000)>>16;
if(seg2<0)
seg2+=0x100;
long seg3=(ip_int&0x0000ff00)>>8;
if(seg3<0)
seg3+=0x100;
long seg4=(ip_int&0x000000ff);
if(seg4<0)
seg4+=0x100;
string ip=seg1.tostring()+"."+seg2.tostring()+"."+seg3.tostring()+"."+seg4.tostring();

return ip;
}
#endregion

獲取起始ip范圍#region 獲取起始ip范圍
private long getstartip(long recno)
{
long offset = firststartip+recno*7;
//objfs.seek(offset,seekorigin.begin);
objfs.position=offset;
byte [] buff = new byte[7];
objfs.read(buff,0,7);

endipoff=convert.toint64(buff[4].tostring())+convert.toint64(buff[5].tostring())*256+convert.toint64(buff[6].tostring())*256*256;
startip=convert.toint64(buff[0].tostring())+convert.toint64(buff[1].tostring())*256+convert.toint64(buff[2].tostring())*256*256+convert.toint64(buff[3].tostring())*256*256*256;
return startip;
}
#endregion

獲取結(jié)束ip#region 獲取結(jié)束ip
private long getendip()
{
//objfs.seek(endipoff,seekorigin.begin);
objfs.position=endipoff;
byte [] buff = new byte[5];
objfs.read(buff,0,5);
this.endip=convert.toint64(buff[0].tostring())+convert.toint64(buff[1].tostring())*256+convert.toint64(buff[2].tostring())*256*256+convert.toint64(buff[3].tostring())*256*256*256;
this.countryflag=buff[4];
return this.endip;
}
#endregion

獲取國(guó)家/區(qū)域偏移量#region 獲取國(guó)家/區(qū)域偏移量
private string getcountry()
{
switch(this.countryflag)
{
case 1:
case 2:
this.country=getflagstr(this.endipoff+4);
this.local=( 1 == this.countryflag )?" ":this.getflagstr(this.endipoff+8);
break;
default:
this.country=this.getflagstr(this.endipoff+4);
this.local=this.getflagstr(objfs.position);
break;
}
return " ";
}
#endregion

獲取國(guó)家/區(qū)域字符串#region 獲取國(guó)家/區(qū)域字符串
private string getflagstr(long offset)
{
int flag=0;
byte [] buff = new byte[3];
while(1==1)
{
//objfs.seek(offset,seekorigin.begin);
objfs.position=offset;
flag = objfs.readbyte();
if(flag==1||flag==2)
{
objfs.read(buff,0,3);
if(flag==2)
{
this.countryflag=2;
this.endipoff=offset-4;
}
offset=convert.toint64(buff[0].tostring())+convert.toint64(buff[1].tostring())*256+convert.toint64(buff[2].tostring())*256*256;
}
else
{
break;
}
}
if(offset<12)
return " ";
objfs.position=offset;
return getstr();
}
#endregion

getstr#region getstr
private string getstr()
{
byte lowc=0;
byte upc=0;
string str="";
byte[] buff = new byte[2];
while(1==1)
{
lowc= (byte)objfs.readbyte();
if(lowc==0)
break;
if(lowc>127)
{
upc=(byte)objfs.readbyte();
buff[0]=lowc;
buff[1]=upc;
system.text.encoding enc = system.text.encoding.getencoding("gb2312");
str+=enc.getstring(buff);
}
else
{
str+=(char)lowc;
}
}
return str;
}
#endregion

獲取ip地址#region 獲取ip地址
public string iplocation()
{
this.qqwry();
return this.country+this.local;
}
public string iplocation(string datapath,string ip)
{
this.datapath=datapath;
this.ip=ip;
this.qqwry();
return this.country+this.local;
}
#endregion


}
}

調(diào)用方式:
測(cè)試地址搜索#region 測(cè)試地址搜索
ipscaner objscan = new ipscaner();
string ip="221.224.205.13";
[email protected]"e:/個(gè)人資料/imtools/qqwryupdate/qqwry.dat";
objscan.ip=ip;
string addre=objscan.iplocation();
string err=objscan.errmsg;

#endregion
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 鄂托克前旗| 河源市| 宝坻区| 凤翔县| 扶绥县| 富顺县| 隆安县| 象州县| 烟台市| 凭祥市| 永仁县| 苍南县| 清原| 城市| 武强县| 栾城县| 佛山市| 石家庄市| 北海市| 根河市| 平南县| 高清| 盐城市| 资溪县| 独山县| 普兰店市| 台山市| 房山区| 中西区| 大连市| 佳木斯市| 金塔县| 临邑县| 忻州市| 灵台县| 惠州市| 台中市| 高阳县| 洛宁县| 苍南县| 客服|