JavaSE-21.3.3【TCP通信程序练习2】

 1 package day12.lesson3.p2;
 2 
 3 import java.io.*;
 4 import java.net.Socket;
 5 
 6 /*
 7 3.4 TCP通信程序练习2
 8 
 9     案例需求
10         客户端:数据来自于键盘录入, 直到输入的数据是886,发送数据结束
11         服务端:接收到数据在控制台输出
12     案例分析
13         客户端创建对象,使用键盘录入循环接受数据,接受一行发送一行,直到键盘录入886为止
14         服务端创建对象,使用输入流按行循环接受数据,直到接受到null为止
15  */
16 public class ClientDemo {
17     public static void main(String[] args) throws IOException {
18         Socket socket = new Socket("MSI-YUBABY", 10000);
19 
20         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
21         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
22 
23         String line;
24         while ((line=br.readLine()) != null){
25             if("886".equals(line)){
26                 break;
27             }
28 
29             bw.write(line);
30             bw.newLine();
31             bw.flush();
32         }
33 
34         socket.close();
35     }
36 }
 1 package day12.lesson3.p2;
 2 
 3 import javax.net.ssl.SSLServerSocket;
 4 import java.io.BufferedReader;
 5 import java.io.IOException;
 6 import java.io.InputStream;
 7 import java.io.InputStreamReader;
 8 import java.net.ServerSocket;
 9 import java.net.Socket;
10 
11 public class ServerDemo {
12     public static void main(String[] args) throws IOException {
13         ServerSocket ss = new ServerSocket(10000);
14 
15         Socket socket = ss.accept();
16 
17         /*InputStream is = socket.getInputStream();
18         InputStreamReader isr = new InputStreamReader(is);
19         BufferedReader br = new BufferedReader(isr);*/
20         BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
21         String line;
22         while ((line=br.readLine()) != null){
23             System.out.println(line);
24         }
25 
26         ss.close();
27     }
28 }

 

posted @ 2021-06-11 09:56  yub4by  阅读(38)  评论(0编辑  收藏  举报