C:\> Rostislav Persion's Projects

.:: Geolocation ::.
Finding city, state and country from IP






This is a program that outputs your location based on your IP address. The C# program uses two CSV files to achieve this. The first file contains a range of IP addresses with a location code on the same line. Once you find your IP range, you will find the location code (IP addresses are in long integer format). The second file contains location codes as the primary key, along with city, state, country, zip, metro code and area code.

EXECUTABLE

CSV DATABASE #1 (IP-START, IP-END, LOCATION-CODE)

CSV DATABASE #2 (LOCATION-CODE, COUNTRY, STATE, CITY, ZIP, LAT, LON, METRO-CODE, AREA-CODE)


SOFTWARE WRITTEN IN C#
   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using System.Net;
   6:  using System.IO;
   7:   
   8:  namespace Geo
   9:  {
  10:      class Program
  11:      {
  12:   
  13:   
  14:          static long IPToInt(string addr)
  15:          {
  16:              return (long) (uint) IPAddress.NetworkToHostOrder((int) IPAddress.Parse(addr).Address);
  17:          }
  18:   
  19:          static void Main(string[] args)
  20:          {
  21:   
  22:              // get and convert input
  23:              Console.Write("IP ADDRESS: ");
  24:              string ip_string = Console.ReadLine();
  25:              long ip_long = IPToInt(ip_string);
  26:   
  27:   
  28:              // Read the block file and display it line by line.
  29:              Console.WriteLine("SEARCHING IP...");
  30:              long location_code = 0;
  31:              string line = "";
  32:              StreamReader file = new StreamReader(@"GeoLiteCity-Blocks.csv");
  33:              while ((line = file.ReadLine()) != null)
  34:              {
  35:                  string[] parts = line.Split(',');
  36:                  long ip1 = Convert.ToInt64(parts[0].Trim('"'));
  37:                  long ip2 = Convert.ToInt64(parts[1].Trim('"'));
  38:                  long location = Convert.ToInt64(parts[2].Trim('"'));
  39:                  if ((ip_long >= ip1) && (ip_long <= ip2))
  40:                  {
  41:                      location_code = location;
  42:                      break;
  43:                  }
  44:              }
  45:   
  46:              file.Close();
  47:   
  48:   
  49:   
  50:   
  51:   
  52:   
  53:   
  54:   
  55:              // Read the location file and display it line by line.
  56:              Console.WriteLine("SEARCHING LOCATION...");
  57:              string line2 = "";
  58:              StreamReader file2 = new StreamReader(@"GeoLiteCity-Location.csv");
  59:              while ((line2 = file2.ReadLine()) != null)
  60:              {
  61:                  string[] parts2 = line2.Split(',');
  62:   
  63:                  long location2 = Convert.ToInt64(parts2[0].Trim('"'));
  64:                  string country = parts2[1].Trim('"');
  65:                  string state = parts2[2].Trim('"');
  66:                  string city = parts2[3].Trim('"');
  67:                  string zip = parts2[4].Trim('"');
  68:   
  69:                  if (location2 == location_code)
  70:                  {
  71:                      Console.WriteLine();
  72:                      Console.WriteLine("CITY: " + city);
  73:                      Console.WriteLine("STATE: " + state);
  74:                      Console.WriteLine("COUNTRY: " + country);
  75:                      Console.WriteLine("ZIP: " + zip);
  76:                      Console.WriteLine();
  77:                  }
  78:   
  79:              }
  80:   
  81:              file.Close();
  82:   
  83:   
  84:              Console.WriteLine("PRESS ANY KEY...");
  85:              Console.ReadLine();
  86:          }
  87:      }
  88:  }