프로그래밍/JAVA 프로그래밍 초급
[자바][네트워크]java.net.*; 패키지의 InetAddress.getAllByName() 사용
앗뜨거
2014. 7. 3. 09:24
728x90
InetAddress.getAllByName() 사용방법
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class Ex02_01 { public static void main(String[] args) { try { //InetAddress ia = new InetAddress(); 처럼 하면 좋겠지만 //InetAddress 는 생성자 자체가 존재하지 않기때문에 쓸수없다 InetAddress[] ia = InetAddress.getAllByName("www.naver.com"); //여러개의 주소를 얻는방법 for(int i=0; i < ia.length ; i++) { System.out.println(i + 1 + "번째의 주소 => " + ia[i].getHostName() +" 아이피 => "+ia[i].getHostAddress()); } } catch(UnknownHostException e){} } } |
InetAddress 는 생성자가 존재하지 않기 때문에 new InetAddress(); 처럼 쓸 수 없다.
그래서
InetAddress[] ia = InetAddress.getAllByName("www.naver.com"); 이런식으로 써야된다.
배열형으로 쓴이유는
InetAddress.getAllByName() 의 getAllByName() 이 배열형으로 읽어오기때문이다 여러개의주소를 한번에 담아낸다
그래서 InetAddress.getAllByName() 를 쓸때는 for문 등 반복자를 통해서 출력해야된다.