내가 보려고 정리하는/JAVA
Network Test - InetAddress
보동이용용
2023. 1. 24. 16:01
반응형
InetAddress 클래스
==> IP주소를 다루기 위한 클래스
1. naver의 IP정보 가져오기
// www.naver.com의 IP정보 가져오기
InetAddress naverIp = InetAddress.getByName("www.naver.com");
System.out.println("HostName : " + naverIp.getHostName());
System.out.println("HostAddress : " + naverIp.getHostAddress());
System.out.println("toString : " + naverIp.toString());
System.out.println();
2. 내 컴퓨터의 정보도 가져올 수 있다.
// 자신의 컴퓨터의 IP정보 가져오기
InetAddress localIp = InetAddress.getLocalHost();
System.out.println("HostName : " + localIp.getHostName());
System.out.println("HostAddress : " + localIp.getHostAddress());
System.out.println("toString : " + localIp.toString());
System.out.println();
3. IP주소가 여러개인 호스트의 정보도 가져올 수 있다.
InetAddress[] naverArr = InetAddress.getAllByName("www.naver.com");
for(InetAddress ip : naverArr) {
System.out.println(ip);
}
반응형