码迷,mamicode.com
首页 > 编程语言 > 详细

java多线程控制台聊天室(转)

时间:2015-03-09 23:41:51      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

 

用java多线程实现一个控制台聊天室,呵呵,好玩! 
聊天室服务器端 
Java代码  技术分享
  1. package tf.thread;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6. import java.io.PrintWriter;  
  7. import java.net.ServerSocket;  
  8. import java.net.Socket;  
  9. import java.util.ArrayList;  
  10. import java.util.List;  
  11. import java.util.concurrent.ExecutorService;  
  12. import java.util.concurrent.Executors;  
  13.   
  14. public class ChatServer {  
  15.     private static final int PORT = 8888; // 端口号  
  16.     private static List<Socket> list = new ArrayList<Socket>(); // 保存连接对象  
  17.     private ExecutorService exec;  
  18.     private ServerSocket server;  
  19.   
  20.     public static void main(String[] args) {  
  21.         new ChatServer();  
  22.     }  
  23.   
  24.     public ChatServer() {  
  25.         try {  
  26.             server = new ServerSocket(PORT);  
  27.             exec = Executors.newCachedThreadPool();  
  28.             System.out.println("服务器已启动!");  
  29.   
  30.             Socket client = null;  
  31.             while (true) {  
  32.                 client = server.accept(); // 接收客户连接  
  33.                 list.add(client);  
  34.                 exec.execute(new ChatTask(client));  
  35.             }  
  36.         } catch (IOException e) {  
  37.             e.printStackTrace();  
  38.         }  
  39.     }  
  40.   
  41.     static class ChatTask implements Runnable {  
  42.         private Socket socket;  
  43.         private BufferedReader br;  
  44.         private PrintWriter pw;  
  45.         private String msg;  
  46.   
  47.         public ChatTask(Socket socket) throws IOException {  
  48.             this.socket = socket;  
  49.             br = new BufferedReader(new InputStreamReader(socket  
  50.                     .getInputStream()));  
  51.             msg = "【" + this.socket.getInetAddress() + "】进入聊天室!当前聊天室有【"  
  52.                     + list.size() + "】人";  
  53.   
  54.             sendMessage();  
  55.         }  
  56.   
  57.         public void run() {  
  58.             try {  
  59.                 while ((msg = br.readLine()) != null) {  
  60.                     if (msg.trim().equals("bye")) {  
  61.                         list.remove(socket);  
  62.                         br.close();  
  63.                         pw.close();  
  64.                         msg = "【" + socket.getInetAddress() + "】离开聊天室!当前聊天室有【"  
  65.                                 + list.size() + "】人";  
  66.                         socket.close();  
  67.   
  68.                         sendMessage();  
  69.                         break;  
  70.                     } else {  
  71.                         msg = "【" + socket.getInetAddress() + "】说:" + msg;  
  72.                         sendMessage();  
  73.                     }  
  74.                 }  
  75.             } catch (IOException e) {  
  76.                 e.printStackTrace();  
  77.             }  
  78.         }  
  79.   
  80.         /** 
  81.          * 群发消息给聊天室的所有人 
  82.          */  
  83.         private void sendMessage() throws IOException {  
  84.             System.out.println(msg);  
  85.             for (Socket client : list) {  
  86.                 pw = new PrintWriter(client.getOutputStream(), true);  
  87.                 pw.println(msg);  
  88.             }  
  89.         }  
  90.     }  
  91. }  
聊天室客户端(输入bye可退出聊天室) 
Java代码  技术分享
  1. package tf.thread;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.InputStreamReader;  
  5. import java.io.PrintWriter;  
  6. import java.net.Socket;  
  7. import java.util.concurrent.ExecutorService;  
  8. import java.util.concurrent.Executors;  
  9.   
  10. public class ChatClient {  
  11.     private static final int PORT = 8888;  
  12.     private static ExecutorService exec = Executors.newCachedThreadPool();  
  13.   
  14.     public static void main(String[] args) throws Exception {  
  15.         new ChatClient();  
  16.     }  
  17.   
  18.     public ChatClient() {  
  19.         try {  
  20.             Socket socket = new Socket("192.168.1.120", PORT);  
  21.             exec.execute(new Sender(socket));  
  22.             System.out  
  23.                     .println("【" + socket.getInetAddress() + "】您好,欢迎来到阿飞聊天室!");  
  24.   
  25.             BufferedReader br = new BufferedReader(new InputStreamReader(socket  
  26.                     .getInputStream()));  
  27.             String msg;  
  28.             while ((msg = br.readLine()) != null) {  
  29.                 System.out.println(msg);  
  30.             }  
  31.         } catch (Exception e) {  
  32.   
  33.         }  
  34.     }  
  35.   
  36.     /** 
  37.      * 客户端线程获取控制台输入消息 
  38.      */  
  39.     static class Sender implements Runnable {  
  40.         private Socket socket;  
  41.   
  42.         public Sender(Socket socket) {  
  43.             this.socket = socket;  
  44.         }  
  45.   
  46.         public void run() {  
  47.             try {  
  48.                 BufferedReader br = new BufferedReader(new InputStreamReader(  
  49.                         System.in));  
  50.                 PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);  
  51.                 String msg;  
  52.   
  53.                 while (true) {  
  54.                     msg = br.readLine();  
  55.                     pw.println(msg);  
  56.   
  57.                     if (msg.trim().equals("bye")) {  
  58.                         pw.close();  
  59.                         br.close();  
  60.                         exec.shutdownNow();  
  61.                         break;  
  62.                     }  
  63.                 }  
  64.             } catch (Exception e) {  
  65.                 e.printStackTrace();  
  66.             }  
  67.         }  
  68.     }  
  69. }  
 
mina 几句就搞定了的
 
 
http://www.iteye.com/topic/365764
 
 
 
 
 

java多线程控制台聊天室(转)

标签:

原文地址:http://www.cnblogs.com/softidea/p/4324725.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!