How To: ICMP Ping in Java (JDK 1.5 and above)
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.
Filed under Computer Security, Headline News, How To, Java Software, Linux, Tech Note, Web, Web Hosting, Web Services, Windows |
|
RSS 2.0 |
Trackback this Article
|
Email this Article
You may also like to read |



Add to Technorati Favorites

March 7th, 2007 at 3:00 pm
This is not true for Windows. ICMP was not implemented for Windows.
Check
http://forum.java.sun.com/thread.jspa?threadID=712790&messageID=4122700
March 7th, 2007 at 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
August 10th, 2007 at 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…
June 13th, 2008 at 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 [...]