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 – IP 주소를 ping할 수 있는데 InetAddress.isReachable이 false를 반환하는 이유는 무엇입니까?
- 인터넷에 연결되어 있는지 확인하는 방법은 무엇입니까?
- java.net.InetAddress.isReachable()에 대한 Java 코드 예제
- 자바는 IP 주소에 도달할 수 있는지 확인
- 원격 시스템에 연결할 수 있는지 테스트하는 방법은 무엇입니까?
HttpURLConnection.openConnection()을 사용하여 ping 검사를 수행하려면 이 자습서를 따르십시오.
이 자습서에서는 Ping 검사를 수행하는 두 가지 방법을 살펴보겠습니다.
- InetAddress.isReachable(시간 초과) 메서드
- 100% 작동하는 Crunchify의
crunchifyAddressReachable(host, port, timeout)
메서드
시작하자:
- 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 Application으로 실행하면 아래 결과를 볼 수 있습니다.

콘솔 출력:
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 |