ASP read Pure IP library code

Source: Internet
Author: User
Tags pow reference split tostring

Database using the pure database qqwry.dat,6 trillion or so.

Database download Address: North Netcom |  East China Telecom | Automatic selection of lines

Always wanted to do with a watermark IP signature map. Have a busy weekend. ASP this aspect of a lot of information, do not say more. Just post the algorithm. NET on this aspect of the article is very few, I search anyway. NET now first say the algorithm, after a few days to make watermark map of the tutorial hair.

ASP version, find two algorithms, one is found, the other is a database with a description (the author is very kind).

Both of these algorithms are labeled:

First algorithm:

Reference:

function CIP (SIP)
TIP=CSTR (SIP)
Sip1=left (Tip,cint (InStr (Tip, ".") -1))
Tip=mid (Tip,cint (InStr (Tip, ".") +1))
Sip2=left (Tip,cint (InStr (Tip, ".") -1))
Tip=mid (Tip,cint (InStr (Tip, ".") +1))
Sip3=left (Tip,cint (InStr (Tip, ".") -1))
Sip4=mid (Tip,cint (InStr (Tip, ".") +1))
If CInt (SIP1) <128 Then
Cip=cint (SIP1) *256*256*256+cint (SIP2) *256*256+cint (SIP3) *256+cint (SIP4)
Else
Cip=cint (SIP1) *256*256*256+cint (SIP2) *256*256+cint (SIP3) *256+cint (SIP4)-4294967296
End If
End Function

This algorithm is a bit messy. I didn't see what it meant. Especially

Reference:

If CInt (SIP1) <128 Then
Cip=cint (SIP1) *256*256*256+cint (SIP2) *256*256+cint (SIP3) *256+cint (SIP4)
Else
Cip=cint (SIP1) *256*256*256+cint (SIP2) *256*256+cint (SIP3) *256+cint (SIP4)-4294967296
End If

After testing, no judgment is required. That
Cip=cint (SIP1) *256*256*256+cint (SIP2) *256*256+cint (SIP3) *256+cint (SIP4)
You can get the correct value.

The second algorithm, this relatively simple and concise, the algorithm at a glance, the fool will choose this, to annotate a bit:

Reference:

Function Cacuip (SIP)
On Error Resume Next
Dim Srip, AIp
Srip=0
AIp = Split (SIP, ".") ' Intercept string, get array aip[0] to aip[3]
If UBound (AIP) <>3 Then ' If not the correct IP
Cacuip=0 ' return 0
Exit function ' exits functions
End If
For I=0 to 3
Srip=srip+ (CInt (AIP (i)) * (256^ (3-i))) ' Multiply by 256 of the 3-i power, then add the value
Next
Cacuip=srip-1 ' finally minus 1, you get a bunch of numbers that can't be read in the database of innocence.
If ERR Then cacuip=0
End Function

Simple and clear, I like it ~


OK, replace it with the following. NET algorithm.

Reference:

void ciker_ip (String IP)
{
System.Int64 ip0 = 0; 64-bit
string[] Ip_array = IP. Split (new char[] {'. '}, Stringsplitoptions.removeemptyentries); With ASP
for (int i = 0; I <= 3;i++)
{
Ip0 = Ip0 + convert.toint64 (ip_array[i)) * Convert.toint64 (Math.pow (256, 3-i)); 64-bit
}
ip0=ip0-1;
}

OK, here it is. NET algorithm is over.. NET all code is served.

Web.config:

Reference:

<appSettings>
<add key= "Access" value= "Ciker_ip.mdb"/>
<connectionStrings/>

Ip. ASPX.CS:

Add the following namespaces:

Reference:

Using System.Data.OleDb;
Using System.Text;

protected void Page_Load (object sender, EventArgs e)
{
Ciker_ip (request.servervariables["REMOTE_ADDR"). ToString ());
}

void ciker_ip (String IP)
{
System.Int64 ip0 = 0;
string[] Ip_array = IP. Split (new char[] {'. '}, Stringsplitoptions.removeemptyentries);
for (int i = 0; I <= 3;i++)
{
Ip0 = Ip0 + convert.toint64 (ip_array[i)) * Convert.toint64 (Math.pow (256, 3-i));
}
ip0=ip0-1;

String strconnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath ( configurationsettings.appsettings["Access"]);
OleDbConnection conn = new OleDbConnection (strconnection);
OleDbCommand cmd = conn. CreateCommand ();
Cmd.commandtext = "Select Pos,detail from IP where startip<=" + Ip0 + "and endip>=" + ip0;
Conn. Open ();
OleDbDataReader dr = cmd. ExecuteReader ();
while (Dr. Read ())
{
Response.Write ("Your Ya IP:" + IP + "<br/>");
Response.Write ("IP code:" + ip0 + "<br/>");
Response.Write ("area:" + dr[0]. ToString () + "" + dr[1]. ToString ());
}
Dr. Close ();
Conn. Close ();
}


Related Article

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.