Java---基於TCP協議的相互即時通訊小程式
這是幾年前,新浪的一個面試題~要求是3天之內實現~
通過TCP 協議,建立一個伺服器端。
通過配置伺服器端的IP和埠:
客戶端之間就可以相互通訊~
上線了全部線上使用者會收到你上線的通知。
下線了全部的線上使用者會收到你下線的通知!
可以私聊,可以群聊。
這是第一個版本~以後有空可以再增加功能~比如傳檔案啊~等等~
設計思想如下:
在伺服器端 用一個HashMap
伺服器端原始碼:
<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">package</span> cn.hncu; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.awt.BorderLayout; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.awt.Dimension; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.awt.Toolkit; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.awt.event.ActionEvent; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.awt.event.ActionListener; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.awt.event.KeyEvent; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.io.IOException; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.io.PrintWriter; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.net.ServerSocket; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.net.Socket; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.util.HashMap; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.util.Iterator; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.util.Map; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.util.Scanner; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> javax.swing.DefaultListModel; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> javax.swing.JFrame; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> javax.swing.JList; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> javax.swing.JMenu; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> javax.swing.JMenuBar; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> javax.swing.JMenuItem; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> javax.swing.JPanel; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> javax.swing.JScrollPane; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> javax.swing.JTextArea; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> javax.swing.KeyStroke; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> javax.swing.border.Border; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> javax.swing.border.TitledBorder; <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 伺服器 * *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @author</span> 陳浩翔 * *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @version</span> 1.0 2016-5-13 */</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">ServerForm</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extends</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">JFrame</span> {</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> JList<String> list; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> JTextArea area; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> DefaultListModel<String> lm; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-title" style="box-sizing: border-box;">ServerForm</span>() { JPanel p = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> JPanel(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> BorderLayout()); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 最右邊的使用者線上列表</span> lm = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> DefaultListModel<String>(); list = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> JList<String>(lm); JScrollPane js = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> JScrollPane(list); Border border = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> TitledBorder(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"線上"</span>); js.setBorder(border); Dimension d = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Dimension(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, p.getHeight()); js.setPreferredSize(d);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 設定位置</span> p.add(js, BorderLayout.EAST); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 通知文字區域</span> area = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> JTextArea(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//area.setEnabled(false);//不能選中和修改</span> area.setEditable(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">false</span>); p.add(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> JScrollPane(area), BorderLayout.CENTER); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.getContentPane().add(p); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 新增選單項</span> JMenuBar bar = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> JMenuBar();<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 選單條</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.setJMenuBar(bar); JMenu jm = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> JMenu(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"控制(C)"</span>); jm.setMnemonic(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'C'</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 設定助記符---Alt+'C',顯示出來,但不執行</span> bar.add(jm); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">final</span> JMenuItem jmi1 = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> JMenuItem(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"開啟"</span>); jmi1.setAccelerator(KeyStroke.getKeyStroke(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'R'</span>, KeyEvent.CTRL_MASK));<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 設定快捷鍵Ctrl+'R'</span> jmi1.setActionCommand(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"run"</span>); jm.add(jmi1); JMenuItem jmi2 = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> JMenuItem(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"退出"</span>); jmi2.setAccelerator(KeyStroke.getKeyStroke(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'E'</span>, KeyEvent.CTRL_MASK));<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 設定快捷鍵Ctrl+'R'</span> jmi2.setActionCommand(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"exit"</span>); jm.add(jmi2); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 監聽</span> ActionListener a1 = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ActionListener() { <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">actionPerformed</span>(ActionEvent e) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (e.getActionCommand().equals(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"run"</span>)) { startServer(); jmi1.setEnabled(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">false</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 內部方法~訪問的只能是final物件</span> } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> { System.exit(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>); } } }; jmi1.addActionListener(a1); Toolkit tk = Toolkit.getDefaultToolkit(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> width = (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>) tk.getScreenSize().getWidth(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> height = (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>) tk.getScreenSize().getHeight(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.setBounds(width / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>, height / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>, width / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>, height / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.setDefaultCloseOperation(EXIT_ON_CLOSE);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 關閉按鈕器作用</span> setVisible(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">final</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> PORT = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9090</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">startServer</span>() { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try</span> { ServerSocket server = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ServerSocket(PORT); area.append(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"啟動服務:"</span> + server); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ServerThread(server).start(); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (IOException e) { e.printStackTrace(); } } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 用來儲存所有線上使用者的名字和Socket----池</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> Map<String, Socket> usersMap = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> HashMap<String, Socket>(); class ServerThread extends Thread { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> ServerSocket server; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-title" style="box-sizing: border-box;">ServerThread</span>(ServerSocket server) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.server = server; } <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">run</span>() { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try</span> {<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 和客戶端握手</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>) { Socket socketClient = server.accept(); Scanner sc = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Scanner(socketClient.getInputStream()); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (sc.hasNext()) { String userName = sc.nextLine(); area.append(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\r\n使用者[ "</span> + userName + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" ]登入 "</span> + socketClient);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 在客戶端通知</span> lm.addElement(userName);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 新增到使用者線上列表</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ClientThread(socketClient).start();<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 專門為這個客戶端服務</span> usersMap.put(userName, socketClient);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 把當前登入的使用者加到“線上使用者”池中</span> msgAll(userName);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 把“當前使用者登入的訊息即使用者名稱”通知給所有其他已經線上的人</span> msgSelf(socketClient);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 通知當前登入的使用者,有關其他線上人的資訊</span> } } } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (IOException e) { e.printStackTrace(); } } } class ClientThread extends Thread { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> Socket socketClient; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-title" style="box-sizing: border-box;">ClientThread</span>(Socket socketClient) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.socketClient = socketClient; } <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">run</span>() { System.out.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"一個與客戶端通訊的執行緒啟動並開始通訊..."</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try</span> { Scanner sc = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Scanner(socketClient.getInputStream()); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> (sc.hasNext()) { String msg = sc.nextLine(); System.out.println(msg); String msgs[] = msg.split(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"@#@#"</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//防黑</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(msgs.length!=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>){ System.out.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"防黑處理..."</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">continue</span>; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"on"</span>.equals(msgs[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>])){ sendMsgToSb(msgs); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"exit"</span>.equals(msgs[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>])){ <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//伺服器顯示</span> area.append(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\r\n使用者[ "</span> + msgs[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>] + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" ]已退出!"</span> + usersMap.get(msgs[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>])); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//從線上使用者池中把該使用者刪除</span> usersMap.remove(msgs[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>]); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//伺服器的線上列表中把該使用者刪除</span> lm.removeElement(msgs[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>]); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//通知其他使用者,該使用者已經退出</span> sendExitMsgToAll(msgs); } } } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (IOException e) { e.printStackTrace(); } } } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//通知其他使用者。該使用者已經退出</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">sendExitMsgToAll</span>(String[] msgs) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">throws</span> IOException { Iterator<String> userNames = usersMap.keySet().iterator(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span>(userNames.hasNext()){ String userName = userNames.next(); Socket s = usersMap.get(userName); PrintWriter pw = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> PrintWriter(s.getOutputStream(),<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>); String str = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"[email protected]#@#[email protected]#@#使用者[ "</span>+msgs[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>]+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" ]已退出!"</span>; pw.println(str); pw.flush(); str = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"[email protected]#@#[email protected]#@#"</span>+msgs[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>]; pw.println(str); pw.flush(); } } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//伺服器把客戶端的聊天訊息轉發給相應的其他客戶端</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">sendMsgToSb</span>(String[] msgs) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">throws</span> IOException { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"全部"</span>.equals(msgs[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>])){ Iterator<String> userNames = usersMap.keySet().iterator(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//遍歷每一個線上使用者,把聊天訊息發給他</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span>(userNames.hasNext()){ String userName = userNames.next(); Socket s = usersMap.get(userName); PrintWriter pw = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> PrintWriter(s.getOutputStream(),<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>); String str = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"[email protected]#@#"</span>+msgs[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>]+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"@#@#"</span>+msgs[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>]; pw.println(str); pw.flush(); } }<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>{ Socket s = usersMap.get(msgs[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>]); PrintWriter pw = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> PrintWriter(s.getOutputStream(),<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>); String str = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"[email protected]#@#"</span>+msgs[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>]+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"對你@#@#"</span>+msgs[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>]; pw.println(str); pw.flush(); } } <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 把“當前使用者登入的訊息即使用者名稱”通知給所有其他已經線上的人 * *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> userName */</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 技術思路:從池中依次把每個socket(代表每個線上使用者)取出,向它傳送userName</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">msgAll</span>(String userName) { Iterator<Socket> it = usersMap.values().iterator(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> (it.hasNext()) { Socket s = it.next(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try</span> { PrintWriter pw = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> PrintWriter(s.getOutputStream(), <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 加true為自動重新整理</span> String msg = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"[email protected]#@#[email protected]#@#使用者[ "</span> + userName + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" ]已登入!"</span>;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 通知客戶端顯示訊息</span> pw.println(msg); pw.flush(); msg = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"[email protected]#@#[email protected]#@#"</span> + userName;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 通知客戶端在線上列表新增使用者線上。</span> pw.println(msg); pw.flush(); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (IOException e) { e.printStackTrace(); } } } <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 通知當前登入的使用者,有關其他線上人的資訊 * *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> socketClient */</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 把原先已經線上的那些使用者的名字發給該登入使用者,讓他給自己介面中的lm新增相應的使用者名稱</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">msgSelf</span>(Socket socketClient) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try</span> { PrintWriter pw = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> PrintWriter(socketClient.getOutputStream(),<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>); Iterator<String> it = usersMap.keySet().iterator(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> (it.hasNext()) { String msg = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"[email protected]#@#[email protected]#@#"</span> + it.next(); pw.println(msg); pw.flush(); } } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (IOException e) { e.printStackTrace(); } } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">main</span>(String[] args) { JFrame.setDefaultLookAndFeelDecorated(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 設定裝飾</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ServerForm(); } }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li><li style="box-sizing: border-box; padding: 0px 5px;">60</li><li style="box-sizing: border-box; padding: 0px 5px;">61</li><li style="box-sizing: border-box; padding: 0px 5px;">62</li><li style="box-sizing: border-box; padding: 0px 5px;">63</li><li style="box-sizing: border-box; padding: 0px 5px;">64</li><li style="box-sizing: border-box; padding: 0px 5px;">65</li><li style="box-sizing: border-box; padding: 0px 5px;">66</li><li style="box-sizing: border-box; padding: 0px 5px;">67</li><li style="box-sizing: border-box; padding: 0px 5px;">68</li><li style="box-sizing: border-box; padding: 0px 5px;">69</li><li style="box-sizing: border-box; padding: 0px 5px;">70</li><li style="box-sizing: border-box; padding: 0px 5px;">71</li><li style="box-sizing: border-box; padding: 0px 5px;">72</li><li style="box-sizing: border-box; padding: 0px 5px;">73</li><li style="box-sizing: border-box; padding: 0px 5px;">74</li><li style="box-sizing: border-box; padding: 0px 5px;">75</li><li style="box-sizing: border-box; padding: 0px 5px;">76</li><li style="box-sizing: border-box; padding: 0px 5px;">77</li><li style="box-sizing: border-box; padding: 0px 5px;">78</li><li style="box-sizing: border-box; padding: 0px 5px;">79</li><li style="box-sizing: border-box; padding: 0px 5px;">80</li><li style="box-sizing: border-box; padding: 0px 5px;">81</li><li style="box-sizing: border-box; padding: 0px 5px;">82</li><li style="box-sizing: border-box; padding: 0px 5px;">83</li><li style="box-sizing: border-box; padding: 0px 5px;">84</li><li style="box-sizing: border-box; padding: 0px 5px;">85</li><li style="box-sizing: border-box; padding: 0px 5px;">86</li><li style="box-sizing: border-box; padding: 0px 5px;">87</li><li style="box-sizing: border-box; padding: 0px 5px;">88</li><li style="box-sizing: border-box; padding: 0px 5px;">89</li><li style="box-sizing: border-box; padding: 0px 5px;">90</li><li style="box-sizing: border-box; padding: 0px 5px;">91</li><li style="box-sizing: border-box; padding: 0px 5px;">92</li><li style="box-sizing: border-box; padding: 0px 5px;">93</li><li style="box-sizing: border-box; padding: 0px 5px;">94</li><li style="box-sizing: border-box; padding: 0px 5px;">95</li><li style="box-sizing: border-box; padding: 0px 5px;">96</li><li style="box-sizing: border-box; padding: 0px 5px;">97</li><li style="box-sizing: border-box; padding: 0px 5px;">98</li><li style="box-sizing: border-box; padding: 0px 5px;">99</li><li style="box-sizing: border-box; padding: 0px 5px;">100</li><li style="box-sizing: border-box; padding: 0px 5px;">101</li><li style="box-sizing: border-box; padding: 0px 5px;">102</li><li style="box-sizing: border-box; padding: 0px 5px;">103</li><li style="box-sizing: border-box; padding: 0px 5px;">104</li><li style="box-sizing: border-box; padding: 0px 5px;">105</li><li style="box-sizing: border-box; padding: 0px 5px;">106</li><li style="box-sizing: border-box; padding: 0px 5px;">107</li><li style="box-sizing: border-box; padding: 0px 5px;">108</li><li style="box-sizing: border-box; padding: 0px 5px;">109</li><li style="box-sizing: border-box; padding: 0px 5px;">110</li><li style="box-sizing: border-box; padding: 0px 5px;">111</li><li style="box-sizing: border-box; padding: 0px 5px;">112</li><li style="box-sizing: border-box; padding: 0px 5px;">113</li><li style="box-sizing: border-box; padding: 0px 5px;">114</li><li style="box-sizing: border-box; padding: 0px 5px;">115</li><li style="box-sizing: border-box; padding: 0px 5px;">116</li><li style="box-sizing: border-box; padding: 0px 5px;">117</li><li style="box-sizing: border-box; padding: 0px 5px;">118</li><li style="box-sizing: border-box; padding: 0px 5px;">119</li><li style="box-sizing: border-box; padding: 0px 5px;">120</li><li style="box-sizing: border-box; padding: 0px 5px;">121</li><li style="box-sizing: border-box; padding: 0px 5px;">122</li><li style="box-sizing: border-box; padding: 0px 5px;">123</li><li style="box-sizing: border-box; padding: 0px 5px;">124</li><li style="box-sizing: border-box; padding: 0px 5px;">125</li><li style="box-sizing: border-box; padding: 0px 5px;">126</li><li style="box-sizing: border-box; padding: 0px 5px;">127</li><li style="box-sizing: border-box; padding: 0px 5px;">128</li><li style="box-sizing: border-box; padding: 0px 5px;">129</li><li style="box-sizing: border-box; padding: 0px 5px;">130</li><li style="box-sizing: border-box; padding: 0px 5px;">131</li><li style="box-sizing: border-box; padding: 0px 5px;">132</li><li style="box-sizing: border-box; padding: 0px 5px;">133</li><li style="box-sizing: border-box; padding: 0px 5px;">134</li><li style="box-sizing: border-box; padding: 0px 5px;">135</li><li style="box-sizing: border-box; padding: 0px 5px;">136</li><li style="box-sizing: border-box; padding: 0px 5px;">137</li><li style="box-sizing: border-box; padding: 0px 5px;">138</li><li style="box-sizing: border-box; padding: 0px 5px;">139</li><li style="box-sizing: border-box; padding: 0px 5px;">140</li><li style="box-sizing: border-box; padding: 0px 5px;">141</li><li style="box-sizing: border-box; padding: 0px 5px;">142</li><li style="box-sizing: border-box; padding: 0px 5px;">143</li><li style="box-sizing: border-box; padding: 0px 5px;">144</li><li style="box-sizing: border-box; padding: 0px 5px;">145</li><li style="box-sizing: border-box; padding: 0px 5px;">146</li><li style="box-sizing: border-box; padding: 0px 5px;">147</li><li style="box-sizing: border-box; padding: 0px 5px;">148</li><li style="box-sizing: border-box; padding: 0px 5px;">149</li><li style="box-sizing: border-box; padding: 0px 5px;">150</li><li style="box-sizing: border-box; padding: 0px 5px;">151</li><li style="box-sizing: border-box; padding: 0px 5px;">152</li><li style="box-sizing: border-box; padding: 0px 5px;">153</li><li style="box-sizing: border-box; padding: 0px 5px;">154</li><li style="box-sizing: border-box; padding: 0px 5px;">155</li><li style="box-sizing: border-box; padding: 0px 5px;">156</li><li style="box-sizing: border-box; padding: 0px 5px;">157</li><li style="box-sizing: border-box; padding: 0px 5px;">158</li><li style="box-sizing: border-box; padding: 0px 5px;">159</li><li style="box-sizing: border-box; padding: 0px 5px;">160</li><li style="box-sizing: border-box; padding: 0px 5px;">161</li><li style="box-sizing: border-box; padding: 0px 5px;">162</li><li style="box-sizing: border-box; padding: 0px 5px;">163</li><li style="box-sizing: border-box; padding: 0px 5px;">164</li><li style="box-sizing: border-box; padding: 0px 5px;">165</li><li style="box-sizing: border-box; padding: 0px 5px;">166</li><li style="box-sizing: border-box; padding: 0px 5px;">167</li><li style="box-sizing: border-box; padding: 0px 5px;">168</li><li style="box-sizing: border-box; padding: 0px 5px;">169</li><li style="box-sizing: border-box; padding: 0px 5px;">170</li><li style="box-sizing: border-box; padding: 0px 5px;">171</li><li style="box-sizing: border-box; padding: 0px 5px;">172</li><li style="box-sizing: border-box; padding: 0px 5px;">173</li><li style="box-sizing: border-box; padding: 0px 5px;">174</li><li style="box-sizing: border-box; padding: 0px 5px;">175</li><li style="box-sizing: border-box; padding: 0px 5px;">176</li><li style="box-sizing: border-box; padding: 0px 5px;">177</li><li style="box-sizing: border-box; padding: 0px 5px;">178</li><li style="box-sizing: border-box; padding: 0px 5px;">179</li><li style="box-sizing: border-box; padding: 0px 5px;">180</li><li style="box-sizing: border-box; padding: 0px 5px;">181</li><li style="box-sizing: border-box; padding: 0px 5px;">182</li><li style="box-sizing: border-box; padding: 0px 5px;">183</li><li style="box-sizing: border-box; padding: 0px 5px;">184</li><li style="box-sizing: border-box; padding: 0px 5px;">185</li><li style="box-sizing: border-box; padding: 0px 5px;">186</li><li style="box-sizing: border-box; padding: 0px 5px;">187</li><li style="box-sizing: border-box; padding: 0px 5px;">188</li><li style="box-sizing: border-box; padding: 0px 5px;">189</li><li style="box-sizing: border-box; padding: 0px 5px;">190</li><li style="box-sizing: border-box; padding: 0px 5px;">191</li><li style="box-sizing: border-box; padding: 0px 5px;">192</li><li style="box-sizing: border-box; padding: 0px 5px;">193</li><li style="box-sizing: border-box; padding: 0px 5px;">194</li><li style="box-sizing: border-box; padding: 0px 5px;">195</li><li style="box-sizing: border-box; padding: 0px 5px;">196</li><li style="box-sizing: border-box; padding: 0px 5px;">197</li><li style="box-sizing: border-box; padding: 0px 5px;">198</li><li style="box-sizing: border-box; padding: 0px 5px;">199</li><li style="box-sizing: border-box; padding: 0px 5px;">200</li><li style="box-sizing: border-box; padding: 0px 5px;">201</li><li style="box-sizing: border-box; padding: 0px 5px;">202</li><li style="box-sizing: border-box; padding: 0px 5px;">203</li><li style="box-sizing: border-box; padding: 0px 5px;">204</li><li style="box-sizing: border-box; padding: 0px 5px;">205</li><li style="box-sizing: border-box; padding: 0px 5px;">206</li><li style="box-sizing: border-box; padding: 0px 5px;">207</li><li style="box-sizing: border-box; padding: 0px 5px;">208</li><li style="box-sizing: border-box; padding: 0px 5px;">209</li><li style="box-sizing: border-box; padding: 0px 5px;">210</li><li style="box-sizing: border-box; padding: 0px 5px;">211</li><li style="box-sizing: border-box; padding: 0px 5px;">212</li><li style="box-sizing: border-box; padding: 0px 5px;">213</li><li style="box-sizing: border-box; padding: 0px 5px;">214</li><li style="box-sizing: border-box; padding: 0px 5px;">215</li><li style="box-sizing: border-box; padding: 0px 5px;">216</li><li style="box-sizing: border-box; padding: 0px 5px;">217</li><li style="box-sizing: border-box; padding: 0px 5px;">218</li><li style="box-sizing: border-box; padding: 0px 5px;">219</li><li style="box-sizing: border-box; padding: 0px 5px;">220</li><li style="box-sizing: border-box; padding: 0px 5px;">221</li><li style="box-sizing: border-box; padding: 0px 5px;">222</li><li style="box-sizing: border-box; padding: 0px 5px;">223</li><li style="box-sizing: border-box; padding: 0px 5px;">224</li><li style="box-sizing: border-box; padding: 0px 5px;">225</li><li style="box-sizing: border-box; padding: 0px 5px;">226</li><li style="box-sizing: border-box; padding: 0px 5px;">227</li><li style="box-sizing: border-box; padding: 0px 5px;">228</li><li style="box-sizing: border-box; padding: 0px 5px;">229</li><li style="box-sizing: border-box; padding: 0px 5px;">230</li><li style="box-sizing: border-box; padding: 0px 5px;">231</li><li style="box-sizing: border-box; padding: 0px 5px;">232</li><li style="box-sizing: border-box; padding: 0px 5px;">233</li><li style="box-sizing: border-box; padding: 0px 5px;">234</li><li style="box-sizing: border-box; padding: 0px 5px;">235</li><li style="box-sizing: border-box; padding: 0px 5px;">236</li><li style="box-sizing: border-box; padding: 0px 5px;">237</li><li style="box-sizing: border-box; padding: 0px 5px;">238</li><li style="box-sizing: border-box; padding: 0px 5px;">239</li><li style="box-sizing: border-box; padding: 0px 5px;">240</li><li style="box-sizing: border-box; padding: 0px 5px;">241</li><li style="box-sizing: border-box; padding: 0px 5px;">242</li><li style="box-sizing: border-box; padding: 0px 5px;">243</li><li style="box-sizing: border-box; padding: 0px 5px;">244</li><li style="box-sizing: border-box; padding: 0px 5px;">245</li><li style="box-sizing: border-box; padding: 0px 5px;">246</li><li style="box-sizing: border-box; padding: 0px 5px;">247</li><li style="box-sizing: border-box; padding: 0px 5px;">248</li><li style="box-sizing: border-box; padding: 0px 5px;">249</li><li style="box-sizing: border-box; padding: 0px 5px;">250</li><li style="box-sizing: border-box; padding: 0px 5px;">251</li><li style="box-sizing: border-box; padding: 0px 5px;">252</li><li style="box-sizing: border-box; padding: 0px 5px;">253</li><li style="box-sizing: border-box; padding: 0px 5px;">254</li><li style="box-sizing: border-box; padding: 0px 5px;">255</li><li style="box-sizing: border-box; padding: 0px 5px;">256</li><li style="box-sizing: border-box; padding: 0px 5px;">257</li><li style="box-sizing: border-box; padding: 0px 5px;">258</li><li style="box-sizing: border-box; padding: 0px 5px;">259</li><li style="box-sizing: border-box; padding: 0px 5px;">260</li><li style="box-sizing: border-box; padding: 0px 5px;">261</li><li style="box-sizing: border-box; padding: 0px 5px;">262</li><li style="box-sizing: border-box; padding: 0px 5px;">263</li><li style="box-sizing: border-box; padding: 0px 5px;">264</li><li style="box-sizing: border-box; padding: 0px 5px;">265</li><li style="box-sizing: border-box; padding: 0px 5px;">266</li><li style="box-sizing: border-box; padding: 0px 5px;">267</li><li style="box-sizing: border-box; padding: 0px 5px;">268</li><li style="box-sizing: border-box; padding: 0px 5px;">269</li><li style="box-sizing: border-box; padding: 0px 5px;">270</li><li style="box-sizing: border-box; padding: 0px 5px;">271</li><li style="box-sizing: border-box; padding: 0px 5px;">272</li><li style="box-sizing: border-box; padding: 0px 5px;">273</li><li style="box-sizing: border-box; padding: 0px 5px;">274</li><li style="box-sizing: border-box; padding: 0px 5px;">275</li><li style="box-sizing: border-box; padding: 0px 5px;">276</li><li style="box-sizing: border-box; padding: 0px 5px;">277</li><li style="box-sizing: border-box; padding: 0px 5px;">278</li><li style="box-sizing: border-box; padding: 0px 5px;">279</li><li style="box-sizing: border-box; padding: 0px 5px;">280</li><li style="box-sizing: border-box; padding: 0px 5px;">281</li><li style="box-sizing: border-box; padding: 0px 5px;">282</li><li style="box-sizing: border-box; padding: 0px 5px;">283</li><li style="box-sizing: border-box; padding: 0px 5px;">284</li><li style="box-sizing: border-box; padding: 0px 5px;">285</li><li style="box-sizing: border-box; padding: 0px 5px;">286</li><li style="box-sizing: border-box; padding: 0px 5px;">287</li><li style="box-sizing: border-box; padding: 0px 5px;">288</li><li style="box-sizing: border-box; padding: 0px 5px;">289</li><li style="box-sizing: border-box; padding: 0px 5px;">290</li><li style="box-sizing: border-box; padding: 0px 5px;">291</li><li style="box-sizing: border-box; padding: 0px 5px;">292</li><li style="box-sizing: border-box; padding: 0px 5px;">293</li><li style="box-sizing: border-box; padding: 0px 5px;">294</li><li style="box-sizing: border-box; padding: 0px 5px;">295</li><li style="box-sizing: border-box; padding: 0px 5px;">296</li><li style="box-sizing: border-box; padding: 0px 5px;">297</li><li style="box-sizing: border-box; padding: 0px 5px;">298</li><li style="box-sizing: border-box; padding: 0px 5px;">299</li><li style="box-sizing: border-box; padding: 0px 5px;">300</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; pa
相關推薦
Java---基於TCP協議的相互即時通訊小程式
這是幾年前,新浪的一個面試題~要求是3天之內實現~ 通過TCP 協議,建立一個伺服器端。 通過配置伺服器端的IP和埠: 客戶端之間就可以相互通訊~ 上線了全部線上使用者會收到你上線的通知。 下線了全部的線上使用者會收到你下線的通知! 可以私
JAVA——基於TCP協議的文字聊天程式
基於TCP協議的文字聊天程式 1.要求 使用JAVA完成一個基於TCP協議的文字聊天程式,建立TCP客戶端套接字,分別為服務端和客戶端,並使兩端可以互聯。 2.TCP協議 在TCP協議中,有伺服器端(Server端)和客戶端(Client端)的概念。TCP傳輸資料的
基於TCP協議的Socket通訊案例
客戶端程式碼 import java.io.IOException; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; public class Main {
Java基於TCP協議的Socket客戶端檔案上傳與下載
import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.F
基於TCP協議的Socket通訊 實現使用者登入 以及服務端的相應
服務端 package com.example; import java.io.BufferedReader; import java.io.IOException; import java.
JAVA——基於TCP/IP網路的文字聊天程式
基於TCP/IP網路的文字聊天程式 1.要求 使用JAVA完成一個基於TCP/IP網路的文字聊天程式,建立TCP客戶端套接字,分別為服務端和客戶端,並使兩端可以互聯。 2.TCP協議 在TCP協議中,有伺服器端(Server端)和客戶端(Client端)的概念
linux下C語言程式設計日誌(1):基於TCP協議的伺服器/客戶端程式
基於TCP協議的伺服器/客戶端程式 首先我們看一下使用TCP協議進行網路通訊的程式基本模型:伺服器首先進行初始化操作:呼叫函式socket建立一個套接字,函式bind將這個套接字與伺服器的公認地址繫結在一起,函式listen將這個套接字換成傾聽套接字,然後呼叫函式acc
基於TCP協議的伺服器/客戶端程式
作為傳輸層的主要協議,TCP協議不僅可以支援本地的資料通訊,還可以支援跨網路的程序間通訊。 在偌大的網際網路中,我們可以通過“IP地址+端⼜號”標識網際網路中唯一的一個程序。然而,“IP地址+端⼜號”就稱為socket,這就是網路socket程式設計。 在TCP協議中,建⽴
python內網即時通訊小程式
根據TCP協議,使用python在兩程序之間建立通訊,來模擬一個即時通訊,類似於QQ環境:kali 一個server.py和client.pysever.py# -*- coding: utf-8 -* #Server.py import socket host = soc
mqtt協議實現即時通訊-activemq nginx.支援JS,JAVA,微信小程式
MQTT協議通訊 簡述: 使用MQTT協議實現後臺推送、及時通訊等功能。本案例實現了web-js端、微信小程式端、Java client端、Java serv
基於TCP協議的通訊(基於Java語言)
端與端通訊,經常由客戶端和服務端兩者組成,其中客戶端傳送請求給服務端,而服務端則響應請求。這兩者的通訊可以通過Socket來實現兩端的資料傳輸。其中Java自帶的Socket類可以建立客戶端socket,而ServerSocket可以建立一
[原始碼和文件分享]使用同一資料庫基於TCP Socket和Websocket實現的相互即時通訊系統
摘 要 隨著網路通訊和計算機技術的發展,人們越來越希望能夠即時傳送和接收網際網路訊息。與此同時隨著網際網路的發展在HTML5中提出了websocket協議,能更好的節省伺服器資源和頻寬並且伺服器和瀏覽器能夠雙向實時通訊。為了能讓使用者體驗傳統客戶端和web帶來的即時通訊結合的超爽體驗,本次畢業設
基於TCP協議的點對點通訊java程式,帶圖形介面
客戶端 package client; import java.awt.*; import java.awt.event.*; import java.io.DataInputStream; import java.io.DataOutputStream;
Java:基於TCP協議網路socket程式設計(實現C/S通訊)
目錄 一、前言:TCP原理簡介 二、Socket程式設計通訊 三、TCP伺服器端(具體程式碼) 四、TCP客戶端(具體程式碼) 五、通訊效果演示 六、“創意”機器人:價值一個億的AI核心程式碼(具體程式碼) 七、最後 一、前言:TCP原理簡介 首先,保證文章完整性,TC
基於TCP協議實現Linux下客戶端與伺服器之間的通訊,實現多執行緒、多程序伺服器
TCP是TCP/IP協議族中一個比較重要的協議,這是一種可靠、建立連結、面向位元組流的傳輸,工作在傳輸層。和TCP相對的不可靠、無連結、面向資料報的協議UDP,瞭解UDP客戶端與伺服器之間通訊請戳UDP協議實現的伺服器與客戶端通訊 TCP協議建立連線 首
【tcp-ip學習總結】使用tcp協議實現簡單的通訊小程式
環境:vs2013 server.c #include<Winsock2.h> #include<stdio.h> #pragma comment(lib,"Ws2_32.lib") #include<stdlib.h&
Java 基於TCP/IP 實現簡單的 socket 通訊
Server.java package com.learn; import java.io.BufferedReader; import java.io.IOException; import
【Java】基於TCP協議多執行緒伺服器-客戶端互動控制檯聊天室簡例
前兩天想到一個手機APP專案,使用到藍芽,發現BluetoothSocket和J2EE網路變成的Socket差不多,使用之餘順手寫一個多執行緒伺服器與客戶端互動實現聊天室的一個小例子,方便新人學習網路程式設計模組,期間使用到多執行緒和IO輸入輸出流的
基於websocket的網頁即時通訊(可傳附件圖片塗鴉、最小化狀態通知).NET,winform客戶端、服務端
公司網站需要即時通訊,就研究了下主要以下功能:websocket通訊,網頁端即時通訊,可以傳送表情,可以傳送附件,可以塗鴉,可以實現客服一對多聊天,winform做服務端負責收發,notification提醒,一番百度下來發現websocket做客戶端+superwebsoc
JAVA網路程式設計實現基於TCP協議的時間服務(向伺服器查詢當前時間)
服務端程式: package doc_02_tcp.server; import java.io.PrintWriter; import java.net.InetAddress; import