实现 一台服务器对多个客户机的响应 服务器: import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; public class TcpServer { public static void main(String[] args) throws Exception { if (args.length == 0) { System.out.println("未输入端口号,格式:java TcoServer 端口号"); return; } System.out.println("服务器已开启,等待客户机响应。。。"); ServerSocket serverSocket = new ServerSocket(Integer.parseInt(args[0])); while (true) { Socket socket = serverSocket.accept(); System.out.println(socket.getInetAddress().getHostAddress() + ":" + socket.getPort() + " 已连接!"); new Thread(new Send(socket)) { }.start(); } } } // 发送数据 class Send implements Runnable { public Send(Socket socket) { this.socket = socket; } private Socket socket; private InputStream in; private OutputStream out; private BufferedReader br; private BufferedWriter bw; public void run() { try { in = socket.getInputStream(); out = socket.getOutputStream(); br = new BufferedReader(new InputStreamReader(in)); bw = new BufferedWriter(new OutputStreamWriter(out)); while (true) { String msg = br.readLine(); if ("exit".equals(msg)) { System.out.println(socket.getInetAddress().getHostAddress() + ":" + socket.getPort() + "已断开!"); break; } StringBuffer sb = new StringBuffer(msg); msg = sb.reverse().toString(); bw.write("服务器响应:-->" + msg + "/r/n"); bw.flush(); } } catch (Exception e) { e.printStackTrace(); } try { br.close(); bw.close(); socket.close(); } catch (Exception e) { e.printStackTrace(); } } } 客户端: import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.Socket; public class TcpClient { public static void main(String[] args) throws Exception { if (args.length < 2) { System.out.println("请输入端口号以及IP地址,格式:java TcoClient IP 端口号"); return; } Socket socket = new Socket(args[0], Integer.parseInt(args[1])); InputStream in = socket.getInputStream(); OutputStream out = socket.getOutputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out)); BufferedReader console = new BufferedReader(new InputStreamReader( System.in)); while (true) { System.out.println("请输入:"); String msg = console.readLine(); bw.write(msg + "/r/n"); bw.flush(); if ("exit".equalsIgnoreCase(msg)) break; System.out.println(br.readLine() + "/n"); } bw.close(); br.close(); socket.close(); } } 运行效果: