Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / C#
Article

KBSoft IP Locator

Rate me:
Please Sign up or sign in to vote.
4.05/5 (8 votes)
1 Mar 2008CPOL13 min read 45.2K   2.2K   31   4
This article covers aspects of getting information by IP Address.
Image 1

Introduction

It is often necessary to get information about a user or a site by their IP address. For example, such a need appears at the determination of a site's target audience. Having received the data about site users, it is possible to promote the site more effectively and develop different marketing actions. There are quite a lot of commercial services providing such information. Choice of such systems is often determined by the volume of information received, by its accuracy and importance and also by the simplicity of adjusting and getting information. The purpose of this article is to show techniques of getting information about a user or a site by the IP address, and demonstrate an example of developing a demo application enabling us to get this information quickly and simply. You can find the demo application allowing to receive information by IP address and the source code of this application in the zip file attached to the article. The demo application uses a simplified version of the class, delivered in the form of a DLL library. This class provides basic functions of information receipt by IP address. You can find the source code of this class here. You can use this code with research aims in your desktop or Web-applications for getting information about site users or with an aim to get information about some site (it is enough to know this site's IP address).

This article is based on the experience of KBSoft, and information received from the documentation of IP addresses analyzing systems studied in this article.

Review of Services for Getting Information by IP Address

A lot of companies provide services on getting information by IP address. Some of them, providing free demo services are described in this article. One of them is CDYNE. This company presents Web-service under the name IP2Geo, which returns information in the form of XML by IP address. This Web-service is convenient, and no restrictions were noticed for a test getting of information. But there are disadvantages as well: the service does not provide a large quantity of information and the accuracy of location definition is not very high.

The company, IP2Location, provides similar services. The company's site provides more information than CDYNE service, but the demo page restricts quantity of requests to not more than 20 per day and presents results in an inconvenient way for processing a form.

The company FraudLabs also uses the IP2Location database. Advantages and disadvantages coincide with those of the previous company with an exception of the fact that the demo page returns the result in XML form, which significantly simplifies the processing of this data. Moreover, with the help of demo pages it is possible to get much more information than that from the previous company.

The most convenient services, from the author's point of view, are those provided by the company GeoBytes. Their advantage is a big volume of useful information, there is a possibility to get a reply from the server in XML form, and also more acceptable restrictions of the trial-version (not more than 20 requests per hour). Besides, for getting information in different formats, there is a possibility to use different templates, which can present information in a desirable view. Unfortunately, disadvantages also exist here: requests are sent via HTTP GET, but not via Web-service (it is under development now), which influences the security of the sent confidential data not in the best form (in case you bought a paid access). Moreover, in case of the use of paid access for getting information, it is complicated due to the need to use a session token.

Why a Probability to Get Wrong Information Exists?

The most useful information that can be received by IP address is the user's location. It is only necessary to get information about the latitude and the longitude for that and the rest of the information needed to make different reports (for example, monetary units, time zone etc.) can be received knowing these characteristics. But it's not that simple to do as it seems at first site. The thing is, that IP address is not assigned with some certain geographical location (for example IP addresses of subnets are absolutely useless for determination of a geographical location), that is why it is impossible to get information about the location of the IP address possessor with an absolute accuracy. Furthermore, there are proxy-servers and NAT, which have an opaque structure and that is why it is impossible to receive the location of an IP address using proxy-server or NAT. What can be maximally received is the location of the proxy-server or NAT itself. Moreover, there are proxy-servers, which generate random IP addresses or use header HTTP_X_FORWARDED_FOR. IP address from mobile phones or satellite systems will distinguish themselves with a most inaccurate location. Nevertheless, in the majority of cases, location of IP addresses possessors can be precisely determined.

Program Code Description

The designed class KBS_IPLocatorClass provides opportunities on the request of information from two sites: CDYNE and Geobytes. There is a possibility to request information about your own IP address and about any other IP address entered in the request field. Besides, the class presents a possibility to parse the received XML data. Along with XML-template Geobytes presents other templates providing the achievement of other aims as well (see here). The most useful template is valuepairsAll.txt, because this template presents most complete information, but, unfortunately, problems can occur while parsing data received. Nevertheless, having developed a function on parsing and having added it into the class source code, you can easily add a possibility of processing any template and get information you are interested in with the help of the LookupCustomIP()class. Besides, the class's flexible architecture enables to add a request and process information from any site presenting such information.

Here is the LookupCustomIP() method code:

C#
public bool LookupCustomIP(string ipAddress, ServerTypes serverType)
{
    try
    {
        //create the request URL
        string requestURL="";
        //add the host element of the URL
        switch (serverType)
        {
            case ServerTypes.Geobytes:
               requestURL = "http://www.geobytes.com/IpLocator.htm?GetLocation&template="
                   + strTemplateFileName + "&IpAddress=" + ipAddress;
                break;
            case ServerTypes.Cdyne:
               requestURL = "http://ws.cdyne.com/ip2geo/ip2geo.asmx/ResolveIP?ipAddress="
                    + ipAddress + "&licenseKey=0";
                break;
        }

        // create the web client and obtain the response data
        // as a byte array
        WebClient webclient = new WebClient();
        byte[] response = webclient.DownloadData(requestURL.ToString());
        // process the XML result to obtain a validation result
        return processXMLResult(response);

    }
    catch (Exception)
    {
        return false;
    }
}

Besides, you can get to know your real IP address by which you will be identified in the global network with the help of the demo application. The class also presents a possibility to check the validity of the entered IP. The source code, containing only basic functions necessary for getting information by IP is attached to the article. In real projects developed by KBSoft, a class with wider opportunities which doesn't use free services and allows to collect information about visitors of the developed sites and make different reports is used.

Use of the Source Code

You can find the demo application allowing to receive information by IP address and the source code of this application in the archive attached to the article. The demo application uses a simplified version of the class, delivered in the form of a DLL library. This class provides basic functions of information receipt by IP address. You can find the source code of this class here. You can use this code in your program products (sites) with research aims, in order to receive information by IP address or just use the demo application for getting information about singular IP addresses. In order to use the source code attached to the article, it is necessary that .NET Framework 2.0 or higher is installed in the system, and the library KBS_IPLocatorClass.dll be in the same folder as the executed file. If you have Visual Studio 2005, you can run the demo application by opening KBS_IPLocatorDemo.sln solution and executing the code. Or you can simply start the executed file KBS_IPLocatorDemo.exe. As Geobytes puts a restriction on the quantity of requests per hour (not more than 20), if you exhausted this limit, you can get information via CDYNE, but in this case the information will be less detailed.

The demo application presents the following data:

ParameterDescription
IPThis field presents IP address for which the request was sent.
Country IDThis field is an internal key used to relate a specific row in the DB table to related rows in the Regions, Cities, and Dmas tables. This is the primary key and is unique. The number "113" for example is the key for India.
CountryThis field is a string representation of the country name, for example, "India", "Singapore" or "Canada".
FIPSFIPS104 or more precisely FIPS 10-4 is a publication by the Federal Information Processing Standards body that identifies geopolitical entities around the world with a code. For example, "AS07" is the state of Victoria in Australia. This field represents logical groupings of these four letter codes, so in this example the letters "AS" would be stored in the "FIPS104" field. More information about this publication can be found at the National Imagery and Mapping Agency at this page and this page .
ISO 2This field represents the "A 2" column in the ISO 3166 document which lists each country with associated alpha and numeric codes. More information on ISO 3166 standard can be found at the International Organization for Standardization web-site at this page. An example is "US" which represents the United States of America.
ISO 3This field represents the "A 3" column in the ISO 3166 document which lists each country with associated alpha and numeric codes. More information on ISO 3166 standard can be found at the International Organization for Standardization web-site at this page. An example is "GTM" which represents Guatemala.
ISO NThis field represents the "Number" column in the ISO 3166 document which lists each country with associated alpha and numeric codes. More information on the ISO 3166 standard can be found at the International Organization for Standardization web-site at this page. An example is "036" which represents Australia.
InternetThis field represents the "ccTLD" code designated by IANA (Internet Assigned Numbers Authority) which is employed in the DNS to identify hosts in various countries around the world. For example, the code "uk" is assigned to the United Kingdom. The "ccTLD" codes are based on the "A 2" column of the ISO 3166 standard document. More information on the Internet Assigned Numbers Authority can be found at this page, more information on the International Organization for Standardization can be found at this page.
CommentThis field represents the comments.
Region IDThis field is an internal key used to relate a specific row in the DB table to related rows in the "Cities" table.
RegionThis field is the character representation of the sub-country geographical region. For example, the value of this column might be the state "Florida" in the USA or "New South Wales" in Australia.
CodeThis field is a 2 character code used to represent the general abbreviation for a sub-country geographical region. Each region has a unique code within a given country, and existing official codes were used where possible. "Official" can mean various things, but the ISO 3166-2 codes have been favored at this page. The codes are mnemonic. They consist of the first letter in the subdivision name, followed by a letter that occurs later in that name, unless it was impossible to assign unique codes by that rule.
ADMThis field is related to the "FIPS104" field in the "Countries" table. Where the "FIPS104" code is used to identify a geopolitical region at country resolution, the "ADM1Code" is used to identify a geopolitical region at a sub-country region. For more information on the "ADM1Code" field visit the National Imagery and Mapping Agency Internet site at this page.
City IDThis field is an internal key used to relate specific rows in DB table to related rows in the "Subnets" and "NearbyCities" table. This is the primary key and is unique. The number "1225" for example is the key for Melbourne, Australia.
CityThis field is a character representation of the actual name of the city. For example "New York", "London" or "Paris". This field should not be used as the key for queries since the names of cities are not globally unique.
LatitudeThis field is used to store the approximate latitude of the city specified by the rest of the row.
LongitudeThis field is used to store the approximate longitude of the city specified by the rest of the row.
Time ZoneThis field is used to indicate which time zone the city is in. For example, the "TimeZone" field value for Sydney, Australia is "+10:00" being UTC (or GMT) plus 10 hours. The time is represented in UTC (Coordinated Universal Time). UTC was formerly known as GMT (Greenwich Mean Time). See ISO8601:2000(E) for further information on time zone representation. The ISO is available in this PDF-file.
DMAThis is the DMA code as assigned by Nielsen Media - http://www.nielsenmedia.com. Designated Market Areas (DMA's) are used by Nielsen Media Research to identify TV stations whose broadcast signals reach a specific area and attract the most viewers. A DMA consists of all counties whose largest viewing share is given to stations of that same market area. Non-overlapping DMA's cover the entire continental United States, Hawaii and parts of Alaska. There are currently 210 Designated Market Areas throughout the U.S.
DMA IDThis field is an internal key used to relate specific rows in the DB table.
MarketThis field represents the market area.
CertaintyThis field is used to list the certainty for the information contained in the DB in relation to the subnet being searched on and the city that it is related to. A lower certainty rating may indicate that the data gathered was inconclusive or that the particular subnet serves a greater geographical area.
Location CodeThis field is a 4 character alpha code used to represent a city within a given country and region. The code consists of the first letter of each word, then followed by the next 2 characters in the last word. If the city has only one word, then the first 4 characters would be used. When generating these codes if there was a duplicate code generated, then the next letter in the city name would be used. If a duplicate code is still found, then the last character of the code is replaced by a letter from the alphabet in descending order.
IP AddressThis field is the IP address that used to perform the resolution. It is useful to use this field to pass in a selected IP address for use with testing.
Area CodeThis field represents the area code.
State ProvinceThis field represents the information about state province.

Table 1 - Main parameters of the response.

Conclusion

In the article, various techniques of getting information by IP Address were demonstrated. The source code of the created class in the form of a DLL-library presenting main functions of getting information by IP address, and also a demo application using this class were shown.

References

  • This is a demo page of the company CDYNE for getting information by IP Address.
  • This is a demo page of the company IP2Location for getting information by IP Address.
  • This page contains the set of demo pages of the company FraudLabs for getting information by IP Address.
  • This is a demo page of the company Geobytes for getting information by IP Address.
  • This page contains the latest version of this article and the C# implementation of the class described in the article.

History

  • 1 March, 2008 -- Original version posted

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Russian Federation Russian Federation
Alexandr Golovanov is a .NET developer at KB_Soft Group, an offshore software development company located in Russia, Novosibirsk. Here he has worked
on various .NET projects.

Comments and Discussions

 
Questionis it free? Pin
amrelgarhy811-Sep-08 13:32
amrelgarhy811-Sep-08 13:32 
AnswerRe: is it free? Pin
adriancs11-Jan-14 2:30
mvaadriancs11-Jan-14 2:30 
GeneralHostIp.info Pin
compiler1-Mar-08 18:51
compiler1-Mar-08 18:51 
GeneralRe: HostIp.info Pin
Alexandr Golovanov3-Mar-08 18:10
Alexandr Golovanov3-Mar-08 18:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.