How To: ICMP Ping in Java (JDK 1.5 and above)

By Angsuman Chakraborty, Gaea News Network
Saturday, January 6, 2007

Programatically using ICMP Ping is a great way to establish that a server is up and running. Previously you couldn’t do ICMP ping (what ping command does in Linux/Unix & Windows) in java without using JNI or exec calls. Here is a simple and reliable method to do ICMP pings in Java without using JNI or NIO.


String host = "172.16.0.2"
int timeOut = 3000; // I recommend 3 seconds at least
boolean status = InetAddress.getByName(host).isReachable(timeOut)

status is true if the machine is reachable by ping; false otherwise. Best effort is made to try to reach the host, but firewalls and server configuration may block requests resulting in a unreachable status while some specific ports may be accessible. A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host.

In Linux/Unix you may have to suid the java executable to get ICMP Ping working, ECHO REQUESTs will be fine even without suid. However on Windows you can get ICMP Ping without any issues whatsoever.

Discussion

chiranjeevi
May 31, 2010: 11:27 pm

how to open thread to a particular process


bruno
July 24, 2009: 3:32 pm

After a lot of testing I can say with certainty that in Windows XP isReachable() will fail where windows ping succeeds.

isReachable() cannot be trusted to reliably ping hosts outside of the local subnet.

The most likely reason is that windows is using ICMP ping while isReachable() is failing to use it. This is the case up to at least Java 6.

February 13, 2009: 11:32 pm

I was very pleased with the program and thought the practical go-at-your-own-speed teaching style was great!


skeptikal
November 17, 2008: 7:20 pm

Regarding ICMP ping in Windows, above poster quoted the Java site: “A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host.”

Read this carefully. Most current versions of Windows (XP SP1, SP2, SP3, Vista, etc.) do NOT use the ICMP to deliver the packet for Java or pretty much anywhere else, instead using the less reliable & more inaccurate port 7 echo.

Read further on the official Java forums for a more complete discussion. Sorry guys, no guarantee on a Windows platform your ping will go out ICMP or not.

October 22, 2008: 12:59 am

Where are your related posts?


David Saldaña
October 9, 2008: 7:10 pm

Thank you a lot, I tried it and work fine. I will make a little program that send a mail when the redirect from the domain fail.

Thak you again.

June 13, 2008: 5:58 pm

[...] How To: ICMP Ping in Java (JDK 1.5 and above) __________________ Daniel @ [To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.] Language is froth on the surface of thought [...]

August 10, 2007: 10:05 am

[SPS-687] Details: add “ping” tools…

Sometimes customers have problems with transponders that show as offline due to network failures. To help with that, we should add a “ping tool” to the power supply details in two versions:

- The first version would ping the power supply, using t…

March 7, 2007: 9:44 pm

It does implement ICMP ECHO on windows.

“Test whether that address is reachable. Best effort is made by the implementation to try to reach the host, but firewalls and server configuration may block requests resulting in a unreachable status while some specific ports may be accessible. A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host.” - JDK 5.0 Documentation


LookingforPing
March 7, 2007: 3:00 pm

This is not true for Windows. ICMP was not implemented for Windows.

Check
https://forum.java.sun.com/thread.jspa?threadID=712790&messageID=4122700

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :