根據馬士兵老師聊天室程式進行最佳化,同時增加聊天者之間的互動。
同時增加服務端會為每一個用戶端增加一個互動視窗,讓伺服器可以和每一個用戶端互動!
服務端代碼
1.import java.net.*;2.import java.util.*;3.import java.io.*;4.import java.awt.*;5.import java.awt.event.*;6.import javax.swing.*;7.8.import javax.swing.JFrame;9.10.public class ChatServer extends JFrame {11. JTextArea ta = new JTextArea();12. ServerSocket server = null;13. Collection cClient = new ArrayList();14.15. public ChatServer(int port) throws Exception {16. server = new ServerSocket(port);17. add(ta, BorderLayout.CENTER);18. setBounds(200, 200, 300, 450);19. this.addWindowListener(new WindowAdapter() {20. public void windowClosing(WindowEvent e) {21. System.exit(0);22. }23. });24. setVisible(true);25. }26.27. public void startServer() throws Exception {28. while (true) {29. Socket s = server.accept();30. cClient.add(new ClientConn(s));31. ta.append(s.getInetAddress().getHostName() + "進入" + " " + "連接埠號碼"32. + s.getPort());33. ta.append("\n" + "當前在前總人數: " + cClient.size() + "\n\n");34. }35. }36.37. class ClientConn extends Frame implements Runnable, ActionListener {38. TextArea ta1 = null;39. TextArea ta2 = null;40. Button btn = null;41. Socket s = null;42.43. public ClientConn(Socket s) {44. ta1 = new TextArea(3, 30);45. ta2 = new TextArea(2, 15);46. btn = new Button("發送");47. this.setLayout(new BorderLayout());48. this.add(ta1, BorderLayout.CENTER);49. this.add(ta2, BorderLayout.SOUTH);50. this.add(btn, BorderLayout.EAST);51. this.setSize(300, 200);52. this.setVisible(true);53. this.setTitle("" + s.getInetAddress().getHostName() + "連接埠"54. + s.getPort());55. this.s = s;56. (new Thread(this)).start();57. btn.addActionListener(this);58. }59.60. public void actionPerformed(ActionEvent e) {61. try {62. DataOutputStream dos = new DataOutputStream(s.getOutputStream());63. dos.writeUTF("伺服器:\n" + ta2.getText() + "\n");64. ta1.append("伺服器:\n" + ta2.getText() + "\n");65. ta2.setText("");66. } catch (IOException E) {67.68. }69. }70.71. public void send(String str, String st) throws IOException {72. DataOutputStream dos = new DataOutputStream(s.getOutputStream());73. dos.writeUTF(st + "說:\n" + str);74. }75.76. public void dispose() {77. try {78. // this.setVisible(false);79. super.dispose();80. ta.append(s.getInetAddress().getHostName() + "退出" + "\n");81. if (s != null)82. s.close();83. cClient.remove(this);84. ta.append("當前線上人數: " + cClient.size() + "\n\n");85. } catch (Exception e) {86. e.printStackTrace();87. }88. }89.90. public void run() {91. try {92.93. DataInputStream dis = new DataInputStream(s.getInputStream());94. String str = dis.readUTF();95. String st = s.getInetAddress().getHostName();96. while (str != null && str.length() != 0) {97. for (Iterator it = cClient.iterator(); it.hasNext();) {98. ClientConn cc = (ClientConn) it.next();99. if (this != cc) {100. cc.send(str, st);101. }102. }103. ta1.append(st + "說:\n" + str + "\n");104. str = dis.readUTF();105. }106. this.dispose();107. } catch (Exception e) {108. this.dispose();109. }110.111. }112. }113.114. public static void main(String[] args) throws Exception {115. JFrame.setDefaultLookAndFeelDecorated(true);116. ChatServer cs = new ChatServer(8888);117. cs.startServer();118. }119.}