java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to ping a host?

Since Java 1.5 (Tiger) the InetAddress class introduces an isReachable() method that can be use to ping or check if the address specified by the InetAddress is reachable.



public class PingExample 
    public static void main(String[] args)
            InetAddress address = InetAddress.getByName("");

            // Try to reach the specified address within the timeout
            // periode. If during this periode the address cannot be
            // reach then the method returns false.
            boolean reachable = address.isReachable(10000);

            System.out.println("Is host reachable? " + reachable);
        } catch (Exception e)