tags:

views:

193

answers:

1

hi, I need to get a domain name if I have an IP address (e.g. I type 209.85.129.103 and the program should find out that is the Google address)

As far as I've figured out is get the hostname:

IPHostEntry IpToDomainName = Dns.GetHostEntry("209.85.129.103");
string HostName = IpToDomainName.HostName; //it returns "fk-in-f103.1e100.net"

but it's not that I want. I don't know how to achieve that. Any ideas will be helpful

+1  A: 

I guess you're talking about getting the top-level domain name from the host name? The TLD is just the last two dot-separated parts of the full host name, so a function would look like this:

public static string GetTopLevelDomain(string hostName)
{
    int lastDot = hostName.LastIndexOf('.');
    if (lastDot < 0)
        return hostName;
    int previousDot = hostName.LastIndexOf('.', lastDot - 1);
    return (previousDot >= 0) ? hostName.Substring(previousDot + 1) : hostName;
}

If you're actually trying to figure out who owns the domain, you have to use a whois lookup. Here's a whois example in C#. The information just comes back as plain text; keep in mind that it won't necessarily even tell you the real person or company who owns it, sometimes that information is private and all you'll get is the registrar (like GoDaddy).

Also, different whois servers will give different information and different areas; for example you can get information on a U.S. domain with ARIN, but for European domains you need to use RIPE instead. Honestly I hope that this isn't what you're trying to do because you're going to find that it's a quite a tar-pit; there's no simple way of reliably determining that Domain X is owned by Company Y.

Aaronaught
Ok, but the user should know that is Google
Tony
I'm developing a small HTTP monitor, so I need to knows if the user types "209.85.129.103" he'll know that it is google.com
Tony
@Tony: You can only do that if the host name for `209.85.129.103` is actually `google.com` or one of its subdomains. As I explained, you can try to use whois lookups to get more information, but I would have to say that you're on a sinking ship - the process of finding the right whois server and parsing its output is very complicated and extremely error-prone, since there is no standard governing its output. I suppose you could compromise and try to parse the whois text from arin.net, and if that fails, just show the original host name or IP.
Aaronaught