歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Java簡易聊天室

支持多人向服務器端發送消息並由服務器發送至各個客戶端

服務器端代碼:

[java]
  1. /* 
  2. *   @topic:服務器端 
  3. */  
  4. import java.io.*;  
  5. import java.net.*;  
  6. import java.util.List;  
  7. import java.util.ArrayList;  
  8. import java.util.concurrent.ExecutorService;  
  9. import java.util.concurrent.Executors;  
  10. public class Server{  
  11.     //服務器端口   
  12.     private static final int SERVERPORT = 8889 ;  
  13.     //客戶端連接   
  14.     private static List<Socket> mClientList = new ArrayList<Socket>();  
  15.     //線程池   
  16.     private ExecutorService mExecutorService;  
  17.     //ServerSocket對象   
  18.     private ServerSocket mServerSocket;  
  19.     public static void main(String[] args){  
  20.         //System.out.println("Hello Landor!");   
  21.         new Server();  
  22.     }  
  23.   
  24.     public Server(){  
  25.         try{  
  26.             //設置服務器端口   
  27.             mServerSocket = new ServerSocket(SERVERPORT);  
  28.             //創建一個線程池   
  29.             mExecutorService = Executors.newCachedThreadPool();  
  30.             System.out.println("wait client :");  
  31.             //用來臨時保存客戶端連接的Socket對象   
  32.             Socket client = null;  
  33.             while(true){  
  34.                 //接收客戶連接並添加到List中   
  35.                 client = mServerSocket.accept();  
  36.                 mClientList.add(client);  
  37.                 //開啟一個客戶端線程   
  38.                 mExecutorService.execute(new ThreadServer(client));  
  39.             }  
  40.         }catch(IOException e){  
  41.             e.printStackTrace();  
  42.         }  
  43.     }  
  44.     //每個客戶端單獨開啟一個線程   
  45.     static class ThreadServer implements Runnable{  
  46.         private Socket mSocket;  
  47.         private BufferedReader mBufferedReader;  
  48.         private PrintWriter mPrintWriter;  
  49.         private String mStrMSG;  
  50.   
  51.         public ThreadServer(Socket socket) throws IOException{  
  52.             this.mSocket = socket;  
  53.             mBufferedReader = new BufferedReader(new InputStreamReader(  
  54.                 socket.getInputStream()));  
  55.             mStrMSG = "user: "+this.mSocket.getInetAddress()+" come total:"  
  56.                     + mClientList.size();  
  57.             sendMessage();  
  58.         }  
  59.   
  60.         public void run(){  
  61.             try{  
  62.                 while((mStrMSG = mBufferedReader.readLine())!=null){  
  63.                     if(mStrMSG.trim().equals("exit")){  
  64.                         //當一個客戶端退出時   
  65.                         mClientList.remove(mSocket);  
  66.                         mBufferedReader.close();  
  67.                         mPrintWriter.close();  
  68.                         mStrMSG = "user:" + this.mSocket.getInetAddress()  
  69.                                 + " exit total:" + mClientList.size();  
  70.                         mSocket.close();  
  71.                         sendMessage();  
  72.                         break;  
  73.                     }else{  
  74.                         mStrMSG = mSocket.getInetAddress() + ":" + mStrMSG;  
  75.                         sendMessage();  
  76.                     }  
  77.                 }  
  78.             }catch(IOException e){  
  79.                 e.printStackTrace();  
  80.             }  
  81.         }  
  82.   
  83.         //發送消息給所有客戶端   
  84.         private void sendMessage() throws IOException {  
  85.             System.out.println(mStrMSG);  
  86.             for(Socket client : mClientList){  
  87.                 mPrintWriter = new PrintWriter(client.getOutputStream(),true);  
  88.                 mPrintWriter.println(mStrMSG);  
  89.             }  
  90.         }  
  91.     }  
  92. }  

客戶端:

[java]
  1. import java.io.BufferedReader;  
  2. import java.io.InputStreamReader;  
  3. import java.io.PrintWriter;  
  4. import java.net.Socket;  
  5. import java.util.concurrent.ExecutorService;  
  6. import java.util.concurrent.Executors;  
  7. /* 
  8. *   @topic:客戶端 
  9. */  
  10. public class Client{  
  11.     private static final int PORT = 8889;  
  12.     private static ExecutorService exec = Executors.newCachedThreadPool();  
  13.   
  14.     public static void main(String[] args) throws Exception{  
  15.         //System.out.println("Hello Landor!");   
  16.         new Client();  
  17.     }  
  18.   
  19.     public Client(){  
  20.         try{  
  21.             Socket socket = new Socket("localhost",PORT);  
  22.             exec.execute(new Sender(socket));  
  23.             BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));  
  24.             String msg;  
  25.             while((msg = br.readLine())!=null){  
  26.                 System.out.println(msg);  
  27.             }  
  28.         }catch(Exception e){  
  29.             e.printStackTrace();  
  30.         }  
  31.     }  
  32.   
  33.     //客戶端線程獲取控制台輸入消息   
  34.     static class Sender implements Runnable{  
  35.         private Socket socket;  
  36.           
  37.         public Sender(Socket socket){  
  38.             this.socket = socket;  
  39.         }  
  40.   
  41.         public void run(){  
  42.             try{  
  43.                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
  44.                 PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);  
  45.                 while(true){  
  46.                     String msg = br.readLine();  
  47.                     pw.println(msg);  
  48.                     if (msg.trim().equals("exit")) {  
  49.                         pw.close();  
  50.                         br.close();  
  51.                         exec.shutdownNow();  
  52.                         break;  
  53.                     }  
  54.                 }  
  55.             }catch(Exception e){  
  56.                 e.printStackTrace();  
  57.             }  
  58.         }  
  59.     }  
  60. }  
Copyright © Linux教程網 All Rights Reserved