用java多线程实现一个控制台聊天室,呵呵,好玩!
聊天室服务器端
聊天室服务器端
- package tf.thread;
- 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;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class ChatServer {
- private static final int PORT = 8888; // 端口号
- private static List<Socket> list = new ArrayList<Socket>(); // 保存连接对象
- private ExecutorService exec;
- private ServerSocket server;
- public static void main(String[] args) {
- new ChatServer();
- }
- public ChatServer() {
- try {
- server = new ServerSocket(PORT);
- exec = Executors.newCachedThreadPool();
- System.out.println("服务器已启动!");
- Socket client = null;
- while (true) {
- client = server.accept(); // 接收客户连接
- list.add(client);
- exec.execute(new ChatTask(client));
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- static class ChatTask implements Runnable {
- private Socket socket;
- private BufferedReader br;
- private PrintWriter pw;
- private String msg;
- public ChatTask(Socket socket) throws IOException {
- this.socket = socket;
- br = new BufferedReader(new InputStreamReader(socket
- .getInputStream()));
- msg = "【" + this.socket.getInetAddress() + "】进入聊天室!当前聊天室有【"
- + list.size() + "】人";
- sendMessage();
- }
- public void run() {
- try {
- while ((msg = br.readLine()) != null) {
- if (msg.trim().equals("bye")) {
- list.remove(socket);
- br.close();
- pw.close();
- msg = "【" + socket.getInetAddress() + "】离开聊天室!当前聊天室有【"
- + list.size() + "】人";
- socket.close();
- sendMessage();
- break;
- } else {
- msg = "【" + socket.getInetAddress() + "】说:" + msg;
- sendMessage();
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 群发消息给聊天室的所有人
- */
- private void sendMessage() throws IOException {
- System.out.println(msg);
- for (Socket client : list) {
- pw = new PrintWriter(client.getOutputStream(), true);
- pw.println(msg);
- }
- }
- }
- }
聊天室客户端(输入bye可退出聊天室)
- package tf.thread;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class ChatClient {
- private static final int PORT = 8888;
- private static ExecutorService exec = Executors.newCachedThreadPool();
- public static void main(String[] args) throws Exception {
- new ChatClient();
- }
- public ChatClient() {
- try {
- Socket socket = new Socket("192.168.1.120", PORT);
- exec.execute(new Sender(socket));
- System.out
- .println("【" + socket.getInetAddress() + "】您好,欢迎来到阿飞聊天室!");
- BufferedReader br = new BufferedReader(new InputStreamReader(socket
- .getInputStream()));
- String msg;
- while ((msg = br.readLine()) != null) {
- System.out.println(msg);
- }
- } catch (Exception e) {
- }
- }
- /**
- * 客户端线程获取控制台输入消息
- */
- static class Sender implements Runnable {
- private Socket socket;
- public Sender(Socket socket) {
- this.socket = socket;
- }
- public void run() {
- try {
- BufferedReader br = new BufferedReader(new InputStreamReader(
- System.in));
- PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
- String msg;
- while (true) {
- msg = br.readLine();
- pw.println(msg);
- if (msg.trim().equals("bye")) {
- pw.close();
- br.close();
- exec.shutdownNow();
- break;
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
声明:ITeye文章版权属于作者,受法律保护。没有作者
mina 几句就搞定了的
http://www.iteye.com/topic/365764