Programatically using ICMP Ping is a great way to establish that a server is up and running. Programatically utilisant ICMP Ping est un excellent moyen de mettre en place un serveur qui est en place et fonctionne. Previously you couldn’t do ICMP ping (what ping command does in Linux/Unix & Windows) in java without using JNI or exec calls. Auparavant, on ne pouvait pas faire de ping ICMP (ce qui ne commande ping sous Linux / Unix et Windows) en java sans utiliser JNI ou exec appels. Here is a simple and reliable method to do ICMP pings in Java without using JNI or NIO. Voici une simple méthode fiable et de faire ping ICMP en Java sans utiliser JNI ou NIO.


String host = “172.16.0.2″ String host = "172.16.0.2"
int timeOut = 3000; // I recommend 3 seconds at least int timeout = 3000 / / Je recommande 3 secondes au moins
boolean status = InetAddress.getByName(host).isReachable(timeOut) boolean status = InetAddress.getByName (hôte). isReachable (timeout)

status is true if the machine is reachable by ping; false otherwise. statut est vrai si la machine est accessible par ping, faux sinon. 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. Best effort est fait pour tenter d'atteindre l'hôte, mais les pare-feu et configuration de serveur mai bloc demandes résultant en un statut inaccessible alors que certains ports mai être accessibles. 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. Une application typique utilisera ICMP echo request si le privilège peut être obtenu, sinon il essaiera d'établir une connexion TCP sur le port 7 (Echo) de l'hôte de destination.

In Linux/Unix you may have to suid the java executable to get ICMP Ping working, ECHO REQUESTs will be fine even without suid. Sous Linux / Unix, vous devez mai suid java exécutable pour obtenir ICMP Ping de travail, ECHO demandes seront bien même sans suid. However on Windows you can get ICMP Ping without any issues whatsoever. Toutefois, sur Windows, vous pouvez obtenir ICMP Ping sans problème que ce soit.