투케이2K

825. (Android/Java) [간단 소스] InetAddress 사용해 특정 도메인 접속 가능한 IP 주소 확인 및 Socket connect 연결 상태 확인 본문

Android

825. (Android/Java) [간단 소스] InetAddress 사용해 특정 도메인 접속 가능한 IP 주소 확인 및 Socket connect 연결 상태 확인

투케이2K 2024. 8. 2. 18:52

[개발 환경 설정]

개발 툴 : AndroidStudio

개발 언어 : Java / Kotlin

 

[소스 코드]

    try {

        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    InetAddress [] addressAll = InetAddress.getAllByName("www.google.com");
                    for(InetAddress adr : addressAll)
                    {
                        S_Log._D_("Connection Test [Start] :: " + String.valueOf(adr.getHostAddress()), null);
                        try {
                            new Socket().connect(new InetSocketAddress(adr, 80), 2000); // TODO [HTTP : 기본 80 포트 / 특정 포트 지정]
                            S_Log._W_("Connection Test [Success] :: " + String.valueOf(adr.getHostAddress()), null);
                        } catch (Exception e) {
                            S_Log._E_("Connection Test [Fail] :: " + String.valueOf(adr.getHostAddress()), null);
                        }
                    }
                } catch (Exception e) {
                }
            }
        });
    }
    catch (Exception e){
        e.printStackTrace();
    }
 

[결과 출력]

 

D///===========//: ================================================
I/: [LOG :: CLASS PLACE :: com.example.javaproject.A_Intro$1$1.run(A_Intro.java:346)]
I/: ----------------------------------------------------
I/: [LOG :: NOW TIME :: 2024-08-02 10:55:28 금요일]
I/: ----------------------------------------------------
I/: [LOG :: DESCRIPTION :: Connection Test [Start] :: 2404:6800:400a:804::2004]
D///===========//: ================================================


W///===========//: ================================================
I/: [LOG :: CLASS PLACE :: com.example.javaproject.A_Intro$1$1.run(A_Intro.java:349)]
I/: ----------------------------------------------------
I/: [LOG :: NOW TIME :: 2024-08-02 10:55:29 금요일]
I/: ----------------------------------------------------
I/: [LOG :: DESCRIPTION :: Connection Test [Success] :: 2404:6800:400a:804::2004]
W///===========//: ================================================



D///===========//: ================================================
I/: [LOG :: CLASS PLACE :: com.example.javaproject.A_Intro$1$1.run(A_Intro.java:346)]
I/: ----------------------------------------------------
I/: [LOG :: NOW TIME :: 2024-08-02 10:55:29 금요일]
I/: ----------------------------------------------------
I/: [LOG :: DESCRIPTION :: Connection Test [Start] :: 142.250.76.132]
D///===========//: ================================================


W///===========//: ================================================
I/: [LOG :: CLASS PLACE :: com.example.javaproject.A_Intro$1$1.run(A_Intro.java:349)]
I/: ----------------------------------------------------
I/: [LOG :: NOW TIME :: 2024-08-02 10:55:29 금요일]
I/: ----------------------------------------------------
I/: [LOG :: DESCRIPTION :: Connection Test [Success] :: 142.250.76.132]
W///===========//: ================================================

 

반응형
Comments