Network 1 2 Java URLConnection1 URLConnection2 URLConnection 1

  • Slides: 27
Download presentation
Network

Network

목차 1. 네트워크 배경 2. Java 네트워크 URLConnection클래스란1 URLConnection클래스란2 URLConnection클래스의 활용 1 URLConnection클래스의 활용

목차 1. 네트워크 배경 2. Java 네트워크 URLConnection클래스란1 URLConnection클래스란2 URLConnection클래스의 활용 1 URLConnection클래스의 활용 2 6. 1 URLEncoder, URLDecoder클래스란? 6. 2 URLEncoder, URLDecoder예제 7. URLClass. Loader클래스 5. 1 5. 2 5. 3 5. 4 6. URLEncoder와 URLDecoder클래스 4. 1 URL클래스란 4. 2 URL클래스의 활용 1 4. 3 URL클래스이 활용 2 5. URLConnection클래스 3. 1 Inet. Address클래스란 3. 2 Inet. Address클래스의 활용 4. URL클래스 2. 1 자바네트워크의 구성 2. 2 java. net패키지 3 Inet. Address클래스 1. 1 네트워크란 1. 2 TCP/IP 1. 3 URL 7. 1 URLClass. Loader란 7. 2 URLClass. Loader의 활용 8. 마무리 12/12/2021 2

2. Java Network 2. 1 자바 네트워크의 구성 2. 2 java. net package

2. Java Network 2. 1 자바 네트워크의 구성 2. 2 java. net package

3. Inet. Address클래스 3. 1 Inet. Address 클래스란? 3. 2 Inet. Address 클래스의 활용

3. Inet. Address클래스 3. 1 Inet. Address 클래스란? 3. 2 Inet. Address 클래스의 활용

3. 1 Inet. Address클래스란? 3. Inet. Address클래스 IP주소를 표현하고 제어하는 기능을 제공하는 클래스 Host

3. 1 Inet. Address클래스란? 3. Inet. Address클래스 IP주소를 표현하고 제어하는 기능을 제공하는 클래스 Host Name자체를 가지고 주소를 표현해 그에 관련된 기능을 제공 인스턴스 생성법 생성자 메서드가 없다 다음의 static메서드를 사용하여 인스턴스를 생성한다. public static Inet. Address get. By. Name(String host) throws Unknown. Host. Exception (호스트의 이름(www. bitacademy. net) 또는 주소로 Inet. Address객체 반환) public static Inet. Address get. Local. Host() throws Unknown. Host. Exception (로컬 컴퓨터에 대한 Inet. Address객체 반환. 방화벽으로 가려진경우 127. 0. 0. 1) public static Inet. Address[] get. All. By. Name(String host) throws Unknown. Exception (호스트에 대한 모든 IP주소를 Inet. Address객체 배열로 반환) Inet. Address my. Ip = Inet. Address. get. By. Name(“www. bitacademy. net”); 12/12/2021 11

3. Inet. Address클래스 3. 2 Inet. Address클래스의 활용 Inet. Address클래스로 비트(www. bitacademy. net)의 IP정보

3. Inet. Address클래스 3. 2 Inet. Address클래스의 활용 Inet. Address클래스로 비트(www. bitacademy. net)의 IP정보 알아내기 Inet. Address. Test. java import java. net. *; public class Inet. Address. Test{ public static void main(String[] args) { Inet. Address my. Ip = null; try { my. Ip = Inet. Address. get. By. Name("www. bitacademy. net"); System. out. println("호스트이름 : " + my. Ip. get. Host. Name()); System. out. println("호스트주소 : " + my. Ip. get. Host. Address()); System. out. println("to. String : " + my. Ip. to. String()); System. out. println("로컬호스트 : " + my. Ip. get. Local. Host()); }catch(Unknown. Host. Exception e) {System. out. println(e); } } } 주요 메서드 get. Host. Name(); 호스트명 get. Host. Address(); 호스트 주소 get. Local. Host(); 로컬호스트 정보 12/12/2021 12

4. URL class 4. 1 URL class란? 4. 2 URL class의 활용 1 4.

4. URL class 4. 1 URL class란? 4. 2 URL class의 활용 1 4. 3 URL class의 활용 2

4. 1 URL class란? 4. URL클래스 웹상의 주소(URL) 그 자체를 나타내는 클래스 웹상에 존재하는

4. 1 URL class란? 4. URL클래스 웹상의 주소(URL) 그 자체를 나타내는 클래스 웹상에 존재하는 자원에 접근하거나 유일한 주소를 나타내기 위해 사용됨 Inet. Address클래스보다 확장된 개념 : 네트웍 연결까지 가능 URL클래스 객체 생성 URL url = new URL(protocol: resource); URL bit = new URL(http: //www. bitacademy. net); URL url = new URL(String protocol, String host, String file); URL bit = new URL(“http”, ”www. bitacademy. net”, “ 8080”, “html/index. html”); 주요 멤버메서드 public 12/12/2021 String get. File() : URL파일명 리턴 String get. Host() : URL의 호스트명 리턴 int get. Port() : URL포트번호 리턴 Strimg get. Protocol (): URL의 프로토콜 리턴 14

4. 2 URL클래스의 활용 1 4. URL클래스로 페이지를 다운받아 확인하기 위한 절차 URL객체 생성

4. 2 URL클래스의 활용 1 4. URL클래스로 페이지를 다운받아 확인하기 위한 절차 URL객체 생성 URL객체로의 스트림 열기 Input. Stream in = url. open. Stream(); 받는 스트림을 버퍼에 저정 URL home = new URL(“http: //www. bitacademy. net: 80/index. html”); Buffered. Reader br = new Buffered. Reader( new put. Stream. Reader(in)); 버퍼를 화면에 출력하고 스트림 닫기 Buffered. Reader br; URL home = new URL(“http: //www. bitacademy. net: 80/index. html”); br = new Buffered. Reader(new Input. Stream. Reader(home. open. Stream())); while ((line = br. read. Line()) != null) { System. out. println(line); } br. close(); open. Stream() = open. Connection(). get. Input. Stream() 12/12/2021 15

4. 3 URL클래스의 활용 2 4. URL클래스로 호스트의 정보를 표시하고 페이지를 다운로드 URLTest. java

4. 3 URL클래스의 활용 2 4. URL클래스로 호스트의 정보를 표시하고 페이지를 다운로드 URLTest. java import java. net. *; import java. io. *; public class URLTest { public static void main(String args[]) { try { URL home = new URL("http: //www. bitacademy. net: 80/index. html"); Buffered. Reader br; String line; System. out. println("Port: "+home. get. Port()); System. out. println("Protocol: "+home. get. Protocol()); System. out. println("Host. Name: "+home. get. Host()); System. out. println("File: "+home. get. File()); System. out. println("Ref: "+home. get. Ref()); br = new Buffered. Reader(new Input. Stream. Reader(home. open. Stream())); while ((line = br. read. Line()) != null) { System. out. println(line); } br. close(); } catch (Exception e) { System. out. println("Exception: " + e); } } } 12/12/2021 16

5. URLConnection class 5. 1 URLConnection class 1 5. 2 URLConnection class 2 5.

5. URLConnection class 5. 1 URLConnection class 1 5. 2 URLConnection class 2 5. 3 URLConnection class 활용

5. URLConnection클래스 5. 2 URLConnection class 2 URLConnection클래스의 주요 메서드 URL객체에 대하여 여러가지 프로토콜을

5. URLConnection클래스 5. 2 URLConnection class 2 URLConnection클래스의 주요 메서드 URL객체에 대하여 여러가지 프로토콜을 연결해 주는 메서드로 구성 void set. Do. Input(boolean do. Input) boolean get. Do. Input() URLConnction객체가 입력용인지를 반환. 디폴트 true void set. Do. Output(boolean do. Output) URLConnection객체를 입력용으로 사용여부 결정 출력용으로 사용할것인지의 여부 세팅 boolean get. Do. Output() URLConnection객체가 출력용인지를 반환. 디폴트 false 12/12/2021 19

5. URLConnection클래스 5. 4 URLConnection class 활용 야후로 검색어 ‘자북’을 보내 검색하는 예제 URLConnection.

5. URLConnection클래스 5. 4 URLConnection class 활용 야후로 검색어 ‘자북’을 보내 검색하는 예제 URLConnection. java import java. net. *; import java. io. *; public class URLConnection. Output { public static void main(String[] args) { try { URL url = new URL("http: //search. naver. com/search. naver"); URLConnection conn = url. open. Connection(); conn. set. Do. Output(true); Output. Stream. Writer wr = new output. Stream. Writer(conn. get. Output. Stream()); wr. write("p=" + URLEncoder. encode(비트")); wr. flush(); wr. close(); String line=null; Buffered. Reader rd = new Buffered. Reader(new Input. Stream. Reader(conn. get. Input. Stream())); while ((line = rd. read. Line()) != null) { System. out. println(line); } rd. close(); } catch (Exception e) {e. print. Stack. Trace(); } } } 12/12/2021 20

6. URLEncoder & URLDecoder class 6. 1 URLEncder, URLDecoder class 란? 6. 2 URLEncoder,

6. URLEncoder & URLDecoder class 6. 1 URLEncder, URLDecoder class 란? 6. 2 URLEncoder, URLDecoder 예제

6. URLEncoder, Decoder 6. 2 URLEncoder, URLDecoder 예제 문자열을 인코딩하고 디코딩하는 예제 URLTest. java

6. URLEncoder, Decoder 6. 2 URLEncoder, URLDecoder 예제 문자열을 인코딩하고 디코딩하는 예제 URLTest. java import java. net. *; public class URLEn. Decoder. Test{ public static void main(String [] args) { String str, encoded. Str, decoded. Str; str=“자바스킬업 – www. bitadademy. net"; encoded. Str = URLEncoder. encode(str, “KSC 5601”); decoded. Str = URLDecoder. decode(encoded. Str, “KSC 5601”); System. out. println("str : " + str); System. out. println("encoded. Str : " + encoded. Str); System. out. println("decoded. Str : " + decoded. Str); } } 12/12/2021 23

7. URLClass. Loader class 7. 1 URLClass. Loader class 란? 7. 2 URLClass. Loader

7. URLClass. Loader class 7. 1 URLClass. Loader class 란? 7. 2 URLClass. Loader 활용

7. 1 URLClass. Loader란? 7. URLClass. Loader클래스 URL을 통해 JAR파일 및 디렉터리의 자원 및

7. 1 URLClass. Loader란? 7. URLClass. Loader클래스 URL을 통해 JAR파일 및 디렉터리의 자원 및 클래스파일 로드 ‘/’로 끝나는 URL에서 JAR파일 및 디렉터리 참조 URL 검색경로는 네트워크상의 경로 또는 파일디렉터리상의 경로 Class. Loader클래스를 상속하여 load. Class()메서드 사용 URLClass. Loader생성자 메서드 public URLClass. Loader(URL[] urls) Class. Loader를 사용해 지정된 새로운 URLClass. Loader생성 URL[] url. Array = {new File(“C: /myweb/network/src"). to. URL()}; URLClass. Loader ucl = new URLClass. Loader(url. Array); Class c =ucl. load. Class("Hello"); Hello hello =(Hello)c. new. Instance(); 12/12/2021 25

7. 2 URLClass. Loader 활용 7. URLClass. Loader클래스 URLClass. Loader를 이용하여 디렉터리에서 바이트코드 로딩하는

7. 2 URLClass. Loader 활용 7. URLClass. Loader클래스 URLClass. Loader를 이용하여 디렉터리에서 바이트코드 로딩하는 예제 URLClass. Loader. Test. java import java. net. *; import java. io. *; public class URLClass. Loader. Test{ public static void main(String args[]) throws Exception { URL[] url. Array = {new File(“C: /myweb/network/"). to. URL()}; URLClass. Loader ucl = new URLClass. Loader(url. Array); Class c =ucl. load. Class("Hello"); Hello hello =(Hello)c. new. Instance(); hello. say. Hello("홍길동"); } C: java16>javac URLClass. Loader. Test. java } C: java16>java URLClass. Loader. Test class Hello{ 홍길동씨 안녕하세요 public Hello(){ } public void say. Hello(String name){ System. out. println(name + "씨 안녕하세요"); } } 12/12/2021 26