Java Hostname Lookup
The following snippet shows how to get the hostname and IP address.
<geshi lang="java"> public static void main(String[] args) throws UnknownHostException { java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost(); System.out.println("Hostname of local machine: " + localMachine.getCanonicalHostName()); System.out.println("IP of local machine: " + localMachine.getHostAddress()); java.net.InetAddress localhost = java.net.InetAddress.getByName("localhost"); System.out.println("Hostname of localhost: " + localhost.getCanonicalHostName()); System.out.println("IP of localhost: " + localhost.getHostAddress()); }
Hostname of local machine: mail.theeggeadventure.com IP of local machine: 67.207.137.114 Hostname of localhost: localhost IP of localhost: 127.0.0.1 </geshi>