Check if a server is reachable or not by InetAddress in Java

Description

The following code shows how to check if a server is reachable or not by InetAddress.

Example


  /*  w  w w.jav a  2 s.c om*/


import java.net.InetAddress;

public class Main {
  public static void main(String[] argv) throws Exception {
    InetAddress address = InetAddress.getByName("google.com");
    System.out.println("Name: " + address.getHostName());
    System.out.println("Addr: " + address.getHostAddress());
    System.out.println("Reach: " + address.isReachable(3000));
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Network »




NetworkInterface
URI
URL
HTTP
HTTP Read
IP
Socket
UDP
URL Encode