Dave's Brain

Browse - Programming Tips - How can I resolve a DNS address into an IP-address?

Date: 2015may15
Language: Java
Keywords: lookup

Q.  How can I resolve a DNS address into an IP-address?


A.  use InetAddress.getByName() like this:

import java.net.InetAddress; import java.net.UnknownHostException; String lookupHostIp(final String host) { String address = null; try { InetAddress a = java.net.InetAddress.getByName(host); address = a.getHostAddress(); } catch (UnknownHostException e) { address = "0.0.0.0"; } return address; }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.