Java Hostname Lookup: Difference between revisions

From EggeWiki
m (Created page with '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.InetAddr…')
 
mNo edit summary
 
Line 1: Line 1:
The following snippet shows how to get the hostname and IP address.
The following snippet shows how to get the hostname and IP address.


<geshi lang="java">
<syntaxhighlight lang="java">
public static void main(String[] args) throws UnknownHostException {
public static void main(String[] args) throws UnknownHostException {
java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
Line 15: Line 15:
Hostname of localhost: localhost
Hostname of localhost: localhost
IP of localhost: 127.0.0.1
IP of localhost: 127.0.0.1
</geshi>
</syntaxhighlight>


[[Category:Java | Hostname Lookup]]
[[Category:Java | Hostname Lookup]]

Latest revision as of 18:42, 10 December 2011

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