Programatically using ICMP Ping is a great way to establish that a server is up and running. Programatically utilizzando ICMP Ping è un ottimo modo per stabilire che un server è installato e funzionante. Previously you couldn’t do ICMP ping (what ping command does in Linux/Unix & Windows) in java without using JNI or exec calls. Precedentemente che non si possono fare ping ICMP (cosa comando ping funziona su Linux / Unix e Windows) in Java, senza usare JNI o exec chiamate. Here is a simple and reliable method to do ICMP pings in Java without using JNI or NIO. Qui è un semplice ed affidabile metodo per fare ping ICMP in Java senza usare JNI o NIO.


String host = “172.16.0.2″ Stringa host = "172.16.0.2"
int timeOut = 3000; // I recommend 3 seconds at least int timeout = 3000; / / raccomando 3 secondi a meno
boolean status = InetAddress.getByName(host).isReachable(timeOut) boolean status = InetAddress.getByName (host). isReachable (timeout)

status is true if the machine is reachable by ping; false otherwise. status è vero se la macchina è raggiungibile da ping; false altrimenti. 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. Massimo sforzo è fatto per cercare di raggiungere l'host, ma firewall e configurazione del server può bloccare le richieste risultanti in un stato non raggiungibile, mentre alcuni porti possono essere accessibili. 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. Una tipica applicazione utilizzerà le richieste ICMP ECHO se il privilegio può essere ottenuta, altrimenti si tenta di stabilire una connessione TCP sulla porta 7 (ECHO) della destinazione ospitante.

In Linux/Unix you may have to suid the java executable to get ICMP Ping working, ECHO REQUESTs will be fine even without suid. In Linux / Unix potrebbe essere necessario suid java eseguibile per ottenere ICMP Ping di lavoro, richieste di ECHO sarà bene anche senza suid. However on Windows you can get ICMP Ping without any issues whatsoever. Tuttavia in Windows potete ottenere ICMP Ping, senza problemi di sorta.