InetAddress.getLocalHost()
현재 사용중인 컴퓨터의 네트워크 식별자가 저장된
InetAddress 객체를 반환하는 정적메소드
호스트 이름의 컴퓨터를 검색할 수 없는 경우 UnknownHostException 발생
현재 사용중인 컴퓨터는 기본적으로 [127.0.0.1]의 IP 주소(LoopBack IP)라 제공되며 [localhost]라는 호스트이름 사용
InetAddress.toString()
InetAddress.getHostName()
InetAddress.getHostAddress()
InetAddress.getByName(String name)
InetAddress.getAllByName(String name)
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressApp {
public static void main(String[] args) throws UnknownHostException {
// InetAddress 클래스: 네트워크 식별자(IP 주소와 호스트 이름)를 저장한 객체를 생성하기 위한 클래스
// InetAddress.getLocalHost(): 현재 사용중인 컴퓨터의 네트워크 식별자가 저장된
// InetAddress 객체를 반환하는 정적 메소드
// => 호스트 이름의 컴퓨터를 검색할 수 없는 경우 UnknownHostException 발생
// 현재 사용중인 컴퓨터는 기본적으로 [127.0.0.1]의 IP 주소(LoopBack IP)라 제공되며 [localhost]라는 호스트명 사용
InetAddress myComputer = InetAddress.getLocalHost(); // UnknownHostException 발생
// InetAddress.toString(): InetAddress 객체에 저장된 네트워크 식별자를 문자열로 반환하는 메소드
System.out.println("myComputer = " + myComputer); // toString() 메소드 자동 호출
// InetAddress.getHostName(): InetAddress 객체에 저장된 호스트이름을 문자열로 반환하는 메소드
System.out.println("myComputer = " + myComputer.getHostName());
// InetAddress.getHostAddress(): InetAddress 객체에 저장된 IP 주소를 문자열로 반환하는 메소드
System.out.println("myComputer = " + myComputer.getHostAddress());
System.out.println("================================================================================");
// InetAddress.getByName(String name) : 매개변수로 전달받은 호스트이름(IP 주소)에 대한
// 네트워크 식별자가 저장된 InetAddress 객체를 반환하는 정적메소드
InetAddress google = InetAddress.getByName("www.google.com");
System.out.println("[www.naver.com] 컴퓨터의 IP 주소 = " + google.getHostAddress());
System.out.println("================================================================================");
// InetAddress.getAllByName(String name) : 매개변수로 전달받은 호스트이름(IP 주소)에 대한
// 네트워크 식별자가 저장된 InetAddress 객체 배열을 반환하는 정적메소드
InetAddress[] naver = InetAddress.getAllByName("www.naver.com");
for (InetAddress address : naver) {
System.out.println("[www.naver2.com] 컴퓨터의 IP 주소 = " + address.getHostAddress());
}
System.out.println("================================================================================");
}
}