Java UnknownHostException

2023. 2. 11. 23:02Java

프로젝트 내 src 내 javabasic 패키지 내 Ex5InetAddress.java

package javabasic;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Ex5InetAddress {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		InetAddress[] naverInet = null;
		try {
			naverInet = InetAddress.getAllByName("www.naver.com");
			System.out.println("Naver Server Ip 알아보기");
			for (InetAddress in : naverInet) {
				System.out.println("네이버 컴퓨터 이름 :" + in.getHostName());
				System.out.println("네이버 컴퓨터 서버 ip : " + in.getHostAddress());
			}
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		;

		InetAddress[] googleInet = null;
		try {
			googleInet = InetAddress.getAllByName("www.google.com");
			System.out.println("Google Server Ip 알아보기");
			for (InetAddress in : googleInet) {
				System.out.println("구글 컴퓨터 이름 :" + in.getHostName());
				System.out.println("구글 컴퓨터 서버 ip : " + in.getHostAddress());
			}
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

실행 결과

Naver Server Ip 알아보기
네이버 컴퓨터 이름 :www.naver.com
네이버 컴퓨터 서버 ip : 223.130.200.104
네이버 컴퓨터 이름 :www.naver.com
네이버 컴퓨터 서버 ip : 223.130.195.95
Google Server Ip 알아보기
구글 컴퓨터 이름 :www.google.com
구글 컴퓨터 서버 ip : 142.250.206.228

InetAddress[] java.net.InetAddress.getAllByName(String host) throws UnknownHostException

 

public static InetAddress[] getAllByName(String host)
    throws UnknownHostException {
    return getAllByName(host, null);
}

getAllByName(String host) 메서드는 InetAddress 클래스의 static 메서드이다.

getAllByName(String host) 메서드는

하나의 host 으 이름을 받아서

그 시스템 위에 설정된 name service 를 기반으로 한

그 host 의 IP 주소들이 담긴 하나의 배열을 반환한다.


 

'Java' 카테고리의 다른 글

Java Socket  (0) 2023.02.13
Java Class URL  (0) 2023.02.11
Java InetAddress  (0) 2023.02.11
Java start()  (0) 2023.02.11
Java Thread class 와 Runnable interface  (0) 2023.02.11