关于网友提出的“纯真ip数据库 今天写的读取纯真IP数据库的代码。”问题疑问,本网通过在网上对“纯真ip数据库 今天写的读取纯真IP数据库的代码。”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题:纯真ip数据库 今天写的读取纯真IP数据库的代码。
描述: 呵呵,第一次贴代码出来~~大家不要见笑
'读取纯真IP数据库
'参考文档:http://lumaqq.linuxsir.org/article/qqwry_format_detail.html
'作者: gounliey
'Email:4473117@qq.com
'第一次写东西出来大家分享,还有很多不足之处,请大家指点。
'辅助类,用于保存IP索引信息
Public Class CZ_INDEX_INFO
Public IpSet As UInt32
Public IpEnd As UInt32
Public Offset As UInt32
Sub New()
IpSet = 0
IpEnd = 0
Offset = 0
End Sub
End Class
'读取纯真IP数据库类
Public Class PHCZIP
Protected bFilePathInitialized As Boolean
Protected FilePath As String
Protected FileStrm As FileStream
Protected Index_Set As UInt32
Protected Index_End As UInt32
Protected Index_Count As UInt32
Protected Search_Index_Set As UInt32
Protected Search_Index_End As UInt32
Protected Search_Set As CZ_INDEX_INFO
Protected Search_Mid As CZ_INDEX_INFO
Protected Search_End As CZ_INDEX_INFO
Public Sub New()
bFilePathInitialized = False
End Sub
Public Sub New(ByVal dbFilePath As String)
bFilePathInitialized = False
SetDbFilePath(dbFilePath)
End Sub
'使用二分法查找索引区,初始化查找区间
Protected Sub Initialize()
Search_Index_Set = 0
Search_Index_End = Index_Count - 1
End Sub
'关闭文件
Public Sub Dispose()
If (bFilePathInitialized) Then
bFilePathInitialized = False
FileStrm.Close()
FileStrm.Dispose()
End If
End Sub
'设置纯真IP数据库的文件路径
Public Function SetDbFilePath(ByVal dbFilePath As String) As Boolean
If (dbFilePath = "") Then
Return False
End If
'Try
'打开数据文件
FileStrm = New FileStream(dbFilePath, FileMode.Open, FileAccess.Read)
'Catch
'Return False
'End Try
'检查文件长度
If (FileStrm.Length < 8) Then
FileStrm.Close()
FileStrm.Dispose()
Return False
End If
'得到第一条索引的绝对偏移和最后一条索引的绝对偏移
FileStrm.Seek(0, SeekOrigin.Begin)
Index_Set = GetUInt32()
Index_End = GetUInt32()
'得到总索引条数
Index_Count = (Index_End - Index_Set) / 7 + 1
bFilePathInitialized = True
Return True
End Function
'主接口函数,根据传入的IP返回该IP的地址信息
Public Function GetAddressWithIP(ByVal IPValue As String) As String
If Not bFilePathInitialized Then
Return ""
End If
Initialize()
'将IP转化为数字
Dim ip As UInt32 = IPToUInt32(IPValue)
While (True)
'首先初始化本轮查找的区间
'区间头
Search_Set = IndexInfoAtPos(Search_Index_Set)
'区间尾
Search_End = IndexInfoAtPos(Search_Index_End)
'判断IP是否在区间头内
If (ip >= Search_Set.IpSet And ip <= Search_Set.IpEnd) Then
Return ReadAddressInfoAtOffset(Search_Set.Offset)
End If
'判断IP是否在区间尾内
If (ip >= Search_End.IpSet And ip <= Search_End.IpEnd) Then
Return ReadAddressInfoAtOffset(Search_End.Offset)
End If
'计算出区间中点
Search_Mid = IndexInfoAtPos((Search_Index_End + Search_Index_Set) / 2)
'判断IP是否在中点
If (ip >= Search_Mid.IpSet And ip <= Search_Mid.IpEnd) Then
Return ReadAddressInfoAtOffset(Search_Mid.Offset)
End If
'本轮没有找到,准备下一轮
If (ip < Search_Mid.IpSet) Then
'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
End If
End While
Return ""
End Function
以上介绍了“纯真ip数据库 今天写的读取纯真IP数据库的代码。”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/3580050.html