name - return ip address in java




Resolving ip-address of a hostname (4)

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?


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");

In addition to what @daksh pointed out, you can disable the DNS response cache as suggested here but be aware of the timing of disabling the cache as well as caching of DNS responses made by your OS as well as other DNS servers as suggested here. Hope this helps


You can do it like this:

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

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());




java