package kr.s29.network;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressMain01 {
public static void main(String[] args) {
BufferedReader br =null;
InetAddress iaddr = null;
String name = null;
try {
br = new BufferedReader (new InputStreamReader(System.in));
System.out.print("도메인명 입력:");
name = br.readLine();
//InetAddress를 이용해서 IP주소 구하기
iaddr = InetAddress.getByName(name); //ip주소를 구해줌
System.out.println("호스트이름 : "+ iaddr.getHostName());//호스트 이름을 구하는 코드
System.out.println("호스트 IP주소 : " + iaddr.getHostAddress());// 호스트의 Ip주소를 구하는 코드
System.out.println("-----------------------");
//로컬 호스트 ip주소 구하기
iaddr = InetAddress.getLocalHost();
System.out.println("로컬 호스트 이름 : " + iaddr.getHostName());//내 컴퓨터 이름정보
System.out.println("로컬 호스트 IP주소 : " + iaddr.getHostAddress());//내 컴퓨터 ip주소
}catch(UnknownHostException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally {
if(br != null)try {br.close();}catch(IOException e) {};
}
}
}
도메인명 입력:naver.com
호스트이름 : naver.com
호스트 IP주소 : 223.130.200.236
-----------------------
로컬 호스트 이름 : DESKTOP-1M0APMI
로컬 호스트 IP주소 : 211.184.126.158
package kr.s29.network;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressMain02 {
public static void main(String[] args) {
BufferedReader br = null; //얘는 꼭 null로 초기화 해줘야함.
InetAddress[] addresses;
String name;// = null안써도됨.
try {
br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("도메인명 입력 : ");
name = br.readLine();
//해당 도메인과 매핑되어 있는 모든 주소를 읽어옴
addresses = InetAddress.getAllByName(name); //모든 IP주소를 읽어오는 코드
for(int i =0; i<addresses.length; i++) {
System.out.println("호스트 이름 : " + addresses[i].getHostName()+","+"Ip주소 : " + addresses[i].getHostAddress());
}
}catch(UnknownHostException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally {
if(br !=null)try {br.close();} catch(IOException e) {};
}
}
}
도메인명 입력 : naver.com
호스트 이름 : naver.com,Ip주소 : 223.130.200.236
호스트 이름 : naver.com,Ip주소 : 223.130.200.219
호스트 이름 : naver.com,Ip주소 : 223.130.192.247
호스트 이름 : naver.com,Ip주소 : 223.130.192.248