前几天有一个朋友说一直想有一个这样程序,可以把网络上比较流行的QQwry.Dat作为IP数据源来实现IP地址的查询显示。可是网络上一直没有出现.Net的版本。我想既然如此,那我就抽空写一写好了,先到Google上Go了一下,找到相关介绍文章,了解了其格式,然后又参考了其它语言版本的程序,一天多一点时间,终于把这个东西写好了。不敢独享,帖出来
// /* *****************************************************************
** 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
{
// ///
1<summary>
2 /// to scan the ip location from qqwry.dat
3 /// </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
构造函数 #region 构造函数
public IPScaner()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#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
搜索匹配数据 #region 搜索匹配数据
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格式错误 " ;
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 = " 本机内部环回地址 " ;
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 = " 网络保留地址 " ;
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 )
<IMG id=Codehighlighter1_2790_3008_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_2790_3008_Closed_Text.style.display='none'; Codehighl