Get geo-location based on IP address

Source: Internet
Author: User

    ///<summary>    ///provide the method of searching IP information from the pure IP database;///</summary>     Public classIpselect {FileStream ipfile; LongIP;  Public stringIpfilepath; ///<summary>        ///constructor Function///</summary>        ///<param name= "Ipfilepath" >Pure IP Database path</param>         PublicIpselect () {Ipfilepath="Pure IP Database path";//Textutility.getfullpath (utility.getipdbfilepath);        }        ///<summary>        ///location, including country and region///</summary>         Public structiplocation { Public stringcountry, area; }        ///<summary>        ///gets the location entity where the specified IP resides///</summary>        ///<param name= "StrIP" >the IP address to query</param>        ///<returns></returns>         Public stringGetiplocation (stringStrIP) {            if(!file.exists (Ipfilepath)) {                return ""; } IP= Iptolong (StrIP);//Converts the IP in the form of a string to a bit longIplocation loc =Newiplocation (); Try{ipfile=NewFileStream (Ipfilepath, FileMode.Open, FileAccess.Read);//file Stream                Long[] Iparray = Blocktoarray (Readipblock ());//                Longoffset = searchip (Iparray,0, Iparray.length-1) *7+4; Ipfile.position+ = offset;//Skip Start IPIpfile.position = Readlongx (3) +4;//Skip End IP                intFlag = Ipfile.readbyte ();//Read Flags                if(Flag = =1)//indicates that the country and region have been diverted{ipfile.position= Readlongx (3); Flag= Ipfile.readbyte ();//re-read flag                }                LongCountryoffset =ipfile.position; Loc.country=ReadString (flag); if(Flag = =2) {ipfile.position= Countryoffset +3; } Flag=Ipfile.readbyte (); Loc.area=ReadString (flag);                Ipfile.close (); Ipfile=NULL; }            Catch            {                return ""; }            returnLoc.country +Loc.area; }        ///<summary>        ///Converts the IP in the form of a string to a bit long///</summary>        ///<param name= "StrIP" ></param>        ///<returns></returns>         Public LongIptolong (stringStrIP) {            byte[] Ip_bytes =New byte[8]; string[] Strarr = Strip.split (New Char[] {'.' } );  for(inti =0; I <4; i++) {Ip_bytes[i]=byte. Parse (strarr[3-i]); }            returnBitconverter.toint64 (Ip_bytes,0 ); }        ///<summary>        ///converts the starting IP in an indexed block of bytes into a long array///</summary>        ///<param name= "IPblock" ></param>        Long[] Blocktoarray (byte[] ipblock) {            Long[] Iparray =New Long[Ipblock.length/7]; intIpindex =0; byte[] temp =New byte[8];  for(inti =0; i < ipblock.length; i + =7) {array.copy (ipblock, I, temp,0,4 ); Iparray[ipindex]= Bitconverter.toint64 (temp,0 ); Ipindex++; }            returnIparray; }        ///<summary>        ///searches the IP array for the specified IP and returns its index///</summary>        ///<param name= "Iparray" >IP Array</param>        ///<param name= "Start" >Specify the starting location for the search</param>        ///<param name= "End" >specify where to end the search</param>        ///<returns></returns>        intSearchip (Long[] Iparray,intStartintend) {            intMiddle = (start + end)/2; if(middle = =start)returnMiddle; Else if(IP <Iparray[middle])returnsearchip (Iparray, start, middle); Else                returnsearchip (Iparray, middle, end); }        ///<summary>        ///reading an indexed chunk in an IP file///</summary>        ///<returns></returns>        byte[] Readipblock () {LongStartPosition = Readlongx (4 ); LongEndposition = Readlongx (4 ); LongCount = (endposition-startposition)/7+1;//Total Record CountIpfile.position =startposition; byte[] IPblock =New byte[Count *7]; Ipfile.read (IPblock,0, ipblock.length); Ipfile.position=startposition; returnIPblock; }        ///<summary>        ///reads the specified byte from an IP file and converts a bit long///</summary>        ///<param name= "Bytescount" >number of bytes to convert, not more than 8 bytes in mind</param>        ///<returns></returns>        LongREADLONGX (intBytescount) {            byte[] _bytes =New byte[8]; Ipfile.read (_bytes,0, Bytescount); returnBitconverter.toint64 (_bytes,0 ); }        ///<summary>        ///reading a string from an IP file///</summary>        ///<param name= "Flag" >Steering Signs</param>        ///<returns></returns>        stringReadString (intflag) {            if(Flag = =1|| Flag = =2)//Steering SignsIpfile.position = Readlongx (3 ); Elseipfile.position-=1; List<byte> list =Newlist<byte>(); byteB = (byte) Ipfile.readbyte ();  while(B >0) {list.                ADD (b); b= (byte) Ipfile.readbyte (); }            returnEncoding.Default.GetString (list.        ToArray ()); }    }
        /// <summary>        ///depending on the location of the IP/// </summary>        /// <param name= "IP" >IP</param>        /// <returns>location</returns>        protected stringGetaddresswithip (stringIP) {Ipselect Ipselect=NewIpselect (); if(string. IsNullOrEmpty (IP)) {IP="127.0.0.1"; }            returnipselect.getiplocation (IP); }

Pure IP Database format document in Baidu Cloud Network Disk "Programming Tools Files" folder

Get geo-location based on IP address

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.