using
System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ipQuery
... {
//辅助类,用于保存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;
}
}
}
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ipQuery
... {
//辅助类,用于保存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;
}
}
}
这是CZ_INDEX_INFO.cs文件,为辅助类,保存的是IP索引信息。
下面为PHCZIP.cs,为读取纯真IP数据库类。
using
System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ipQuery;
using System.Windows.Forms;
/**/ /// <summary>
/// PHCZIP 的摘要说明
/// </summary>
///
public class PHCZIP
... {
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 PHCZIP()
...{
bFilePathInitialized = false;
SetDbFilePath(Application.StartupPath + "/QQWry.Dat");
}
//使用二分法查找索引区,初始化查找区间
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
...{
//这种模式说明接下来就是保存的国家和地区信息了,以'
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ipQuery;
using System.Windows.Forms;
/**/ /// <summary>
/// PHCZIP 的摘要说明
/// </summary>
///
public class PHCZIP
... {
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 PHCZIP()
...{
bFilePathInitialized = false;
SetDbFilePath(Application.StartupPath + "/QQWry.Dat");
}
//使用二分法查找索引区,初始化查找区间
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
...{
//这种模式说明接下来就是保存的国家和地区信息了,以'
下面为实现手机号码归属地查询的数据链接类DB.cs
using
System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
namespace ipQuery
... {
class DB
...{
public DB()
...{
}
//字符串连接
public static OleDbConnection Create_Conn(string server_path)
...{
string str_conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + server_path + ";";
OleDbConnection conn = new OleDbConnection(str_conn);
return conn;
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
namespace ipQuery
... {
class DB
...{
public DB()
...{
}
//字符串连接
public static OleDbConnection Create_Conn(string server_path)
...{
string str_conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + server_path + ";";
OleDbConnection conn = new OleDbConnection(str_conn);
return conn;
}
}
}
调用代码如下:
using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
namespace ipQuery
... {
public partial class Fm_ipmobile : Form
...{
public Fm_ipmobile()
...{
InitializeComponent();
}
private void btn_ipSearch_Click(object sender, EventArgs e)
...{
PHCZIP checkip = new PHCZIP();
string tt = checkip.GetAddressWithIP(tb_IP.Text);
lbl_ipResult.Text = tt;
}
private void btn_Search_Click(object sender, EventArgs e)
...{
string tempPath = Application.StartupPath + "/MobileDB.dat";
if (File.Exists(tempPath))
...{
if (txt_Search.Text.Length < 7)
...{
MessageBox.Show("输入的长度不够!至少前7位");
}
else
...{
string cmd_str = "SELECT * FROM list WHERE num = '" + txt_Search.Text.Substring(0, 7) + "'";
OleDbConnection conn = DB.Create_Conn(tempPath);
OleDbDataAdapter da = new OleDbDataAdapter(cmd_str, conn);
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds, "temp");
conn.Close();
int count = ds.Tables["temp"].Rows.Count;
if (count < 1)
...{
MessageBox.Show("没找到相应的地区");
lbl_num.Text = txt_Search.Text;
lbl_add.Text = "没找到!!";
lbl_type.Text = "----";
}
else
...{
lbl_num.Text = txt_Search.Text;
lbl_add.Text = ds.Tables["temp"].Rows[0]["area"].ToString();
lbl_type.Text = ds.Tables["temp"].Rows[0]["t"].ToString();
}
}
}
else
...{
MessageBox.Show("数据库文件不存在!请确认目录下MobileDB.dat文件存在");
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
namespace ipQuery
... {
public partial class Fm_ipmobile : Form
...{
public Fm_ipmobile()
...{
InitializeComponent();
}
private void btn_ipSearch_Click(object sender, EventArgs e)
...{
PHCZIP checkip = new PHCZIP();
string tt = checkip.GetAddressWithIP(tb_IP.Text);
lbl_ipResult.Text = tt;
}
private void btn_Search_Click(object sender, EventArgs e)
...{
string tempPath = Application.StartupPath + "/MobileDB.dat";
if (File.Exists(tempPath))
...{
if (txt_Search.Text.Length < 7)
...{
MessageBox.Show("输入的长度不够!至少前7位");
}
else
...{
string cmd_str = "SELECT * FROM list WHERE num = '" + txt_Search.Text.Substring(0, 7) + "'";
OleDbConnection conn = DB.Create_Conn(tempPath);
OleDbDataAdapter da = new OleDbDataAdapter(cmd_str, conn);
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds, "temp");
conn.Close();
int count = ds.Tables["temp"].Rows.Count;
if (count < 1)
...{
MessageBox.Show("没找到相应的地区");
lbl_num.Text = txt_Search.Text;
lbl_add.Text = "没找到!!";
lbl_type.Text = "----";
}
else
...{
lbl_num.Text = txt_Search.Text;
lbl_add.Text = ds.Tables["temp"].Rows[0]["area"].ToString();
lbl_type.Text = ds.Tables["temp"].Rows[0]["t"].ToString();
}
}
}
else
...{
MessageBox.Show("数据库文件不存在!请确认目录下MobileDB.dat文件存在");
}
}
}
}
界面如下:
代码打包下载地址:http://download1.csdn.net/down3/20070520/20152516260.rar