1. 程式人生 > >練手小專案一:JAVA聊天室原始碼

練手小專案一:JAVA聊天室原始碼

客戶端:

  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.awt.event.WindowAdapter;
  5. import java.awt.event.WindowEvent;
  6. import java.io.DataInputStream;
  7. import java.io.DataOutputStream;
  8. import java.io.IOException;
  9. import java.net.Socket;
  10. import java.net.SocketException;
  11. import java.net.UnknownHostException;
  12. publicclass Client extends Frame { //繼承Frame類
  13.     TextField tf = new TextField(); //輸入框物件
  14.     TextArea ta = new TextArea(); //顯示框物件
  15.     Socket s = null;
  16.     DataOutputStream dos = null;
  17.     DataInputStream dis = null;
  18. boolean bConnected = false;
  19.     recvThread r = 
    new recvThread(); //執行緒類
  20. publicstaticvoid main(String[] args) {
  21. new Client().creatFrame();
  22.     }
  23. publicvoid creatFrame() { //產生圖形介面     
  24. this.setBounds(300300300300);
  25.         ta.setEditable(false);
  26. this.add(tf, BorderLayout.SOUTH);
  27. this.add(ta, BorderLayout.NORTH);
  28. this.addWindowListener(new WindowAdapter() { 
    //響應關閉視窗事件
  29. publicvoid windowClosing(WindowEvent e) {
  30.                         disconnect();
  31.                         System.exit(0);
  32.                     }
  33.                 });
  34.         tf.addActionListener(new tfListener()); //響應輸入事件
  35. this.pack();
  36. this.setVisible(true);
  37.         connect();
  38. new Thread(r).start();
  39.     }
  40. publicvoid connect() {
  41. try {
  42.             s = new Socket("127.0.0.1"8888); //建立客戶端物件
  43.             dos = new DataOutputStream(s.getOutputStream());
  44.             dis = new DataInputStream(s.getInputStream());
  45.             bConnected = true;
  46.         } catch (UnknownHostException e) {
  47.             e.printStackTrace();
  48.         } catch (IOException e) {
  49.             e.printStackTrace();
  50.         }
  51.     }
  52. publicvoid disconnect() { //視窗關閉時關閉客戶端,輸入,輸出
  53. try {
  54.             dos.close();
  55.             dis.close();
  56.             s.close();
  57.         } catch (IOException e) {
  58.             e.printStackTrace();
  59.         }
  60.     }
  61. class tfListener implements ActionListener { //輸入框實現的介面,響應輸入事件
  62. publicvoid actionPerformed(ActionEvent e) {
  63.             String str = tf.getText();
  64. //ta.setText(str);
  65.             tf.setText("");
  66. try {
  67.                 dos.writeUTF(str);
  68.                 dos.flush();
  69. //dos.close();
  70.             } catch (IOException e1) {
  71.                 e1.printStackTrace();
  72.             }
  73.         }
  74.     }
  75. class recvThread implements Runnable { //客戶端執行緒接收資料
  76. publicvoid run() {
  77. try {
  78. while (bConnected) {
  79.                     String str;
  80.                     str = dis.readUTF(); //拿到資料
  81.                     ta.setText(ta.getText() + str + '/n');//顯示到顯示框中
  82.                 }
  83.             } catch (SocketException e) {
  84.                 System.out.println("退出了");
  85.             } catch (IOException e1) {
  86.                 e1.printStackTrace();
  87.             }
  88.         }
  89.     }
  90. }

---------------------------------------------------------------------------------------------------------------------------------------

服務端:

  1. import java.io.DataInputStream;
  2. import java.io.DataOutputStream;
  3. import java.io.EOFException;
  4. import java.io.IOException;
  5. import java.net.BindException;
  6. import java.net.ServerSocket;
  7. import java.net.Socket;
  8. import java.net.SocketException;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. publicclass Server {
  12. boolean started = false;
  13.     ServerSocket ss = null;
  14.     List<ChatClient> clients = new ArrayList<ChatClient>(); //儲存客戶端執行緒類
  15. publicstaticvoid main(String[] args) {
  16. new Server().start();
  17.     }
  18. void start() {
  19. try {
  20.             ss = new ServerSocket(8888); //建立服務端物件
  21.             started = true;
  22.         } catch (BindException e) {
  23.             System.out.println("埠使用中");
  24.         } catch (IOException e) {
  25.             e.printStackTrace();
  26.         }
  27. try {
  28. while (started) {
  29.                 Socket s = ss.accept(); //接收客戶端
  30.                 ChatClient c = new ChatClient(s);
  31.                 System.out.println("客戶端接收成功");
  32. new Thread(c).start(); //啟動執行緒
  33.                 clients.add(c); //新增執行緒類
  34.             }
  35.         } catch (IOException e) {
  36.             e.printStackTrace();
  37.         } finally {
  38. try {
  39.                 ss.close();
  40.             } catch (IOException e) {
  41. // TODO 自動生成 catch 塊
  42.                 e.printStackTrace();
  43.             }
  44.         }
  45.     }
  46. class ChatClient implements Runnable { //建立客戶端執行緒接收,傳送資料
  47. private Socket s;
  48.         DataInputStream dis = null;
  49.         DataOutputStream dos = null;
  50. boolean bConnected = false;
  51. public ChatClient(Socket s) {
  52. this.s = s;
  53. try {
  54.                 dis = new DataInputStream(s.getInputStream());
  55.                 dos = new DataOutputStream(s.getOutputStream());
  56.                 bConnected = true;
  57.             } catch (IOException e) {
  58.                 e.printStackTrace();
  59.             }
  60.         }
  61. void send(String str) {
  62. try {
  63.                 dos.writeUTF(str);
  64.             } catch (SocketException e) {
  65.                 System.out.println("對方退出了");
  66.             } catch (IOException e) {
  67.                 e.printStackTrace();
  68.             }
  69.         }
  70. publicvoid run() {
  71. try {
  72. while (bConnected) {
  73.                     String str = dis.readUTF();
  74. // System.out.println(str);
  75. for (int i = 0; i < clients.size(); i++) {
  76.                         ChatClient c = clients.get(i);
  77.                         c.send(str);
  78.                     }
  79.                 }
  80.             } catch (EOFException e) {
  81.                 System.out.println("客戶端退出了");
  82.             } catch (IOException e) {
  83.                 e.printStackTrace();
  84.             } finally {
  85. if (dis != null)
  86. if (s != null)
  87. try {
  88.                             dis.close();
  89.                             s.close();
  90.                             dos.close();
  91.                         } catch (IOException e) {
  92.                             e.printStackTrace();
  93.                         }
  94.             }
  95.         }
  96.     }
  97. }