Demonstrating the Use of the InetAddress Class - Java Network

Java examples for Network:IP Address

Description

Demonstrating the Use of the InetAddress Class

Demo Code

import java.io.IOException;
import java.net.InetAddress;

public class Main {
  public static void main(String[] args) {
    printAddressDetails("www.java2s.com");

    // Print the loopback address details
    printAddressDetails(null);//  w  w  w  .ja  va2s .  c om

    // Print the loopback address details using IPv6 format
    printAddressDetails("::1");
  }

  public static void printAddressDetails(String host) {
    System.out.println("Host '" + host + "' details starts...");

    try {
      InetAddress addr = InetAddress.getByName(host);
      System.out.println("Host IP Address: " + addr.getHostAddress());
      System.out.println("Canonical Host Name: " + addr.getCanonicalHostName());

      int timeOutinMillis = 10000;
      System.out.println("isReachable(): " + addr.isReachable(timeOutinMillis));
      System.out.println("isLoopbackAddress(): " + addr.isLoopbackAddress());
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      System.out.println("Host '" + host + "' details ends...");
      System.out.println("");
    }
  }
}

Result


Related Tutorials