네트워킹
1.1 클라이언트/서버(client/server)
1.2 IP주소(IP address)
1.3 InetAddress
1.4 URL(Uniform Resource Location)
DNS서버에 접근 > ip를 가져옴 > port번호
port번호 : ip주소를 알면 컴퓨터까지 접근 가능
public class InetAddressTest {
public static void main(String[] args) throws UnknownHostException {//throws - 호출한 메서드에서 예외처리
// InetAddress 클래스 ==> IP주소를 다루기 위한 클래스
// www.naver.com의 IP정보 가져오기
InetAddress naverIp = InetAddress.getByName("www.naver.com"); //호스트 이름을 파라미터로
System.out.println("Host Name : " + naverIp.getHostName());
System.out.println("Host Address : " + naverIp.getHostAddress());
System.out.println("toString : " + naverIp.toString()); //hostName과 hostIp를 같이 보여줌
// 자신의 컴퓨터의 IP정보 가져오기
InetAddress localIp = InetAddress.getLocalHost();
System.out.println("내 컴의 Host Name : " + localIp.getHostName());
System.out.println("내 컴의 Host Address : " + localIp.getHostAddress());
System.out.println();
// IP주소가 여러개인 호스트의 정보 가져오기
InetAddress[] ips = InetAddress.getAllByName("www.google.com");
for(InetAddress ip : ips) {
System.out.println(ip.toString());
}
}
}
public class URLTest01 {
public static void main(String[] args) throws MalformedURLException {
// URL 클래스 ==> 인터넷에 존재하는 서버들의 자원에 접근할 수 있는 주소를 다루는 클래스
// http://www.ddit.or.kr:80/index.html?ttt=123
URL url = new URL("http","ddit.or.kr", 80, "index.html?ttt=123");
System.out.println("Protocol : " + url.getProtocol());
System.out.println("Host : " + url.getHost());
System.out.println("Port : " + url.getPort());
System.out.println("File : " + url.getFile()); //Query string까지 가져옴
System.out.println("Path : " + url.getPath());
System.out.println("Query : " + url.getQuery());
System.out.println();
System.out.println(url.toExternalForm()); //전체 주소
}
}
public class URLTest02 {
public static void main(String[] args) throws IOException {
// URLConnection ==> 애플리케이션과 URL간의 통신 연결을 위한 클래스
// 특정 서버의 정보와 파일 내용을 가져와 출력하는 예제
//URL url = new URL("https://www.naver.com/index.html");
URL url = new URL("https://www.naver.com");
// URLConnection 객체 구하기
URLConnection urlCon = url.openConnection();
// Header 정보 가져오기
Map<String, List<String>> headerMap = urlCon.getHeaderFields();
for(String headerKey : headerMap.keySet()) {
System.out.println(headerKey + " : " + headerMap.get(headerKey));
}
System.out.println("--------------------------------------------------");
// 해당 문서의 내용을 가져와 화면에 출력하기
// (index.html문서 내용 가져오기)
/*
// 방법1 ==> URLConnection객체를 이용하는 방법
// 파일을 읽어오기 위한 스트림 객체 생성
InputStream is = urlCon.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");//바이트기반을 문자로 바꾸는 보조스트림
BufferedReader br = new BufferedReader(isr);//속도증가를 위해 버퍼스트림 사용
// 자료를 읽어와 출력하기
while(true) {
String str = br.readLine(); // 한 줄씩 읽기
if(str == null) { // 더이상 읽어올 데이터가 없다.
break;
}
System.out.println(str);
}
br.close(); // 스트림 닫기
*/
// 방법2 ==> URL객체를 이용해서 스트림 객체를 구한다.
// URL객체의 openStream()메서드 이용
InputStream is2 = url.openStream();
BufferedReader br2 = new BufferedReader(
new InputStreamReader(is2, "utf-8")
);
while(true) {
String str = br2.readLine(); // 한 줄씩 읽기
if(str == null) { // 더이상 읽어올 데이터가 없다.
break;
}
System.out.println(str);
}
br2.close(); // 스트림 닫기
}
}