문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 다음 판 | 이전 판 | ||
|
프로그램:java:ㄴhomework:book_source:ch19 [2022/01/04 11:32] clayeryan@gmail.com 만듦 |
프로그램:java:ㄴhomework:book_source:ch19 [2025/06/27 16:07] (현재) |
||
|---|---|---|---|
| 줄 1: | 줄 1: | ||
| - | ====== | + | ====== |
| + | =====네트워크 개요===== | ||
| + | |||
| + | =====네트워크 관련 클래스 ===== | ||
| + | <code java> | ||
| + | package chapter19; | ||
| + | |||
| + | import java.net.InetAddress; | ||
| + | import java.net.UnknownHostException; | ||
| + | |||
| + | public class InetAddressEx { | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | |||
| + | try { | ||
| + | // getByName메서드로 InetAddress 객체 생성 | ||
| + | InetAddress ip = InetAddress.getByName(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | |||
| + | // getAllByName메서드로 InetAddress 객체배열 생성 | ||
| + | InetAddress[] ips = | ||
| + | InetAddress.getAllByName(" | ||
| + | for (InetAddress i : ips) { | ||
| + | System.out.println(" | ||
| + | } | ||
| + | |||
| + | // ip 주소값을 byte[] 배열로 리턴 | ||
| + | byte[] ipAddr = ip.getAddress(); | ||
| + | // byte 자료형의 표현 범위 : 128 ~ 127 | ||
| + | // 127 이상의 값은 음수로 표현됨 | ||
| + | for (byte b : ipAddr) { | ||
| + | System.out.print(((b < 0) ? b + 256 : b) + " | ||
| + | } | ||
| + | System.out.println(); | ||
| + | |||
| + | // getLocalHost 메서드로 InetAddress 객체 생성 호출 | ||
| + | InetAddress local = InetAddress.getLocalHost(); | ||
| + | System.out.println(" | ||
| + | |||
| + | // getByAddress 메서드로 InetAddress 객체 생성 호출 | ||
| + | InetAddress ip2 = InetAddress.getByAddress(ips[0].getAddress()); | ||
| + | System.out.println(ips[0].getHostAddress() + " 주소 :" + ip2); | ||
| + | |||
| + | } catch (UnknownHostException e) { | ||
| + | System.out.println(e.getMessage()); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code java> | ||
| + | package chapter19; | ||
| + | |||
| + | import java.io.IOException; | ||
| + | import java.io.InputStreamReader; | ||
| + | import java.io.Reader; | ||
| + | import java.net.MalformedURLException; | ||
| + | import java.net.URL; | ||
| + | |||
| + | public class URLEx { | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | |||
| + | try { | ||
| + | URL url = null; | ||
| + | url = new URL(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out .println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | // URL 을 통해서 정보 받기 | ||
| + | int data = 0; | ||
| + | try { | ||
| + | Reader is = new InputStreamReader(url.openStream()); | ||
| + | while ((data = is.read()) != -1) { | ||
| + | System.out.print((char) data); | ||
| + | } | ||
| + | System.out.println(); | ||
| + | } catch (IOException e) { | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| + | |||
| + | } catch (Exception e) { | ||
| + | System.out.println(e.getMessage()); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | <code java> | ||
| + | package chapter19; | ||
| + | |||
| + | import java.net.URL; | ||
| + | import java.net.URLConnection; | ||
| + | |||
| + | public class URLConnectionEx { | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | |||
| + | URL url = null; | ||
| + | String address = | ||
| + | " | ||
| + | try { | ||
| + | url = new URL(address); | ||
| + | URLConnection conn = url.openConnection(); | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | conn.getDefaultAllowUserInteraction()); | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | |||
| + | |||
| + | } catch (Exception e) { | ||
| + | System.out.println(e.getMessage()); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | <code java> | ||
| + | package chapter19; | ||
| + | |||
| + | import java.io.BufferedReader; | ||
| + | import java.io.InputStreamReader; | ||
| + | import java.net.URL; | ||
| + | |||
| + | public class URLConnectionEx2 { | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | |||
| + | URL url = null; | ||
| + | String address = | ||
| + | " | ||
| + | BufferedReader br = null; | ||
| + | String readline = ""; | ||
| + | |||
| + | try { | ||
| + | url = new URL(address); | ||
| + | br = new BufferedReader( | ||
| + | new InputStreamReader(url.openStream())); | ||
| + | |||
| + | while ((readline = br.readLine()) != null) { | ||
| + | System.out.println(readline); | ||
| + | } | ||
| + | |||
| + | |||
| + | } catch (Exception e) { | ||
| + | System.out.println(e.getMessage()); | ||
| + | } finally { | ||
| + | try { br.close(); }catch(Exception e) {} | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | </ | ||
| + | =====TCP 소켓 프로그래밍===== | ||
| + | <code java> | ||
| + | package chapter19; | ||
| + | |||
| + | import java.io.BufferedReader; | ||
| + | import java.io.DataOutputStream; | ||
| + | import java.io.FileReader; | ||
| + | import java.io.IOException; | ||
| + | import java.io.InputStreamReader; | ||
| + | import java.io.PrintWriter; | ||
| + | import java.net.ServerSocket; | ||
| + | import java.net.Socket; | ||
| + | |||
| + | public class ServerEx { | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | |||
| + | // 소켓 생성 | ||
| + | ServerSocket server = null; | ||
| + | |||
| + | try { | ||
| + | |||
| + | server = new ServerSocket(9999); | ||
| + | |||
| + | // 무한 반복 (클라이언트 접속 대기) | ||
| + | while(true) { | ||
| + | System.out.println(" | ||
| + | Socket client = server.accept(); | ||
| + | System.out.println(" | ||
| + | HttpThread ht = new HttpThread(client); | ||
| + | ht.start(); | ||
| + | } | ||
| + | }catch (Exception e) { | ||
| + | System.out.println(e.getMessage()); | ||
| + | } finally { | ||
| + | try { | ||
| + | server.close(); | ||
| + | } catch (Exception e) { | ||
| + | System.out.println(e.getMessage()); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | class HttpThread extends Thread { | ||
| + | private Socket client; | ||
| + | BufferedReader br; | ||
| + | PrintWriter pw; | ||
| + | HttpThread(Socket client) { | ||
| + | this.client = client; | ||
| + | try { | ||
| + | br = new BufferedReader (new InputStreamReader(client.getInputStream())); | ||
| + | pw = new PrintWriter(client.getOutputStream()); | ||
| + | } catch(IOException e) { | ||
| + | System.out.println(e.getMessage()); | ||
| + | } | ||
| + | } | ||
| + | public void run() { | ||
| + | BufferedReader fbr = null; | ||
| + | DataOutputStream outToClient = null; | ||
| + | try { | ||
| + | String line = br.readLine(); | ||
| + | //line : GET / HTTP/1.1 | ||
| + | System.out.println(" | ||
| + | int start = line.indexOf("/" | ||
| + | int end = line.lastIndexOf(" | ||
| + | String fileName=line.substring(start, | ||
| + | if(fileName.equals("" | ||
| + | fileName=" | ||
| + | } | ||
| + | System.out.println(" | ||
| + | fbr = new BufferedReader (new FileReader(fileName)); | ||
| + | String fileLine = null; | ||
| + | pw.println(" | ||
| + | while((fileLine = fbr.readLine())!=null){ | ||
| + | pw.println(fileLine); | ||
| + | pw.flush(); | ||
| + | } | ||
| + | } catch(IOException e) { | ||
| + | System.out.println(e.getMessage()); | ||
| + | } finally { | ||
| + | try { | ||
| + | if(br != null) br.close(); | ||
| + | if(pw != null) pw.close(); | ||
| + | if(client != null) client.close(); | ||
| + | } catch(IOException e) { | ||
| + | System.out.println(e.getMessage()); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | =====UDP 소켓 프로그래밍===== | ||
| + | <code java> | ||
| + | package chapter19; | ||
| + | |||
| + | import java.io.BufferedReader; | ||
| + | import java.io.DataOutputStream; | ||
| + | import java.io.FileReader; | ||
| + | import java.io.IOException; | ||
| + | import java.io.InputStreamReader; | ||
| + | import java.io.PrintWriter; | ||
| + | import java.net.ServerSocket; | ||
| + | import java.net.Socket; | ||
| + | |||
| + | public class ServerEx { | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | |||
| + | // 소켓 생성 | ||
| + | ServerSocket server = null; | ||
| + | |||
| + | try { | ||
| + | |||
| + | server = new ServerSocket(9999); | ||
| + | |||
| + | // 무한 반복 (클라이언트 접속 대기) | ||
| + | while(true) { | ||
| + | System.out.println(" | ||
| + | Socket client = server.accept(); | ||
| + | System.out.println(" | ||
| + | HttpThread ht = new HttpThread(client); | ||
| + | ht.start(); | ||
| + | } | ||
| + | }catch (Exception e) { | ||
| + | System.out.println(e.getMessage()); | ||
| + | } finally { | ||
| + | try { | ||
| + | server.close(); | ||
| + | } catch (Exception e) { | ||
| + | System.out.println(e.getMessage()); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | class HttpThread extends Thread { | ||
| + | private Socket client; | ||
| + | BufferedReader br; | ||
| + | PrintWriter pw; | ||
| + | HttpThread(Socket client) { | ||
| + | this.client = client; | ||
| + | try { | ||
| + | br = new BufferedReader (new InputStreamReader(client.getInputStream())); | ||
| + | pw = new PrintWriter(client.getOutputStream()); | ||
| + | } catch(IOException e) { | ||
| + | System.out.println(e.getMessage()); | ||
| + | } | ||
| + | } | ||
| + | public void run() { | ||
| + | BufferedReader fbr = null; | ||
| + | DataOutputStream outToClient = null; | ||
| + | try { | ||
| + | String line = br.readLine(); | ||
| + | //line : GET / HTTP/1.1 | ||
| + | System.out.println(" | ||
| + | int start = line.indexOf("/" | ||
| + | int end = line.lastIndexOf(" | ||
| + | String fileName=line.substring(start, | ||
| + | if(fileName.equals("" | ||
| + | fileName=" | ||
| + | } | ||
| + | System.out.println(" | ||
| + | fbr = new BufferedReader (new FileReader(fileName)); | ||
| + | String fileLine = null; | ||
| + | pw.println(" | ||
| + | while((fileLine = fbr.readLine())!=null){ | ||
| + | pw.println(fileLine); | ||
| + | pw.flush(); | ||
| + | } | ||
| + | } catch(IOException e) { | ||
| + | System.out.println(e.getMessage()); | ||
| + | } finally { | ||
| + | try { | ||
| + | if(br != null) br.close(); | ||
| + | if(pw != null) pw.close(); | ||
| + | if(client != null) client.close(); | ||
| + | } catch(IOException e) { | ||
| + | System.out.println(e.getMessage()); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code java> | ||
| + | package chapter19; | ||
| + | |||
| + | import java.io.BufferedReader; | ||
| + | import java.io.DataOutputStream; | ||
| + | import java.io.FileReader; | ||
| + | import java.io.IOException; | ||
| + | import java.io.InputStreamReader; | ||
| + | import java.io.PrintWriter; | ||
| + | import java.net.ServerSocket; | ||
| + | import java.net.Socket; | ||
| + | |||
| + | public class ServerEx { | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | |||
| + | // 소켓 생성 | ||
| + | ServerSocket server = null; | ||
| + | |||
| + | try { | ||
| + | |||
| + | server = new ServerSocket(9999); | ||
| + | |||
| + | // 무한 반복 (클라이언트 접속 대기) | ||
| + | while(true) { | ||
| + | System.out.println(" | ||
| + | Socket client = server.accept(); | ||
| + | System.out.println(" | ||
| + | HttpThread ht = new HttpThread(client); | ||
| + | ht.start(); | ||
| + | } | ||
| + | }catch (Exception e) { | ||
| + | System.out.println(e.getMessage()); | ||
| + | } finally { | ||
| + | try { | ||
| + | server.close(); | ||
| + | } catch (Exception e) { | ||
| + | System.out.println(e.getMessage()); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | class HttpThread extends Thread { | ||
| + | private Socket client; | ||
| + | BufferedReader br; | ||
| + | PrintWriter pw; | ||
| + | HttpThread(Socket client) { | ||
| + | this.client = client; | ||
| + | try { | ||
| + | br = new BufferedReader (new InputStreamReader(client.getInputStream())); | ||
| + | pw = new PrintWriter(client.getOutputStream()); | ||
| + | } catch(IOException e) { | ||
| + | System.out.println(e.getMessage()); | ||
| + | } | ||
| + | } | ||
| + | public void run() { | ||
| + | BufferedReader fbr = null; | ||
| + | DataOutputStream outToClient = null; | ||
| + | try { | ||
| + | String line = br.readLine(); | ||
| + | //line : GET / HTTP/1.1 | ||
| + | System.out.println(" | ||
| + | int start = line.indexOf("/" | ||
| + | int end = line.lastIndexOf(" | ||
| + | String fileName=line.substring(start, | ||
| + | if(fileName.equals("" | ||
| + | fileName=" | ||
| + | } | ||
| + | System.out.println(" | ||
| + | fbr = new BufferedReader (new FileReader(fileName)); | ||
| + | String fileLine = null; | ||
| + | pw.println(" | ||
| + | while((fileLine = fbr.readLine())!=null){ | ||
| + | pw.println(fileLine); | ||
| + | pw.flush(); | ||
| + | } | ||
| + | } catch(IOException e) { | ||
| + | System.out.println(e.getMessage()); | ||
| + | } finally { | ||
| + | try { | ||
| + | if(br != null) br.close(); | ||
| + | if(pw != null) pw.close(); | ||
| + | if(client != null) client.close(); | ||
| + | } catch(IOException e) { | ||
| + | System.out.println(e.getMessage()); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
별도로 명시하지 않을 경우, 이 위키의 내용은 다음 라이선스에 따라 사용할 수 있습니다: CC Attribution-Share Alike 4.0 International