package com.yjf.esupplier.common.test; import java.io.*; import java.net.Socket; /** * @author shusheng * @description TCP 传输数据:键盘录入 * @Email [email protected] * @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 [email protected] * @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(); } }
原文地址:https://www.cnblogs.com/zuixinxian/p/11275398.html
时间: 2024-12-28 20:58:39