반이중 실습
Jan 10, 2024
![반이중 실습](https://inblog.ai/_next/image?url=https%3A%2F%2Finblog.ai%2Fapi%2Fog%3Ftitle%3D%25EB%25B0%2598%25EC%259D%25B4%25EC%25A4%2591%2520%25EC%258B%25A4%25EC%258A%25B5%26logoUrl%3Dhttps%253A%252F%252Finblog.ai%252Finblog_logo.png%26blogTitle%3Dkhh&w=1920&q=75)
양방향 전송이 가능하지만 동시에 양쪽 방향에서 전송할 수 없는 방식
ex) 무전기
서버
package ex17.half; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.nio.charset.Charset; public class Server { public static void main(String[] args) { try { ServerSocket serverSocket = new ServerSocket(20000); // 서버소켓 Socket socket = serverSocket.accept(); // 연결 // 소켓 연결 완료됨 // 버퍼 만들기 (recevied) BufferedReader br = new BufferedReader( new InputStreamReader(socket.getInputStream()) // 문자로 읽기 ); String requestMsg = br.readLine(); System.out.println("클라이언트로부터 받은 메시지 : " + requestMsg); // 버퍼 만들기 (send) 동기적 실행 PrintWriter pw = new PrintWriter(socket.getOutputStream(), true, Charset.forName("UTF-8")); if (requestMsg.equals("1")) { pw.println("영화"); } else if (requestMsg.equals("2")) { pw.println("드라마"); } else { pw.println("프로토콜을 확인하세요 : 1은 영화, 2는 드라마"); } } catch (IOException e) { throw new RuntimeException(e); } } }
클라이언트
package ex17.half; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.nio.charset.Charset; public class Server { public static void main(String[] args) { try { ServerSocket serverSocket = new ServerSocket(20000); // 서버소켓 Socket socket = serverSocket.accept(); // 연결 // 소켓 연결 완료됨 // 버퍼 만들기 (recevied) BufferedReader br = new BufferedReader( new InputStreamReader(socket.getInputStream()) ); String requestMsg = br.readLine(); System.out.println("클라이언트로부터 받은 메시지 : " + requestMsg); // 버퍼 만들기 (send) 동기적 실행 PrintWriter pw = new PrintWriter(socket.getOutputStream(), true, Charset.forName("UTF-8")); if (requestMsg.equals("1")) { pw.println("영화"); } else if (requestMsg.equals("2")) { pw.println("드라마"); } else { pw.println("프로토콜을 확인하세요 : 1은 영화, 2는 드라마"); } } catch (IOException e) { throw new RuntimeException(e); } } }
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F190e681e-8a6a-4223-a5b4-4d781100c280%2F9f3176b0-f409-4fd6-b221-c0927523cb77%2FScreenshot_4.png?table=block&id=aadfcc6a-b266-40b0-8aba-7167df6d291a&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F190e681e-8a6a-4223-a5b4-4d781100c280%2F82a47ebc-5624-4aee-84ee-1fbaaff9998b%2FScreenshot_1.png?table=block&id=98223172-3530-4db1-ac0d-318693760295&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F190e681e-8a6a-4223-a5b4-4d781100c280%2F1cc72880-2fd1-4d2b-ba95-db6ccefbb111%2FScreenshot_2.png?table=block&id=c3a7b86a-aab2-4417-bcd1-760fc93fb9e4&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F190e681e-8a6a-4223-a5b4-4d781100c280%2F4759ea59-fdce-4b4b-8b5d-ca186671b0ef%2FScreenshot_3.png?table=block&id=0684628f-4b33-47c9-adac-26493c9d4a0c&cache=v2)
Share article