package com.yjf.esupplier.common.test;import java.io.*; import java.net.Socket;/*** @author shusheng* @description TCP 传输数据:键盘录入* @Email shusheng@yiji.com* @date 2019/1/15 22:57*/ public class ClientDemo1 {public static void main(String[] args) throws IOException {// 创建发送端的Socket对象Socket socket = new Socket("localhost", 8888);BufferedReader br = new BufferedReader(new InputStreamReader(System.in));BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));String line = null;while ((line = br.readLine()) != null) {// 键盘录入数据要自定义结束标记if ("886".equals(line)) {break;}bw.write(line);bw.newLine();bw.flush();}socket.close();}}
package com.yjf.esupplier.common.test;import java.io.*; import java.net.ServerSocket; import java.net.Socket;/*** @author shusheng* @description TCP 传输数据:键盘录入* @Email shusheng@yiji.com* @date 2019/1/16 21:11*/ public class ServerDemo1 {public static void main(String[] args) throws IOException {// 创建接收端的 Socket 对象ServerSocket ss = new ServerSocket(8888);Socket s = ss.accept();BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));String line = null;while((line=br.readLine())!=null){System.out.println(line);}s.close();}}