how-to

Network

How to get local IP address and host name

For this example you need to use:

import java.net.InetAddress;
import java.net.UnknownHostException;

For getting IP address and host name of the computer use this code:

try {
  InetAddress ia = InetAddress.getLocalHost();

  /* -- write results */

  System.out.println( ia.getHostAddress() );
  System.out.println( ia.getHostName() );

  System.out.println( ia.toString() );

  System.out.println( ia.getCanonicalHostName() );

  for ( int i = 0; i < ia.getAddress().length; i++ )
    System.out.println( ia.getAddress()[i] );

} catch ( UnknownHostException ex ) {
}
The output is:
10.26.1.11
pc082
pc082/10.26.1.11
pc082.org.local
10
26
1
11