Onlineatoz.net | IP Address Overview in asp.net

IP Address Overview in asp.net

IP Address

IP Address-It stands for "Internet Protocol Address".It serves as an identifier for a computer or device on a TCP/IP network.The tow major role of the IP Address is that the identifier of a particular network on the Internet and an identifier of the particular device which can be a server within that network. Networks under the TCP/IP protocol that based on the IP address of the destination.

An IP address can be represented as a 32-bit numeric address written as 4 set of numbers separated by periods. Each number can be 0 to 255. For example,1.192.168.240 could be an IP address. By using IP Address,we can obtain the Geographical information such as country, region, city, ZIP code, time zone etc. it helps the network provider to avoid unusual information written from clients.

Get User's IP Address

All User of your Website has an IP address during entering in you Website.as we mentioned above, By using this IP Address,we can asserting such information as a security logging, Country,state,city, region and so on . The difficulty is when they are behind a proxy of some sort, and you can only see the IP address of the proxy server. So,the following codes helps you to determine an IP Address using ASP.NET

String strIP = Request.ServerVariables("HTTP_X_FORWARDED_FOR");
If (nowip = "")strIP = Request.ServerVariables("REMOTE_ADDR");
 
Get User's Country details along their IP Address

IP-country database contains an IP number based on the beginning IP number and the ending IP number.It helps to match our country details with a record of this Database. For example, the IP address "202.186.13.4" is equivalent to the IP number "3401190660". This IP Number can be available the range from begining ip number to ending ip number in the Database.It expressed as,

"3401056256","3401400319","MY","MALAYSIA"
 
Calculate IP Address to IP Number

IP address contains four set of numbers seperated by periods. Each number powered by 256. The IP number used in the database because it is efficient to search between a range of numbers in the database. The beginning IP number and the ending IP number can be calculated by following formula:

IP Address = 202.186.13.4   here, A = 202, B = 186, C = 13 and D= 4
IP Number = 16777216*202 + 65536*186 + 256*13 + 4;   here, 65536=256*256 
= 3388997632 + 12189696 + 3328 + 4
= 3401190660
 
Country Database Format

here, it consists of the following:

  • Beginning IP range
  • Ending IP range
  • Two-character country code
  • Three-character country code
  • Country name based

 

 
Download Database
 
Table Screenshots

country_database.jpg

 
Sample Code snippets
       Partial Class Sample_IP_Address_Country _
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object,_
ByVal e As System.EventArgs) Handles Me.Load
Dim strIP As String
strIP = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If strIP = "" Then
strIP = Request.ServerVariables("REMOTE_ADDR")
End If
If txtIPAddress.Text = "" Then
txtIPAddress.Text = strIP
End If
lblError.Text = ""
End Sub
Protected Sub Button1_Click(ByVal sender As Object,_
ByVal e As System.EventArgs) Handles Button1.Click
On Error GoTo HandleError
Dim strGetIP As String
Dim dblGetLongIP As Double
Dim dblPrevPosition As Double
Dim dblPosition As Double
Dim dblNumber As Double
strGetIP = txtIPAddress.Text
For i = 1 To 4
dblPosition = InStr(dblPrevPosition + 1, strGetIP, ".", 1)
If i = 4 Then
dblPosition = Len(strGetIP) + 1
End If
dblNumber = Int(Mid(strGetIP, dblPrevPosition + 1, dblPosition - dblPrevPosition - 1))
dblPrevPosition = dblPosition
dblGetLongIP = ((dblNumber Mod 256) * (256 ^ (4 - i))) + dblGetLongIP
Next
txtIPNumber.Text = dblGetLongIP
HandleError:
lblError.Text = Err.Description
End Sub
End Class
 
output
 
country_output.jpg


Posted by: Admin
Posted on: 9/11/2011 at 8:03 PM
Categories: Asp.net
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed