Resolving ip-address of a hostname

Java

Java Problem Overview


I have the DNS server IP address and a hostname.

Using Java, how can I find the IP address of the hostname as returned by that DNS server using the IP address and the hostname?

Java Solutions


Solution 1 - Java

Take a look at InetAddress and the getHostAddress() method.

InetAddress address = InetAddress.getByName("www.example.com"); 
System.out.println(address.getHostAddress()); 

Solution 2 - Java

You can do it like this:

for(InetAddress addr : InetAddress.getAllByName("stackoverflow.com"))
    System.out.println(addr.getHostAddress());

Solution 3 - Java

You can use InetAddress for this. Try the below code,

InetAddress address = InetAddress.getByName("www.yahoo.com");
System.out.println(address.getHostAddress());
System.out.println(address.getHostName());

Solution 4 - Java

As suggested by all above, you can use InetAddress.getByName("hostName") but this can give you a cached IP, Read the java documentation for the same. If you want to get a IP from DNS you can use:

InetAddress[] ipAddress = DNSNameService.lookupAllHostAddr("hostName");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDark MatterView Question on Stackoverflow
Solution 1 - JavathegrinnerView Answer on Stackoverflow
Solution 2 - JavaWalery StrauchView Answer on Stackoverflow
Solution 3 - JavaAbhinaba BasuView Answer on Stackoverflow
Solution 4 - JavaRudraView Answer on Stackoverflow