JAVA InetAddress 클래스를 활용한 IP 읽기 예제

728x90
public class NetTest1 {
       public static void main(String[] args) {
              InetAddress ia;
              InetAddress ia2[];
              
              try {
                     ia = InetAddress.getByName("www.naver.com");
                     System.out.println(ia);
                     System.out.println(ia.getHostAddress());
                     System.out.println(ia.getHostName());
                     
                     System.out.println("-----");
                     
                     ia2 = InetAddress.getAllByName("www.daum.net");
                     for(InetAddress a:ia2) {
                           System.out.println(a.getHostAddress());
                           System.out.println(a.getHostName());
                     }
                     
                     System.out.println("-----");
                     
                     ia = InetAddress.getLocalHost();
                     System.out.println(ia.getHostAddress());
                     System.out.println(ia.getHostName());
              } catch (Exception e) {
                     System.out.print("err: "+e);
              }
       }
}

< 결과값 >

www.naver.com/210.89.164.90
210.89.164.90
www.naver.com
-----
211.231.99.17
www.daum.net
203.133.167.81
www.daum.net
-----
192.168.0.**
사용자 PC ID








본 포스팅은 IT 교육기관인 KIC 캠퍼스의 지원을 받은 리포트입니다.
혹시 잘못되거나 문제 소지시 댓글 남겨주시면 조치하겠습니다.




728x90