45fan.com - 路饭网

搜索: 您的位置主页 > 电脑频道 > 电脑教程 > 阅读资讯:使用.NET读取QQWry.Dat纯真版ip数据库格式数据源的方法

使用.NET读取QQWry.Dat纯真版ip数据库格式数据源的方法

2016-08-30 12:37:20 来源:www.45fan.com 【

使用.NET读取QQWry.Dat纯真版ip数据库格式数据源的方法

.NET读取QQWry.Dat 纯真版ip数据库格式数据源

把网络上比较流行的纯真版ip数据库格式QQwry.Dat作为IP数据源来实现IP地址的查询显示。

1/******************************************************************
2**FileName:IPScaner.cs
3**CreateDate:2004-12-2720:10:28
4**Modifier:
5**ModifyDate:
6**Description:toscantheiplocationfromqqwry.dat
7**Version:IPScaner1.0.0
8******************************************************************/
9usingSystem;
10usingSystem.IO;
11usingSystem.Collections;
12usingSystem.Text;
13usingSystem.Text.RegularExpressions;
14namespacechat.Rules
15{
16/**////<summary>
17///toscantheiplocationfromqqwry.dat
18///</summary>
19publicclassIPScaner
20{
21#region私有成员
22privatestringdataPath;
23privatestringip;
24privatestringcountry;
25privatestringlocal;
26
27privatelongfirstStartIp=0;
28privatelonglastStartIp=0;
29privateFileStreamobjfs=null;
30privatelongstartIp=0;
31privatelongendIp=0;
32privateintcountryFlag=0;
33privatelongendIpOff=0;
34privatestringerrMsg=null;
35#endregion
36
37#region构造函数
38publicIPScaner()
39{
40//
41//TODO:在此处添加构造函数逻辑
42//
43}
44#endregion
45
46#region公共属性
47publicstringDataPath
48{
49set{dataPath=value;}
50}
51publicstringIP
52{
53set{ip=value;}
54}
55publicstringCountry
56{
57get{returncountry;}
58}
59publicstringLocal
60{
61get{returnlocal;}
62}
63publicstringErrMsg
64{
65get{returnerrMsg;}
66}
67#endregion
68
69#region搜索匹配数据
70privateintQQwry()
71{
72stringpattern=@"(((/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]))";
73RegexobjRe=newRegex(pattern);
74MatchobjMa=objRe.Match(ip);
75if(!objMa.Success)
76{
77this.errMsg="IP格式错误";
78return4;
79}
80
81longip_Int=this.IpToInt(ip);
82intnRet=0;
83if(ip_Int>=IpToInt("127.0.0.0")&&ip_Int<=IpToInt("127.255.255.255"))
84{
85this.country="本机内部环回地址";
86this.local="";
87nRet=1;
88}
89elseif((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")))
90{
91this.country="网络保留地址";
92this.local="";
93nRet=1;
94}
95objfs=newFileStream(this.dataPath,FileMode.Open,FileAccess.Read);
96try
97{
98//objfs.Seek(0,SeekOrigin.Begin);
99objfs.Position=0;
100byte[]buff=newByte[8];
101objfs.Read(buff,0,8);
102firstStartIp=buff[0]+buff[1]*256+buff[2]*256*256+buff[3]*256*256*256;
103lastStartIp=buff[4]*1+buff[5]*256+buff[6]*256*256+buff[7]*256*256*256;
104longrecordCount=Convert.ToInt64((lastStartIp-firstStartIp)/7.0);
105if(recordCount<=1)
106{
107country="FileDataError";
108objfs.Close();
109return2;
110}
111longrangE=recordCount;
112longrangB=0;
113longrecNO=0;
114while(rangB<rangE-1)
115{
116recNO=(rangE+rangB)/2;
117this.GetStartIp(recNO);
118if(ip_Int==this.startIp)
119{
120rangB=recNO;
121break;
122}
123if(ip_Int>this.startIp)
124rangB=recNO;
125else
126rangE=recNO;
127}
128this.GetStartIp(rangB);
129this.GetEndIp();
130if(this.startIp<=ip_Int&&this.endIp>=ip_Int)
131{
132this.GetCountry();
133this.local=this.local.Replace("(我们一定要解放台湾!!!)","");
134}
135else
136{
137nRet=3;
138this.country="未知";
139this.local="";
140}
141objfs.Close();
142returnnRet;
143}
144catch
145{
146return1;
147}
148
149}
150#endregion
151
152#regionIP地址转换成Int数据
153privatelongIpToInt(stringip)
154{
155char[]dot=newchar[]{'.'};
156string[]ipArr=ip.Split(dot);
157if(ipArr.Length==3)
158ip=ip+".0";
159ipArr=ip.Split(dot);
160
161longip_Int=0;
162longp1=long.Parse(ipArr[0])*256*256*256;
163longp2=long.Parse(ipArr[1])*256*256;
164longp3=long.Parse(ipArr[2])*256;
165longp4=long.Parse(ipArr[3]);
166ip_Int=p1+p2+p3+p4;
167returnip_Int;
168}
169#endregion
170
171#regionint转换成IP
172privatestringIntToIP(longip_Int)
173{
174longseg1=(ip_Int&0xff000000)>>24;
175if(seg1<0)
176seg1+=0x100;
177longseg2=(ip_Int&0x00ff0000)>>16;
178if(seg2<0)
179seg2+=0x100;
180longseg3=(ip_Int&0x0000ff00)>>8;
181if(seg3<0)
182seg3+=0x100;
183longseg4=(ip_Int&0x000000ff);
184if(seg4<0)
185seg4+=0x100;
186stringip=seg1.ToString()+"."+seg2.ToString()+"."+seg3.ToString()+"."+seg4.ToString();
187
188returnip;
189}
190#endregion
191
192#region

本文地址:http://www.45fan.com/dnjc/69823.html
Tags: .net 读取 QQWry.Dat
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部