본문 바로가기
JAVA/Networking

[java/net] InetAddress ,getByName,getAllByName,getHostAddress 사용 예시

by java나유 2022. 6. 27.

 

InetAddress : IPnetworkAddress

getByName : 접속할 도메인명

getAllByName :도메인 IP정보 확인 (!배열!)

getHostAddress:IP주소(숫자)를 문자열로 반환

public static void main(String[] args) throws Exception { //server에 연결 하기위해
		//InetAddress : IPnetworkAddress
		//getByName : 접속할 도메인 주소명을 말합니다.
		
		InetAddress ia = InetAddress.getByName("localhost"); //!!자기 기본 정보 확인!!
		System.out.println(ia);
		//System.out.println(ia.getHostName()); // 도메인 명 
		//System.out.println(ia.getHostAddress()); //도메인에 Ip주소 

		//System.out.println(ia.toString());
		
		//getAllbyName : 도메인 IP정보 확인 (배열)
		InetAddress[] ia2 = InetAddress.getAllByName("google.com");//배열
		System.out.println(ia2.length); //ip4개 있음 확인
		int w=0;
		while(w<ia2.length) {
			System.out.println(ia2[w].getHostAddress()); //다 출력 
			w++;
		}

getHostAddress:IP주소(숫자)를 문자열로 반환

 

결과값 :

length로 아이티 개수와 총 아이피 주소를 확인할 수 있어요.

구글은 총1개의 주소, 네이버는 4개, 다음은 3개를 사용하네요.

728x90

댓글