상세 컨텐츠

본문 제목

Java - InetAddress

개발/Java

by 뉴에이스 2018. 10. 2. 16:54

본문

네트워크
ip, url, tcp
	
InetAddress : ip 정보와 연관된 클래스 (ip 정보 관리)
 

소스

 

public static void main(String[] args) {
	try {
		// 현재 컴퓨터의 IP 주소 얻기
		InetAddress local = InetAddress.getLocalHost();
		System.out.println(local);
		System.out.println(local.getHostName());
		System.out.println(local.getHostAddress());
		
		// 외부 사이트의 IP 주소 얻기
		//InetAddress jun = InetAddress.getByName("www.9999.com");
		InetAddress jun = InetAddress.getByName("www.jun2food.com");
		System.out.println(jun);
		
		// 대형사이트의 IP 주소 모두 얻기
		InetAddress[] navers = InetAddress.getAllByName("www.naver.com");
		for(InetAddress naver : navers) {
			System.out.println(naver);
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 

결과

 

PC/xxx.xxx.xxx.xxx // 내PC/내IP
PC // 내 PC 이름
xxx.xxx.xxx.xxx // 내 PC IP
www.jun2food.com/203.246.167.175
www.naver.com/125.209.222.142
www.naver.com/210.89.164.90

 

관련글 더보기

댓글 영역