In Java there are multiple ways to check ping and port check. You could use system default’s ping command, Java’s native method InetAddress
utility, HttpURLConnection
and some more.
In production or your testing environment, if you want to perform multiple port checks let’s say hundreds of checks at the same time then sometimes InetAddress.isReachable()
method is not getting correct response.
Infect in my case, I noticed 100% failure while trying to connect to www.google.com. Do you have any of below questions?
- java – Why does InetAddress.isReachable return false, when I can ping the IP address?
- How to check if I have an internet connection?
- Java Code Examples for java.net.InetAddress.isReachable()
- java check if ip address is reachable
- How to test if a remote system is reachable?
Follow this tutorial if you want to perform ping check using HttpURLConnection.openConnection()
In this tutorial we will go over 2 different ways to perform Ping check:
- InetAddress.isReachable(timeout) method
- Crunchify’s
crunchifyAddressReachable(host, port, timeout)
method which works 100% of the time
Let’s get started:
- Create class CrunchifyInetAddressIsReachable.java.
- We will create 2 methods pingCheckbyInetAddressisReachable() and pingCheckbyCrunchifyisReachable() in which we will perform above 2 different tests.
What method are we using crunchifyAddressReachable()?
We are using java.net.Socket
in our implementation. Socket class implements client sockets. With the help of connect()
utility we are getting 100% of the time correct result. Kindly take a look at below code for more details.
CrunchifyInetAddressIsReachable.java
package crunchify.com.tutorial; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; /** * @author Crunchify.com * Problem: Sometimes InetAddress.isReachable() gives false result. * We have implemented the same Reachable check using Socket. It works almost 100% of the time. * Comparison added. * Version: 1.1 * */ public class CrunchifyInetAddressIsReachable { static String host = "www.google.com"; public static void main(String[] args) { // check ping using default Java Utility pingCheckbyInetAddressisReachable(); // check ping using modified Crunchify Utility pingCheckbyCrunchifyisReachable(); } private static void pingCheckbyInetAddressisReachable() { try { InetAddress crunchifyAddr = InetAddress.getByName(host); boolean reachable = crunchifyAddr.isReachable(2000); if (reachable) { System.out.println("InetAddress.isReachable(timeout) Result ==> Ping successful for host: " + host); } else { System.out.println("InetAddress.isReachable(timeout) Result ==> Ping failed for host: " + host); } } catch (Exception e) { e.printStackTrace(); } } private static void pingCheckbyCrunchifyisReachable() { try { crunchifyAddressReachable(host, 80, 2000); System.out.println("\nOverloaded isReachable(host, port, timeout) Result ==> Ping successful for host: " + host); } catch (Exception e) { System.out.println("\nOverloaded isReachable(host, port, timeout) Result ==> Ping failed for host: " + host); } } /* * Overriding default InetAddress.isReachable() method to add 2 more arguments port and timeout value * * Address: www.google.com * port: 80 or 443 * timeout: 2000 (in milliseconds) */ private static boolean crunchifyAddressReachable(String address, int port, int timeout) throws IOException { Socket crunchifySocket = new Socket(); try { // Connects this socket to the server with a specified timeout value. crunchifySocket.connect(new InetSocketAddress(address, port), timeout); // Return true if connection successful return true; } catch (IOException exception) { exception.printStackTrace(); // Return false if connection fails return false; } finally { crunchifySocket.close(); } } }
Once you copy code over to Eclipse environment, just run as Java Application to see below result.
Console Output:
InetAddress.isReachable(2000) Result ==> Ping failed for host: www.google.com Overloaded isReachable(host, port, timeout) Result ==> Ping successful for host: www.google.com
The post How to Implement your own InetAddress.isReachable(String address, int port, int timeout) method in Java? appeared first on Crunchify.
0 Commentaires