TCP通信程序练习(客户端数据来自于键盘录入)

package NetworkProgramming.TCP.SocketDemo02;

import java.io.*;
import java.net.Socket;

/**
* FileName: CilentDemo
* Author: lps
* Date: 2022/4/2 11:21
* Sign:刘品水 Q:1944900433
*/
public class CilentDemo {
public static void main(String[] args) throws IOException {
Socket s = new Socket("192.168.10.102", 1944);
//OutputStream os = s.getOutputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//封装输出流对象
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while ((line = br.readLine()) != null) {
if ("886".equals(line)) {
break;
}
//os.write(line.getBytes(StandardCharsets.UTF_8));
bw.write(line);
bw.newLine();
bw.flush();
}
//释放资源
s.close();
}

}


package NetworkProgramming.TCP.SocketDemo02;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
* FileName: ServerDemo
* Author: lps
* Date: 2022/4/2 11:22
* Sign:刘品水 Q:1944900433
*/
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(1944);
while (true) {
//Socket accept()
//监听要对这个套接字作出的连接并接受它。
Socket s = ss.accept();
//InputStream is = s.getInputStream();
//InputStreamReader isr = new InputStreamReader(is);
//BufferedReader br = new BufferedReader(new InputStreamReader(isr));
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
System.out.println("接受的数据为:"+line);
}
ss.close();
}
}
}

 

 

 

 




posted @ 2022-04-02 12:10  刘品水  阅读(43)  评论(0编辑  收藏  举报