Java Hostname Lookup

From EggeWiki
Revision as of 18:42, 10 December 2011 by Brianegge (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The following snippet shows how to get the hostname and IP address.

	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