I want to test whether a remote system is reachable using Java or in other words "send a ping" using Java. This functionality should be encapsulated in a method with boolean value, for example
public boolean isReachable(String ip) { // what to do :-) }
I've tested the Java Process class, but I don't think that it is the best way to do this, because of the complex output handling with OutputBuffers.
Process proc = Runtime.getRuntime().exec("ping " + ip);
Another possibility would be creating a Socket Connection and handle thrown exceptions, but if the remote system is a "naked" unix system, there might be no Socket on the other side :-) Additionally, I'd like to be able to set a timeout, when a remote system is not reachable.
So how could I do this? Thank you!
Enter "telnet + IP address or hostname + port number" (e.g., telnet www.example.com 1723 or telnet 10.17. xxx. xxx 5000) to run the telnet command in Command Prompt and test the TCP port status. If the port is open, only a cursor will show.
The ping command is a simple network utility command-line tool in Linux. It's a handy tool for quickly checking a host's network connectivity. It works by sending an ICMP message ECHO_REQUEST to the target host. If the host reply with ECHO_REPLY, then we can safely conclude that the host is available.
One of the biggest perks of Telnet is with a simple command you can test whether a port is open. Issuing the Telnet command telnet [domainname or ip] [port] will allow you to test connectivity to a remote host on the given port.
InetAddress.getByName(ip).isReachable(timeout);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With