Socket
private static void M1() throws Exception { Thread serverThread = new Thread() { @Override public void run() { try { ServerSocket server = new ServerSocket(8888); System.out.println("【服务端】监听 8888 等待"); Socket s = server.accept(); System.out.println("【服务端】收到 socket 端口号:" + s.getPort()); InputStream is = s.getInputStream(); DataInputStream dis = new DataInputStream(is); while (true) { String msg = dis.readUTF(); System.out.println("【服务端】收到消息 " + msg); } } catch (Exception e) { e.printStackTrace(); } } }; serverThread.start(); ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 15, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>()); executor.execute(new Runnable() { @Override public void run() { try { Thread.sleep(1000); Socket socket = new Socket("127.0.0.1", 8888); OutputStream os = socket.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); Scanner scanner = new Scanner(System.in); while (true) { dos.writeUTF(scanner.nextLine()); } } catch (Exception e) { e.printStackTrace(); } } }); }