java socket实现多客户端群聊

fmms 13年前

服务器端主要是接收客户端发送过来的信息,并将该信息发给所有的客户端

import java.io.BufferedReader;  import java.io.IOException;  import java.io.InputStreamReader;  import java.io.PrintWriter;  import java.net.ServerSocket;  import java.net.Socket;  import java.util.ArrayList;  import java.util.List;    /**   *     */  class Chat extends Thread {   private Socket socket;   private List<Socket> socketList;   private int count;     public Chat(int count, Socket socket, List<Socket> socketList) {    this.count = count;    this.socket = socket;    this.socketList = socketList;   }     public void run() {    BufferedReader reader = null;    PrintWriter writer = null;    try {     reader = new BufferedReader(new InputStreamReader(       socket.getInputStream()));       String message = null;     while (true) {      message = reader.readLine();      // 接收到客户端的bye信息,客户端即将退出,并将bye写入到该客户端      if (message.equals("bye")) {       writer = new PrintWriter(socket.getOutputStream());       writer.println("bye");       writer.flush();       continue;      }        // 向所有的客户端发送接收到信息,实现群聊      for (int i = 0; i < socketList.size(); i++) {       writer = new PrintWriter(socketList.get(i)         .getOutputStream());       writer.println(count + " say: " + message);       writer.flush();      }       }    } catch (IOException e) {     e.printStackTrace();    }   }  }    public class ChatServer {     /**    * Description    *     * @param args    */   public void startWork() throws IOException {    ServerSocket serverSocket = new ServerSocket(2345);    List<Socket> socketList = new ArrayList<Socket>();    Socket socket = null;    int count = 0;    while (true) {     socket = serverSocket.accept();     count++;     System.out.println(count + " clinet connected to the server!");     // 将每一个连接到该服务器的客户端,加到List中     socketList.add(socket);     // 每一个连接到服务器的客户端,服务器开启一个新的线程来处理     new Chat(count, socket, socketList).start();    }   }     /**    * Description    *     * @param args    * @throws IOException    */   public static void main(String[] args) throws IOException {    ChatServer chatServer = new ChatServer();    chatServer.startWork();   }    }
客户端主要是两个类,一个是接收服务器端发送过来的信息,一个是向服务器发送客户端输入的信息
import java.io.BufferedReader;  import java.io.IOException;  import java.io.InputStreamReader;  import java.io.PrintWriter;  import java.net.Socket;  import java.net.UnknownHostException;    /**   * 接受服务器信息   */  class ReadMes extends Thread {   private Socket socket;     public ReadMes(Socket socket) {    this.socket = socket;   }     public void run() {    BufferedReader reader = null;    try {     reader = new BufferedReader(new InputStreamReader(       socket.getInputStream()));     String message = null;     while (true) {      message = reader.readLine();      // 当读服务器信息线程接收到bye,该线程退出      if (message.equals("bye")) {       break;      }      System.out.println(message);     }    } catch (IOException e) {     e.printStackTrace();    } finally {     try {      if (reader != null) {       reader.close();      }     } catch (IOException e) {      e.printStackTrace();     }    }   }    }    /**   *    * 发送信息   */  class SendMes extends Thread {   private Socket socket;     public SendMes(Socket socket) {    this.socket = socket;   }     public void run() {    BufferedReader input = null;    PrintWriter writer = null;    try {     input = new BufferedReader(new InputStreamReader(System.in));     writer = new PrintWriter(socket.getOutputStream());     String message = null;     while (true) {      message = input.readLine();      // 当输入bye客户端退出      if (message.equals("bye")) {       break;      }      // 向服务器端发送信息      writer.println(message);      writer.flush();     }    } catch (IOException e) {     e.printStackTrace();    } finally {     if (input != null) {      try {       input.close();      } catch (IOException e) {       e.printStackTrace();      }     }     if (writer != null) {      writer.close();     }    }   }  }    public class ChatClient {   private String ipAdress;     public ChatClient(String ipAString) {    this.ipAdress = ipAdress;   }     public void startWork() throws UnknownHostException, IOException {    Socket socket = new Socket(ipAdress, 2345);    new ReadMes(socket).start();    new SendMes(socket).start();         }     /**    * Description    *     * @param args    * @throws UnknownHostException    * @throws IOException    */   public static void main(String[] args) throws UnknownHostException,     IOException {    ChatClient chatClient = new ChatClient(args[0]);    chatClient.startWork();   }    }