服务端&#xff1a; package cn.itwoody.net; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; public class MyServer { public static void main(String args[]) { ServerSocket server; Socket socket; String s; InputStream Is; OutputStream Os; PrintStream PS; try { // 在端口4321注册服务 server &#61; new ServerSocket(4321); socket &#61; server.accept();// 监听窗口,等待连接 System.out.println("server ok"); System.out.println("************************************************"); // 获得对应Socket的输入/输出流 Is &#61; socket.getInputStream(); Os &#61; socket.getOutputStream(); // 建立数据流 BufferedReader clientInput&#61;new BufferedReader(new InputStreamReader(Is)); PS &#61; new PrintStream(Os); //DataInputStream in &#61; new DataInputStream(System.in); BufferedReader serverInput&#61;new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.println(""); System.out.println("please wait client&#39;s message..."); System.out.println(""); s &#61; clientInput.readLine(); // 读入从client传来的字符串 System.out.println("Client Say:" &#43; s); // 打印字符串 if (s.trim().equals("Bye")) break; // 如果是"BYE",就退出 System.out.print("You Say:"); s &#61; serverInput.readLine(); // 读取用户输入的字符串 PS.println(s); // 将读取得字符串传给client if (s.trim().equals("Bye")) break; // 如果是"BYE",就退出 } // 关闭连接 //DIS.close(); // 关闭数据输入流 PS.close(); // 关闭数据输出流 Is.close(); // 关闭输入流 Os.close(); // 关闭输出流 socket.close(); // 关闭sockey } catch (Exception e) { System.out.println("Error:" &#43; e); } } } 客服端&#xff1a; package cn.itwoody.net; import java.net.*; import java.io.*; public class MyClient { public static void main(String args[]) { if (args.length <1) { // 判断命令加参数没有 System.out.println("You forget the name of the server!"); System.exit(1); // 如果没加参数就退出 } Socket socket; String s &#61; "yxfsoft&#64;263.net"; InputStream Is; OutputStream Os; PrintStream PS; try { // 向主机名为args[0]的服务器申请连接 // 注意端口号要与服务器保持一致:4321 socket &#61; new Socket(args[0], 4321); System.out.println("Client ok"); System.out.println("************************************************"); // 获得对应socket的输入/输出流 Is &#61; socket.getInputStream(); Os &#61; socket.getOutputStream(); // 建立数据流 //serverInput &#61; new DataInputStream(Is); BufferedReader serverInput&#61;new BufferedReader(new InputStreamReader(Is)); PS &#61; new PrintStream(Os); //DataInputStream clientInput &#61; new DataInputStream(System.in); BufferedReader clientInput&#61;new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.print("You Say:"); s &#61; clientInput.readLine(); // 读取用户输入的字符串 PS.println(s); // 将读取得字符串传给server if (s.trim().equals("Bye")) break; // 如果是"BYE",就退出 else { System.out.println(""); System.out.println("please wait server&#39;s message..."); System.out.println(""); } s &#61; serverInput.readLine(); // 从服务器获得字符串 System.out.println("Server Say:" &#43; s); // 打印字符串 if (s.trim().equals("Bye")) break; // 如果是"BYE",就退出 } // 关闭连接 serverInput.close(); // 关闭数据输入流 PS.close(); // 关闭数据输出流 Is.close(); // 关闭输入流 Os.close(); // 关闭输出流 socket.close(); // 关闭socket } catch (Exception e) { System.out.println("Error:" &#43; e); } } }