1.ServerThread package serverclient; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public class ServerThread extends Thread { Socket socket = null; //保存与本线程相关的Socket对象 int clientnum; //保存本进程的客户计数 public ServerThread(Socket socket, int num) { //构造函数 this.socket = socket; //初始化socket变量 clientnum = num + 1; //初始化clientnum变量 } public void run() { //线程主体 try { String line; BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream())); //由Socket对象得到输入流,并构造相应的BufferedReader对象 PrintWriter os = new PrintWriter(socket.getOutputStream()); //由Socket对象得到输出流,并构造PrintWriter对象 BufferedReader sin = new BufferedReader(new InputStreamReader(System.in)); //由系统标准输入设备构造BufferedReader对象 System.out.println("Client:" + clientnum + is.readLine()); //在标准输出上打印从客户端读入的字符串 line = sin.readLine(); //从标准输入读入一字符串 while (!line.equals("bye")) { //如果该字符串为 "bye",则停止循环 os.println(line); //向客户端输出该字符串 os.flush(); //刷新输出流,使Client马上收到该字符串 System.out.println("Server:" + line); //在系统标准输出上打印该字符串 System.out.println("Client:" + clientnum + is.readLine()); //从Client读入一字符串,并打印到标准输出上 line = sin.readLine(); //从系统标准输入读入一字符串 } //继续循环 os.close(); //关闭Socket输出流 is.close(); //关闭Socket输入流 socket.close(); //关闭Socket } catch (Exception e) { System.out.println("Error:" + e); //出错,打印出错信息 } } } 2.MultiTalkClient /** * Created by hoobey on 2017/9/13. */ package serverclient; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public class MultiTalkClient { public static void main(String args[]) { try { Socket socket = new Socket("127.0.0.1", 4700); //向本机的4700端口发出客户请求 BufferedReader sin = new BufferedReader(new InputStreamReader(System.in)); //由系统标准输入设备构造BufferedReader对象 PrintWriter os = new PrintWriter(socket.getOutputStream()); //由Socket对象得到输出流,并构造PrintWriter对象 BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream())); //由Socket对象得到输入流,并构造相应的BufferedReader对象 String readline; readline = sin.readLine(); //从系统标准输入读入一字符串 while (!readline.equals("bye")) { //若从标准输入读入的字符串为 "bye"则停止循环 os.println(readline); //将从系统标准输入读入的字符串输出到Server os.flush(); //刷新输出流,使Server马上收到该字符串 System.out.println("Client:" + readline); //在系统标准输出上打印读入的字符串 System.out.println("Server:" + is.readLine()); //从Server读入一字符串,并打印到标准输出上 readline = sin.readLine(); //从系统标准输入读入一字符串 } //继续循环 os.close(); //关闭Socket输出流 is.close(); //关闭Socket输入流 socket.close(); //关闭Socket } catch (Exception e) { System.out.println("Error" + e); //出错,则打印出错信息 } } } 3.MultiTalkServer package serverclient; import java.io.*; import java.net.*; public class MultiTalkServer { static int clientnum = 0; // 静态成员变量,记录当前客户的个数 public static void main(String args[]) throws IOException { ServerSocket serverSocket = null; boolean listening = true; try { serverSocket = new ServerSocket(4700); // 创建一个ServerSocket在端口4700监听客户请求 } catch (IOException e) { System.out.println("Could not listen on port:4700."); // 出错,打印出错信息 System.exit(-1); // 退出 } while (listening) { // 永远循环监听 new ServerThread(serverSocket.accept(), clientnum).start(); // 监听到客户请求,根据得到的Socket对象和 // 客户计数创建服务线程,并启动之 clientnum++; // 增加客户计数 } serverSocket.close(); // 关闭ServerSocket } }