"VB.net" queries the IP address through the Ipip.net database

Source: Internet
Author: User

The last time we introduced the method of querying IP address details using pure database.
However, the pure database is provided by the user feedback, a lot of data description is not accurate, so I went online to find some other IP database, finally found ipip.net this site provides IP database.

IPIP offers a free version of the database available for both paid and free two versions, which we can use directly.
Https://www.ipip.net/download.html (need to register an account first)
There is a PHP parsing class in the package, there is a 17monipdb.dat file is the database, we just need to use it.

Copy the 17monipdb.dat to the program's home directory, using the following code:

ImportsSystem.IOImportsSystem.Text Public ClassIpipSharedOffset as Integer    SharedIndex as UInteger() =New UInteger(255) {}    SharedDataBuffer as Byte()    SharedIndexBuffer as Byte()    SharedLastModifyTime as Long=0LSharedIpfile as String    SharedRwlock as NewThreading.readerwriterlockShared Sub New() Load ("17monipdb.dat")    End Sub    Shared SubLoad (ByValFileName as String) Ipfile=NewFileInfo (filename). FullName Load ()End Sub    Shared SubLoad () rwlock. AcquireWriterLock (-1)        DimFi as NewFileInfo (ipfile) lastmodifytime=fi. Lastwritetime.ticksTryDataBuffer=File.readallbytes (FI. FullName)DimIndexlength = Bytestolong (DataBuffer (0), DataBuffer (1), DataBuffer (2), DataBuffer (3)) IndexBuffer=New Byte(Indexlength-1) {} array.copy (DataBuffer,4, IndexBuffer,0, indexlength) offset=CType(Indexlength,Integer)                         forLp as Integer=0  to 255Index (LP)=Bytestolong (_ IndexBuffer (LP*4+3), _ IndexBuffer (LP*4+2), _ IndexBuffer (LP*4+1), _ IndexBuffer (LP*4) _                 )            Next        CatchEx asExceptionThrowexEnd TryRwlock. Releasewriterlock ()End Sub    Private Shared FunctionBytestolong (ByValA as Byte,ByValB as Byte,ByValC as Byte,ByValD as Byte) as UInteger        Return(CTypeAUInteger) << -)Or(CType(b,UInteger) << -)Or(CTypeCUInteger) <<8)OrDEnd Function    Shared FunctionFind (ByValIp as String) as String() Rwlock. AcquireReaderLock (-1)        DimIPS = IP. Split (".")        DimIp_prefix_value =Integer. Parse (IPs (0))        DimIp2long_value as Long= Bytestolong (Byte. Parse (IPs (0)),Byte. Parse (IPs (1)),Byte. Parse (IPs (2)),Byte. Parse (IPs (3)))        DimStart =index (ip_prefix_value)DimMax_comp_len = offset-1028        DimIndex_offset as Long= -1LDimIndex_length as Integer= -1        DimB as Byte=0Start= Start *8+1024x768         whileStart <Max_comp_lenIfBytestolong (IndexBuffer (start +0), IndexBuffer (start +1), IndexBuffer (start +2), IndexBuffer (start +3)) >= Ip2long_value ThenIndex_offset= Bytestolong (b, indexbuffer (Start +6), IndexBuffer (start +5), IndexBuffer (start +4)) Index_length= &hff andIndexBuffer (Start +7)                Exit  while            End IfStart+=8        End  while        DimAreabytes =New Byte(Index_length-1) {} array.copy (DataBuffer, offset+ Index_offset-1024x768, Areabytes,0, Index_length)DimRet as String() =Encoding.UTF8.GetString (areabytes). Split (VbTab) rwlock. Releasereaderlock ()ReturnretEnd Function    End Class

This code I translated from the official C # version, and removed some redundant code, leaving only the core functionality.
Without the complete test, there is a bug please feedback to me.

The way to use it is simple:

Dim ret = Ipip. Find ("127.0.0.1")'  breaks all information with a newline , DimString. Join (vbCrLf, ret)

"VB.net" queries the IP address through the Ipip.net database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.