Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting an IP address to host name

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?

like image 735
Anil Kumar C Avatar asked May 11 '12 15:05

Anil Kumar C


People also ask

Can an IP address be a hostname?

A host, or website, on the Internet is identified by a host name, such as www.example.com . Host names are sometimes called domain names. Host names are mapped to IP addresses, but a host name and an IP address do not have a one-to-one relationship. A host name is used when a web client makes an HTTP request to a host.

What is the process of converting an IP address to its domain name equivalent?

DNS servers convert URLs and domain names into IP addresses that computers can understand and use. They translate what a user types into a browser into something the machine can use to find a webpage. This process of translation and lookup is called DNS resolution.

How would you convert an IP address into a hostname in Java?

In Java, you can use InetAddress. getLocalHost() to get the Ip Address of the current Server running the Java app and InetAddress. getHostName() to get Hostname of the current Server name.


3 Answers

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);
like image 92
ewein Avatar answered Sep 20 '22 06:09

ewein


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

like image 21
SomeKittens Avatar answered Sep 21 '22 06:09

SomeKittens


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();
like image 27
nairbv Avatar answered Sep 21 '22 06:09

nairbv