티스토리 뷰

카테고리 없음

Network

mming_0213 2019. 7. 24. 00:14
  • IP 주소 : 컴퓨터를 구별하는데 사용되는 고유한 값으로 인터넷에 연결된 모든 컴퓨터는 IP주소를 갖는다. 
  • InetAddress : IP주소를 다루는 Class
package Day190723;

import java.net.*;
import java.util.Arrays;

public class NetworkEx1 {

	public static void main(String[] args) {
		InetAddress ip = null;
		InetAddress[] ipArr = null;
		
		try {
			//static InetAddress getByName(String host) 도메인명(host)을 통해 IP주소를 얻는다
			ip = InetAddress.getByName("www.naver.com");
			System.out.println("getHostName():" + ip.getHostName());
			System.out.println("getHostAddress():" + ip.getHostAddress());
			System.out.println("toString():" + ip.toString());
			
			//byte[] getAddress() IP주소를 byte배열로 반환
			byte[] ipAddr = ip.getAddress();
			System.out.println("getAddress():" + Arrays.toString(ipAddr));
			
			String result = "";
			for(int i = 0; i < ipAddr.length; i++) {
				result += (ipAddr[i] < 0) ? ipAddr[i] + 256 : ipAddr[i];
				result += ".";
			}
			System.out.println("getAddress()+256:" + result);
			System.out.println();	
		}catch(UnknownHostException e) {
			e.printStackTrace();
		}
		
		try {
			//static InetAddress getLocalHost() 지역호스트의 IP주소를 반환
			ip = InetAddress.getLocalHost();
			System.out.println("getHostName():" + ip.getHostName());
			System.out.println("getHostAddress():" + ip.getHostAddress());
			System.out.println();
		}catch(UnknownHostException e) {
			e.printStackTrace();
		}
		
		try {
			//static InetAddress[] getAllByName(String host) 도메인명에 지정된 모든 호스트의 IP주소를 배열에 담아 반환
			ipArr = InetAddress.getAllByName("www.naver.com");
			
			for(int i = 0; i < ipArr.length; i++) {
				System.out.println("ipArr[" + i + "]:" + ipArr[i]);
			}
			
		}catch(UnknownHostException e) {
			e.printStackTrace();
		}
	}
}
//실행결과
getHostName():www.naver.com
getHostAddress():125.209.222.141
toString():www.naver.com/125.209.222.141
getAddress():[125, -47, -34, -115]
getAddress()+256:125.209.222.141.

getHostName():LAPTOP-CGH98AH8
getHostAddress():172.30.1.46

ipArr[0]:www.naver.com/125.209.222.141
ipArr[1]:www.naver.com/125.209.222.142

 

  • URL : 인터넷상의 자원에 접근할 수 있는 주소
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함