다연이네

[days28] InetAddress 클래스 본문

Java

[days28] InetAddress 클래스

 다연  2020. 11. 1. 00:20
반응형

InetAddress 클래스

자바에서 자바에서는 IP주소를 다루는 클래스를 제공하는 클래스

 

package days28;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Ex01 {

	public static void main(String[] args) {
		
		InetAddress local;
		
		//getHostAddress() : IP주소를 반환하는 메소드
		//getAllByName() : 도메인명을 가지고 IP[] 반환
		try {
			local = InetAddress.getLocalHost(); 
            //로컬PC의 주소 정보를 반환하는 메소드 getLocalHost()
			System.out.println("> 내 컴퓨터의 IP주소 : "+local.getHostAddress());
			
			//naver.com 호스트 IP 주소 확인
			String host = "naver.com";
			InetAddress[] iaArr = InetAddress.getAllByName(host);
			for (InetAddress ia : iaArr) {
				System.out.println(ia.getHostAddress());
			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}

	}

}

 

 

URL

- 인터넷에 존재하는 여러 서버들이 제공하는 [자원에 접근]할 수 있는 주소
- 프로토콜://호스트:포트번호/경로명/파일명?쿼리스트링#참조

  예) www.naver.com:80/test/abc/xxx.html?query=a#test

- 자바 URL클래스 존재

package days28;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class Ex02 {

	public static void main(String[] args) {
		
		String spec = "https://www.daum.net";
		
		try {
			URL url = new URL(spec);
			
			InputStream is = url.openStream();
			
			try(BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
			
				String line = null;
				while ((line=br.readLine())!=null) {
					System.out.println(line);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		

	}

}

daum의 html 정보가 출력됨

반응형
Comments