ASp.Net day

Micro blog



About Satalaj

www.satalaj.com

The best inline translator

Live lookup to see what asp.net developers are searching




Your own Ip to location

kick it on DotNetKicks.com


     In this article I'm trying to explore how one can develop their own IP to Location like stuff. This article will explore simple TCPIP scoket programming.

There are 5 registerer who keeps records of IP addressess Domain Name. I have developed open source whois project which is capable of query almost all

domain extensions like .in , .uk, .com, .mobi etc. You can download it from here
http://satalajwhois.codeplex.com/

    You can telnet those registrar on port 43 using command promt like this http://www.revenmerchantservices.com/post/2009/07/27/whois-telnet-port-43.aspx

Below is simple code that will query asia pacific network IP whois.apnic.net  . Code is simle and self explainetory

using System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Net;
using
System.Net.Sockets;
using
System.Net.Security;
using System.IO; public partial class _Default : System.Web.UI.
Page
{
  
protected void Page_Load(object sender, EventArgs
e)
  {
  }
  protected void Button1_Click1(object sender, EventArgs
e)
 {

  
TcpClient tcpclient = new TcpClient
();     // Create TCPClient object

   tcpclient.Connect("whois.apnic.net",43);
  // open port 43 with asia pasific registrar

   NetworkStream stream = tcpclient.GetStream();  // Get refrence to opened Network stream

   StreamReader reader = new StreamReader(stream);  // Lets assign reader to stream   

   StreamWriter writer = new StreamWriter(stream);   // lets assign writer to stream
   
   writer.WriteLine("59.163.16.173");      // lets write IP address to stream. That we will query asia pacific registrar

//  Instead of passing IP address above if you pass domain name like
http://www.google.in/
//  you will get google info in asia pacific network

   writer.Flush();

   string strResponse = string.Empty;

   while(!reader.EndOfStream)
   {

    strResponse += reader.ReadLine() + Environment.NewLine;

   }

    stream.Close();

    Response.Write(strResponse);    // you can parse this response to get location , country owner address etc.

 }

}
 

Once you get the response from those registerar you need to Parce it to get your appropriate fields from it like Location City country owner etc.
if that IP dosen't belongs to that registarar you need to query another registarar like ARIN. you can visit
http://www.arin.net/

Note* Some shared host provider would not let you open the port 43.
         Above code will work fine with Local host and Dedicated hosting

Use below link to share your ideas and reviews about it
http://revenmerchantservices.com/post/2009/08/20/IP-to-location.aspx

Thanks, Satalaj