기본 개념
호스트 이름 : 네트워크상에서 컴퓨터의 이름
DNS : Domain Name System의 약자로 호스트 이름은 표준인 DNS를 사용해서 생성됩니다.
IP : DNS를 사용하지 않을 경우 사용하는 컴퓨터의 이름입니다.
DNS 서버 : 컴퓨터들이 통신을 하려면 DNS 보다는 IP가 필요합니다. 이때 DNS이름을 IP주소로 변환하여 주는 작업을 수행하는 서버가 DNS 서버 입니다.
로컬 호스트 : 모든 컴퓨터는 자기 자신을 가리키는 특별한 호스트 이름과 IP 주소를 가지고 있습니다. localhost와 127.0.0.1이 자기 자신을 가리키는 이름과 주소 입니다. 간단한 테스트에 유용합니다.
URL : Uniform Resource Locator의 약자로 인터넷상의 파일이나 데이터베이스 같은 자원에 대한 주소를 지정하는 방법입니다.
HTTP Protocol : URL을 보내면 자원을 파싱해서 응답
예제
DNS → IP
import java.net.InetAddress; import java.net.UnknownHostException; public class host2ip { public static void main(String[] args) { String hostname = "www.naver.com"; try { InetAddress address = InetAddress.getByName(hostname); System.out.println("IP 주소 : " + address.getHostAddress()); } catch (UnknownHostException e) { System.out.println(hostname + "의 IP주소를 찾을 수 없습니다."); } } }

인터넷 파일 다운로드
HTTP 연결을 통하여 인터넷 파일을 읽으려면
- URL 클래스의 객체를 생성합니다.
- URL 객체를 이용해서 연결하기 위하여 URLConnection 객체를 생성합니다.
- URLConnection 객체의 getInputStream() 메소드를 호출하여서 입력 스트림을 얻습니다.
- 스트림에서 데이터를 읽습니다.
와 같은 단계를 거쳐야 합니다.
예제
URL 데이터를 읽어서 콘솔에 표시하는 프로그램
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; public class URLConnectionReader { public static void main(String[] args) throws Exception { URL site = new URL("https://www.naver.com/"); URLConnection url = site.openConnection(); BufferedReader br = new BufferedReader( new InputStreamReader(url.getInputStream()) ); String inLine; while((inLine = br.readLine()) != null){ System.out.println(inLine); } br.close(); } }
Share article