Presentation is loading. Please wait.

Presentation is loading. Please wait.

Network.

Similar presentations


Presentation on theme: "Network."— Presentation transcript:

1 Network

2 목차 1. 네트워크 배경 2. Java 네트워크 3 InetAddress클래스 4. URL클래스
1.1 네트워크란 1.2 TCP/IP 1.3 URL 2. Java 네트워크 2.1 자바네트워크의 구성 2.2 java.net패키지 3 InetAddress클래스 3.1 InetAddress클래스란 3.2 InetAddress클래스의 활용 4. URL클래스 4.1 URL클래스란 4.2 URL클래스의 활용1 4.3 URL클래스이 활용2 5. URLConnection클래스 5.1 URLConnection클래스란1 5.2 URLConnection클래스란2 5.3 URLConnection클래스의 활용1 5.4 URLConnection클래스의 활용2 6. URLEncoder와 URLDecoder클래스 6.1 URLEncoder, URLDecoder클래스란? 6.2 URLEncoder, URLDecoder예제 7. URLClassLoader클래스 7.1 URLClassLoader란 7.2 URLClassLoader의 활용 8. 마무리 10/04/07

3 1. 네트워크 배경 1.1 네트워크란? 1.2 TCP/IP 1.3 URL

4 1.1 네트워크란? 같은 데이터 전송 프로토콜을 통해 교신하는 연결된 정보기기 집합 네트워킹
1. 네트워크 배경 1.1 네트워크란? 같은 데이터 전송 프로토콜을 통해 교신하는 연결된 정보기기 집합 네트워킹 하나의 장치로부터 다른 장치로 데이터를 옮기는 것 같은 데이터 전송 언어를 사용 : 프로토콜 하드웨어 프로토콜 애플리케이션 통신 물리적교신 그림 네트워크 통신구조 10/04/07

5 1.2 TCP/IP IP IP 주소 TCP 1. 네트워크 배경
Internet protocol (인터넷 : 전 세계적 TCP/IP프로토콜)‏ 다양한 기기들간의 통신을 지원 IP 주소 인터넷에서 하나 이상의 웹사이트에 보유된 서버에 부여된 유일한 주소 x.x.x.x 의 형태(여기서 x는 1 바이트 총 4바이트로 구성)‏ 의 형태 TCP Transfer Control Protocol IP의 상위에 존재하는 접속기반의 전송 프로토콜 전송된 모든 데이터들이 순서대로 목적지에 도착하도록 보장 클라이언트 서버 HTTP요청 HTTP요청 라우터 TCP 세그먼트 TCP 세그먼트 IP 데이터그램 IP 세그먼트 IP 세그먼트 이더넷 이더넷 프레임 이더넷 프레임 전기신호 전기적 신호 전기적 신호 그림 TCP/IP통신 10/04/07

6 1.3 URL Uniform Resource Locator 인터넷상의 자원에 대한 고유한 주소
1. 네트워크 배경 1.3 URL Uniform Resource Locator 인터넷상의 자원에 대한 고유한 주소 protocol://hostname[:port]/path/filename#section 의 형태 프로토콜 호스트 이름 포트 경로명 파일명 참조 그림 URL의 구조 프로토콜(protocol) : 자원에 접근하기 위해 사용되는 프로토콜 호스트 이름(HostName) : 접근하고자 하는 자원이 있는 위치 포트(Port) : TCP 연결에 대한 포트 번호. 생략시 기본포트번호 사용 (Echo:7 DayTime:13 FTP:21 Telnet:23 SMPT:25 경로명(Path) : 접근하려는 파일이 있는 위치 파일명(File) : 접근하려는 파일 10/04/07

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

8 2.1 자바 네트워크의 구성 자바 : 네트워크를 손쉽게 연결하는 도구, 네트워크 그 자체
2. Java 네트워크 2.1 자바 네트워크의 구성 자바 : 네트워크를 손쉽게 연결하는 도구, 네트워크 그 자체 통신을 구현하는 까다로운 부분이 이미 구현되어 있음 필요한 부분을 호출하여 통신 프로그램 개발 자바 네트워크의 구성 스트림기반 I/O 클래스 + java.net패키지 클래스 1. IP주소지정(InetAddress클래스)‏ 2. TCP로 연결(Socket클래스)‏ 3. 소켓을 사용하여 스트림 생성 4. 스트림을 통한 데이터의 교환 Client server 그림 자바 네트워크 TCP클라이언트 프로그래밍의 원리 10/04/07

9 2.2 java.net패키지 자바의 네트워크를 지원하는 패키지 URL클래스류와 Socket클래스류로 나눌수 있음
InetAddress클래스 URL클래스 URLConnection클래스 URLEncoder클래스 URLDecoder클래스 URLClassLoader클래스 대표적 Socket류 클래스들 클라이언트와 서버를 연결하여 통신하는데 관련 Socket클래스 SocketImpl클래스 ServerSocket클래스 DatagreamSocket클래스 10/04/07

10 3.1 InetAddress 클래스란? 3.2 InetAddress 클래스의 활용

11 3.1 InetAddress클래스란? IP주소를 표현하고 제어하는 기능을 제공하는 클래스
Host Name자체를 가지고 주소를 표현해 그에 관련된 기능을 제공 인스턴스 생성법 생성자 메서드가 없다 다음의 static메서드를 사용하여 인스턴스를 생성한다. public static InetAddress getByName(String host) throws UnknownHostException (호스트의 이름( 또는 주소로 InetAddress객체 반환)‏ public static InetAddress getLocalHost() throws UnknownHostException (로컬 컴퓨터에 대한 InetAddress객체 반환. 방화벽으로 가려진경우 )‏ public static InetAddress[] getAllByName(String host) throws UnknownException (호스트에 대한 모든 IP주소를 InetAddress객체 배열로 반환)‏ InetAddress myIp = InetAddress.getByName(“ 10/04/07

12 3. InetAddress클래스 3.2 InetAddress클래스의 활용 InetAddress클래스로 비트( IP정보 알아내기 InetAddressTest.java import java.net.*; public class InetAddressTest{ public static void main(String[] args) { InetAddress myIp = null; try { myIp = InetAddress.getByName(" System.out.println("호스트이름 : " + myIp.getHostName()); System.out.println("호스트주소 : " + myIp.getHostAddress()); System.out.println("toString : " + myIp.toString()); System.out.println("로컬호스트 : " + myIp.getLocalHost()); }catch(UnknownHostException e) {System.out.println(e);} } 주요 메서드 getHostName(); 호스트명 getHostAddress(); 호스트 주소 getLocalHost(); 로컬호스트 정보 10/04/07

13 4.1 URL class란? 4.2 URL class의 활용1 4.3 URL class의 활용2

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

15 4.2 URL클래스의 활용1 URL클래스로 페이지를 다운받아 확인하기 위한 절차
URL home = new URL(“ URL객체로의 스트림 열기 InputStream in = url.openStream(); 받는 스트림을 버퍼에 저정 BufferedReader br = new BufferedReader( new putStreamReader(in)); 버퍼를 화면에 출력하고 스트림 닫기 BufferedReader br; URL home = new URL(“ br = new BufferedReader(new InputStreamReader(home.openStream())); while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); openStream() = openConnection().getInputStream()‏ 10/04/07

16 4.3 URL클래스의 활용2 URL클래스로 호스트의 정보를 표시하고 페이지를 다운로드 URLTest.java 4. URL클래스
import java.net.*; import java.io.*; public class URLTest { public static void main(String args[]) { try { URL home = new URL(" BufferedReader br; String line; System.out.println("Port: "+home.getPort()); System.out.println("Protocol: "+home.getProtocol()); System.out.println("HostName: "+home.getHost()); System.out.println("File: "+home.getFile()); System.out.println("Ref: "+home.getRef()); br = new BufferedReader(new InputStreamReader(home.openStream())); while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); } catch (Exception e) { System.out.println("Exception: " + e); 10/04/07

17 5. URLConnection class 5.1 URLConnection class 1

18 5.1 URLConnection class 1 URL객체에 대한 프로토콜 연결을 나타내는 추상클래스
Application과 URL간의 통신링크를 위한 작업수행 URL에 데이터 쓰기를 구현하게 함 클라이언트가 서버에 보내는 서비스 요청질의(query)‏ Http로 폼 데이터를 post방식으로 보내는 것과 같은 역할 URLConnection클래스 생성자 메서드 Protected URLConnection(URL url) 지정된 URL로의 연결을 구성 프로토콜을 직접 구현하는 아들클래스에서만 호출가능 URLConnection의 일반적인 객체 생성 URL객체를 얻어서 그 객체로 Connection을 얻음 URL url = new URL(" URLConnection connection = url.openConnection(); 10/04/07

19 5.2 URLConnection class 2 URLConnection클래스의 주요 메서드
void setDoInput(boolean doInput) URLConnection객체를 입력용으로 사용여부 결정 boolean getDoInput() URLConnction객체가 입력용인지를 반환. 디폴트 true void setDoOutput(boolean doOutput)‏ 출력용으로 사용할것인지의 여부 세팅 boolean getDoOutput()‏ URLConnection객체가 출력용인지를 반환. 디폴트 false 10/04/07

20 5.4 URLConnection class 활용
야후로 검색어 ‘자북’을 보내 검색하는 예제 URLConnection.java import java.net.*; import java.io.*; public class URLConnectionOutput { public static void main(String[] args) { try { URL url = new URL(" URLConnection conn = url.openConnection(); conn.setDoOutput(true); OutputStreamWriter wr = new outputStreamWriter(conn.getOutputStream()); wr.write("p=" + URLEncoder.encode(비트")); wr.flush(); wr.close(); String line=null; BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); while ((line = rd.readLine()) != null) { System.out.println(line); } rd.close(); } catch (Exception e) {e.printStackTrace();} 10/04/07

21 6. URLEncoder & URLDecoder class
6.1 URLEncder, URLDecoder class 란? 6.2 URLEncoder, URLDecoder 예제

22 6.1 URLEncoder, URLDecoder class란?
6. URLEncoder, Decoder 6.1 URLEncoder, URLDecoder class란? URLEncoder클래스 데이터를 웹서버에서 요구하는 데이터 형식으로 바꾸어 줌 (x-www-form-urlencoded 형식)‏ 변환규칙 아스키문자(a~z, A~Z, 1~9), ‘,’, ‘-’, ‘*’, ‘_’ : 그대로 전달 공백문자 : ‘+’기호로 전환 기타문자 : ‘%xy’ 같은 형식으로 변환(xy: ASCII코드를 16진수화한 결과)‏ 하나의 static 멤버 메서드를 갖음 public static String encode(String s, String enc)‏ URLDecoder클래스 X-www-form-urlencoded 형식으로 인코딩된 데이터를 디코딩 하나의 static 멤버메서드를 갖음 Public static String decode(String d, String dec)‏ 10/04/07

23 6.2 URLEncoder, URLDecoder 예제
6. URLEncoder, Decoder 6.2 URLEncoder, URLDecoder 예제 문자열을 인코딩하고 디코딩하는 예제 URLTest.java import java.net.*; public class URLEnDecoderTest{ public static void main(String [] args) { String str, encodedStr, decodedStr; str=“자바스킬업 – encodedStr = URLEncoder.encode(str, “KSC5601”); decodedStr = URLDecoder.decode(encodedStr, “KSC5601”); System.out.println("str : " + str); System.out.println("encodedStr : " + encodedStr); System.out.println("decodedStr : " + decodedStr); } 10/04/07

24 7. URLClassLoader class 7.1 URLClassLoader class 란?

25 7.1 URLClassLoader란? URL을 통해 JAR파일 및 디렉터리의 자원 및 클래스파일 로드
ClassLoader클래스를 상속하여 loadClass()메서드 사용 URLClassLoader생성자 메서드 public URLClassLoader(URL[] urls)‏ ClassLoader를 사용해 지정된 새로운 URLClassLoader생성 URL[] urlArray = {new File(“C:/myweb/network/src").toURL()}; URLClassLoader ucl = new URLClassLoader(urlArray); Class c =ucl.loadClass("Hello"); Hello hello =(Hello)c.newInstance(); 10/04/07

26 7.2 URLClassLoader 활용 URLClassLoader를 이용하여 디렉터리에서 바이트코드 로딩하는 예제
URLClassLoaderTest.java import java.net.*; import java.io.*; public class URLClassLoaderTest{ public static void main(String args[]) throws Exception { URL[] urlArray = {new File(“C:/myweb/network/").toURL()}; URLClassLoader ucl = new URLClassLoader(urlArray); Class c =ucl.loadClass("Hello"); Hello hello =(Hello)c.newInstance(); hello.sayHello("홍길동"); } class Hello{ public Hello(){ public void sayHello(String name){ System.out.println(name + "씨 안녕하세요"); C:\java\16>javac URLClassLoaderTest.java C:\java\16>java URLClassLoaderTest 홍길동씨 안녕하세요 10/04/07

27 8. 마무리 자바는 네트워크 그 자체의 언어 자바네트워크는 URL관련 정보를 쉽게 제어할 수있는 방법 제공
InetAddress클래스로 쉽게 호스트의 IP제어 URL류 클래스로 URL자원에 대한 손쉬운 접근 10/04/07


Download ppt "Network."

Similar presentations


Ads by Google