10

In my java application if user enters the IP we need to display the host name, if host name is given then we need to display the IP of the host.

For example if user enters an IP address like 173.194.36.37 application should display google.com and vice verse.

Are there any utilities available to perform this operation?

Anil Kumar C
  • 1,604
  • 4
  • 22
  • 43
  • you know that an IP number may have several hostnames? for example 127.0.0.1 has *both* localhost and localhost.localdomain – vidstige May 11 '12 at 15:34
  • @vidstige Not with reverse DNS. Here the IP is mapped to exactly one host name, the so-called "canonical host name". – glglgl May 11 '12 at 15:35
  • 1
    you can not get unique result see http://aruljohn.com/ip2hostname.html for 173.194.36.37 see bom04s02-in-f5.1e100.net not google.com! – Sajad Bahmani May 11 '12 at 15:37
  • 2
    `173.194.36.37` does not resolve to `google.com` but to `bom04s02-in-f5.1e100.net`. The IP block is owned by Google, though. – Philipp Reichart May 11 '12 at 15:50
  • @SjB You get `bom04s02-in-f5.1e100.net`, right. And only that. Multiple names can point to one IP address, of course. But an IP address can reverse-resolve to only one host name. – glglgl May 11 '12 at 17:01

4 Answers4

20

If you are coding in Java, try using InetAddress

InetAddress addr = InetAddress.getByName("173.194.36.37");
String host = addr.getHostName();
System.out.println(host);
Jeremy
  • 22,188
  • 4
  • 68
  • 81
ewein
  • 2,695
  • 6
  • 36
  • 54
2

What you're looking for is something called DNS. This project seems to be what you're looking for.

SomeKittens
  • 38,868
  • 19
  • 114
  • 143
  • No need to bring in third party libraries if there's `InetAddress.getHostName()`. – Philipp Reichart May 11 '12 at 15:36
  • 2
    @PhilippReichart The question says "Are there any utilities available", so I think why not open-source third party libraries. – Prakash K May 11 '12 at 15:47
  • @PrakashK Simplicity. Why bring in *over 100* classes when all you want can be achieved by calling an existing method? Much less moving parts, much less to go wrong. – Philipp Reichart May 11 '12 at 15:58
  • @PhilippReichart The InetAddress class will only get you the host name of the server, it won't get you the domain, which is what the OP was asking (although he didn't phrase it too well). – Jon May 11 '12 at 17:09
  • @Jon You do realize the naked domain is just one `substring()` away from any host name? That hardly requires a library. – Philipp Reichart May 11 '12 at 17:23
  • @Philipp Reichart Running some of the InetCode on a Google server IP, I get "bom04s02-in-f5.1e100.net" for the host name. Where is the Google domain name in that? – Jon May 14 '12 at 08:31
  • We're talking about different things here. The DNS records for the IP address `173.194.36.37` do not mention Google at all. The only way Google is related to that IP address is *ownership*, which is unrelated to DNS and cannot be answered by DNS. Ownership of domain names and IP addresses is queried via the WHOIS protocol. AFAICT dnsjava doesn't support WHOIS, so there's no way it's capable to get "the Google domain" from the IP address. – Philipp Reichart May 14 '12 at 11:07
2

The project SomeKittens referred to you looks like a complete DNS server written in Java, which might be more than you need. Have a look at java.net.InetAddress:

java.net.InetAddress.getByName("example.com").getHostAddress();
Philipp Reichart
  • 20,771
  • 6
  • 58
  • 65
nairbv
  • 4,045
  • 1
  • 24
  • 26
  • For Google.com this code gives me "74.125.237.100" as the result. – Jon May 14 '12 at 08:34
  • And this is correct, right? If I go to http://74.125.237.100/ I end up at google.com. – nairbv May 21 '12 at 23:17
  • No. The OP asked for something that would give you the domain name given an IP address. The code above gives the complete reverse - an IP address given a domain name. – Jon May 22 '12 at 08:42
  • 1
    He asked for "and vice verse" ... but if you were expecting my code fragment to give you a name for an IP, why did you pass google.com in the first place? – nairbv Jun 12 '12 at 23:32
0

In terms of domain name, there are no built in utilities, no. You can get the name of a host (but not the domain name) by using getCanonicalHostName() on InetAddress - that should work. The best answer here linked to the DNS Java project, which will get you the domain name.

Example code to connect to, and get the host name from, one of Google's servers is given below:

public class GetHostName {
public static void main(String[] args) throws Exception {
    InetAddress address = InetAddress.getByAddress(new byte[]{74, 125,(byte) 227, 7});
    System.out.println(address.getCanonicalHostName());
}
}
Jon
  • 3,510
  • 6
  • 27
  • 32