如何在 Java 中实现自己的 InetAddress.isReachable(String address, int port, int timeout) 方法?
已发表: 2020-10-06
在 Java 中有多种方法可以检查 ping 和端口检查。 您可以使用系统默认的 ping 命令、Java 的本地方法InetAddress
实用程序、 HttpURLConnection
等等。
在生产或测试环境中,如果要执行多个端口检查,比如说同时进行数百次检查,那么有时InetAddress.isReachable()
方法没有得到正确的响应。
在我的情况下感染,我在尝试连接到 www.google.com 时注意到 100% 失败。 您有以下任何问题吗?
- java – 当我可以 ping IP 地址时,为什么 InetAddress.isReachable 返回 false?
- 如何检查我是否有互联网连接?
- java.net.InetAddress.isReachable() 的 Java 代码示例
- java检查ip地址是否可达
- 如何测试远程系统是否可达?
如果您想使用 HttpURLConnection.openConnection() 执行 ping 检查,请遵循本教程
在本教程中,我们将介绍 2 种不同的方式来执行 Ping 检查:
- InetAddress.isReachable(timeout) 方法
- Crunchify 的
crunchifyAddressReachable(host, port, timeout)
方法 100% 有效
让我们开始吧:
- 创建类 CrunchifyInetAddressIsReachable.java。
- 我们将创建 2 个方法 pingCheckbyInetAddressisReachable() 和 pingCheckbyCrunchifyisReachable(),我们将在其中执行上述 2 个不同的测试。
我们使用 crunchifyAddressReachable() 的方法是什么?
我们在实现中使用java.net.Socket
。 Socket 类实现客户端套接字。 在connect()
实用程序的帮助下,我们得到了 100% 正确的结果。 请查看以下代码以获取更多详细信息。
CrunchifyInetAddressIsReachable.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
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 ( ) ; } } } |
将代码复制到 Eclipse 环境后,只需作为 Java 应用程序运行即可查看以下结果。

控制台输出:
1 2 3 |
InetAddress . isReachable ( 2000 ) Result == > Ping failed for host : www . google . com Overloaded isReachable ( host , port , timeout ) Result == > Ping successful for host : www . google . com |