하고재비
[JAVA]TCP/IP 본문
TCP/IP 프로토콜
TCP :
인터넷에서 사용되는 프로토콜
두 시스템간 신뢰성있는 데이터 전송을 관장
IP :
Internet Protocol
패킷교환 네크워크에서 송신호스트와 수신 호스트가 주고받는것을 관장
TCP 보다 하위레벨
ip주소는 네크워크상 컴퓨터 또는 시스템을 식별하는 주소
포트 :
통신하는 프로그램간 가상의 출구
포트번호를 이용하여 통신할 응용프로그램 식별
1. 클라이언트 프로그램 : 연결을 요청하는 프로그램
2. 서버 프로그램 : 연결요청을 기다리는 프로그램
소켓 :
두 프로그램 간 양방향 통신 링크의 끝단
소켓끼리 데이터 주고받음
서버소켓 :
서버프로그램에서만 사용되는 소켓
연결요청을 기다리다가, 연결요청이 오면 연결을 맺고 또다른 소켓 생성
클라이언트 소켓 :
클라이언트 프로그램과 서버 프로그램에서 모두 다 사용되는 소켓
실제 데이터 이동에 사용
서버프로그램에서는 서버소켓에 의해 생성됨
클라이언트 프로그램에서는 직접 생성해야함
Client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | public class Client_Ex { public static void main(String args[]) { BufferedReader in = null; BufferedWriter out = null; Socket socket = null; Scanner sc = new Scanner(System.in); try { socket = new Socket("localhost", 9999); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); while (true) { System.out.println("보내기 >> "); String outputmessage = sc.nextLine(); if (outputmessage.equals("bye")) { out.write(outputmessage + "\n"); out.flush(); break; // bye일 경우 서버로 전송하고 실행종료 } out.write(outputmessage + "\n"); out.flush(); String inputmessage = in.readLine(); // 서버에서 받은 메세지 System.out.println("서버 : " + inputmessage); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { sc.close(); if (socket == null) socket.close(); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("서버와 채팅중 오류 발생"); e.printStackTrace(); } } } } | cs |
Server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | public class Server_Ex { public static void main(String args[]) { BufferedReader in = null; BufferedWriter out = null; ServerSocket listsocket = null; Socket socket = null; Scanner scanner = new Scanner(System.in); try { listsocket = new ServerSocket(9999); System.out.println("연결 기다리는 중"); socket = listsocket.accept(); // 클라이언트로부터 연결 요청 대기상채 System.out.println("연결 되었습니다."); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); while (true) { String inputMessage = in.readLine(); if (inputMessage.equalsIgnoreCase("bye")) { System.out.println("클라이언트에서 bye message로 연결 종료하였음."); break; } System.out.println("클라이언트 : " + inputMessage); System.out.println("보내기 >> "); String outputmessage = scanner.nextLine(); out.write(outputmessage + "\n"); out.flush(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { scanner.close(); socket.close(); listsocket.close(); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("클라이언트와 채팅중 오류 발생!"); e.printStackTrace(); } } } } | cs |
'JAVA' 카테고리의 다른 글
[JAVA] getter, setter (0) | 2018.07.20 |
---|---|
[JAVA] xml생성 (0) | 2018.04.19 |
[자료구조] 연결리스트 (0) | 2017.12.18 |
[JAVA]ArrayList (0) | 2017.12.13 |
[JAVA]Comparable , Comparator (0) | 2017.12.13 |
Comments