Finding IP Addresses and Domain Names


// find IP address for given domain String hostName = www.timothyfisher.com"; String ip =    InetAddress.getByName(hostName).getHostAddress(); // find domain name for ip address String ipAddress = "66.43.127.5"; String hostName =    InetAddres.getByName(ipAddress).getHostName();



In this phrase, we get a hostname when you know the IP address of a remote host, and we get an IP address when you start with the hostname of the remote host. To accomplish both of these tasks, we rely on the InetAddress class.

We use the getByName() static method of the InetAddress class to create an InetAddress instance. We can pass either an IP address or a hostname into the getByName() method to create the InetAddress instance. Once we have the InetAddress instance, we can call the getHostAddress() method to return the IP address as a String. If we already know the IP address, we can call the getHostName() method to return the hostname as a String. If the host name cannot be resolved, the getHostName() method will return the IP address.




JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net