Get All IP addresses by host name in Java

Description

The following code shows how to get All IP addresses by host name.

Example


   //  ww w.j a v a  2s.  c  o  m
import java.net.InetAddress;
import java.net.UnknownHostException;

public class Main {

  public static void main (String[] args) {

    try {
      InetAddress[] addresses = InetAddress.getAllByName("www.google.com");
      for (int i = 0; i < addresses.length; i++) {
        System.out.println(addresses[i]);
      }
    }
    catch (UnknownHostException e) {
      System.out.println("Could not find www.apple.com");
    }

  }

}

The code above generates the following result.





















Home »
  Java Tutorial »
    Network »




NetworkInterface
URI
URL
HTTP
HTTP Read
IP
Socket
UDP
URL Encode