




免費(fèi)預(yù)覽已結(jié)束,剩余34頁可下載查看
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
計(jì)算機(jī)網(wǎng)絡(luò)通信課程設(shè)計(jì)課程設(shè)計(jì) 設(shè)計(jì)題目 聊天程序 課程編號 1610033 學(xué)生姓名李財(cái)錫 劉偉鎮(zhèn) 黃小敏 巫秋燕 學(xué) 號 所在專業(yè)計(jì)算機(jī)網(wǎng)絡(luò)技術(shù) 所在班級084 指導(dǎo)教師劉思鳳 成績 設(shè)計(jì)時(shí)間 1 月 4 日至 2010 年 1 月 8 日 目 錄 目目 錄錄 采用了 WORD 的目錄功能 請同學(xué)查閱 WORD 的標(biāo)題和目錄使用方法 以便正確 生成目錄 模板中的分頁符也不得隨意刪除 以免造成頁眉和頁碼的錯(cuò)誤 設(shè)計(jì)總說明 1 設(shè)計(jì)總說明 I 1系統(tǒng)功能介紹系統(tǒng)功能介紹 1 1 11 1建立偵聽服務(wù) 建立偵聽服務(wù) 1 1 21 2 用戶登錄用戶登錄 1 1 3 收發(fā)聊天信息 1 41 4收發(fā)系統(tǒng)消息收發(fā)系統(tǒng)消息 3 1 51 5 用戶下線和通知用戶下線和通知 3 1 6服務(wù)關(guān)閉服務(wù)關(guān)閉 3 2 代碼實(shí)現(xiàn)代碼實(shí)現(xiàn) 3 2 1 用戶信息鏈表實(shí)現(xiàn) 2 2服務(wù)器主框類架的服務(wù)器主框類架的實(shí)現(xiàn)實(shí)現(xiàn) 5 2 3 服務(wù)器偵聽線程類的實(shí)現(xiàn)服務(wù)器偵聽線程類的實(shí)現(xiàn) 6 2 42 4服務(wù)器接收線程類的實(shí)現(xiàn)服務(wù)器接收線程類的實(shí)現(xiàn) 6 2 5 客戶端主框類的客戶端主框類的實(shí)現(xiàn)實(shí)現(xiàn) 7 2 6 客戶端接收線程類的實(shí)現(xiàn) 3附 源程序附 源程序 8 參考文獻(xiàn) 9 I 設(shè)計(jì)總說明 這里主要用了 java 的網(wǎng)絡(luò)編程相關(guān)知識 通過學(xué)習(xí)了解了 TCP 和 UDP 協(xié)議的基本 知識和 Socket 編程的基本概念 以及 TCP 協(xié)議的響應(yīng)流程 現(xiàn)在通過使用 java 提供 的流 Socket 和數(shù)據(jù)包 Socket 利用流 Socket 和數(shù)據(jù)包 Socket 建立通信連接 最后實(shí) 現(xiàn)了一個(gè)完整的聊天程序 該聊天系統(tǒng)是基于 TCP 協(xié)議的流 Socket 實(shí)現(xiàn)的 它采用了 典型的 C S 架構(gòu) 包括一個(gè)服務(wù)器應(yīng)用程序和一個(gè)客戶端程序 可以同時(shí)供多個(gè)用戶 進(jìn)行聊天 課程設(shè)計(jì)要求 1 登錄功能 客戶端登錄到聊天服務(wù)器 服務(wù)器管理所有登錄的客戶 并將客戶列 表發(fā)送給各個(gè)客戶顯示 2 客戶可以通過服務(wù)器轉(zhuǎn)發(fā) 實(shí)現(xiàn)一對一和多對多聊天 3 實(shí)現(xiàn)呼叫功能 當(dāng)客戶端程序連接服務(wù)器時(shí) 通過服務(wù)器搜索所要呼叫的客戶 如果檢測到此用戶且該用戶正處于聯(lián)網(wǎng)狀態(tài) 則服務(wù)器通知此用戶的客戶端程序響應(yīng) 主叫方客戶端程序 然后在主叫方和被叫方建立連接后 雙方就可以聊天或進(jìn)行其它 的通信 4 客戶端程序應(yīng)該可以實(shí)時(shí)顯示目前其它用戶的狀態(tài) 例如好友信息 上 下線 網(wǎng)絡(luò)系統(tǒng)集成課程結(jié)業(yè)論文 報(bào)告 1 編寫聊天程序 1系統(tǒng)功能介紹系統(tǒng)功能介紹 本程序具有完整的會話功能 采用 C S 結(jié)構(gòu) 由兩部分組成 客戶端和服務(wù)器 可 以供多個(gè)用戶進(jìn)行聊天 服務(wù)器具有建立偵聽服務(wù) 轉(zhuǎn)發(fā)聊天信息 響應(yīng)用戶下線 發(fā)送系統(tǒng)消息的功能 要實(shí)現(xiàn)這四個(gè)功能 需要建立以下四個(gè)類 1 用戶信息鏈表 2 服務(wù)器主框類 3 服務(wù)器偵聽線程類 4 服務(wù)器接收線程類 客戶端具有請求登錄 請求下線 發(fā)送聊天信息 接收聊天信息 接收系統(tǒng)信息的 功能 要實(shí)現(xiàn)這些功能也要建立兩個(gè)類 客戶端主框架類和客戶端接收線程類 下面通過聊天系統(tǒng)的演示來具體了解聊天系統(tǒng)的功能 1 11 1建立偵聽服務(wù) 建立偵聽服務(wù) 初始狀態(tài)的服務(wù)器如圖 1 所示 其主框架上具有兩個(gè)按鈕 分別用于啟動和 關(guān)閉服務(wù)器 點(diǎn)擊 啟動 按鈕處于允許狀態(tài) 點(diǎn)擊 關(guān)閉 按鈕處于禁止?fàn)顟B(tài) 服務(wù)器啟 動后 就進(jìn)入偵聽狀態(tài) 關(guān)閉就結(jié)束偵聽 這里設(shè)計(jì)服務(wù)在 8000 端口啟動 圖 1 圖 2 1 21 2 用戶登錄用戶登錄 用戶登錄過程其實(shí)就是客服端請求登錄和服務(wù)器響應(yīng)登錄兩個(gè)過程 初始狀態(tài)的 網(wǎng)絡(luò)系統(tǒng)集成課程結(jié)業(yè)論文 報(bào)告 2 客戶端如圖 3 它的 上線 按鈕用于用戶登錄 下線 按鈕用于用戶下線 界面 下方的 JTextField 控件 在用戶未登錄時(shí)輸入用戶名稱 在用戶登錄后用于輸入聊天信 息 并按回車確定操作 圖 3 1 3收發(fā)聊天信息收發(fā)聊天信息 這個(gè)過程包含了客戶端發(fā)送聊天信息和服務(wù)器轉(zhuǎn)發(fā)聊天信息 當(dāng)客戶端登錄后與 其他在線用戶進(jìn)行聊天 只要在界面最底部的 JTextField 控件中輸入聊天信息 按回車 鍵就可以發(fā)送出去了 而那兩個(gè) JComboBox 控件分別用來選擇聊天信息的對象和發(fā)送 聊天信息的表情動作 悄悄話 復(fù)選框用來標(biāo)識此信息是否轉(zhuǎn)發(fā)給非目標(biāo)對象的其他 在線用戶 1 41 4收發(fā)系統(tǒng)消息收發(fā)系統(tǒng)消息 系統(tǒng)信息的收發(fā)是單方面的 只能有服務(wù)器發(fā)給客戶端 在服務(wù)器主界面輸入 系統(tǒng)信息 按回車鍵 系統(tǒng)就會將信息發(fā)送到所有客戶端 客戶端收到信息后 網(wǎng)絡(luò)系統(tǒng)集成課程結(jié)業(yè)論文 報(bào)告 3 就會顯示在界面上 如下圖 1 51 5用戶下線和通知用戶下線和通知 用戶下線與用戶登錄一樣 要涉及到客戶端請求下線和服務(wù)器響應(yīng)請求兩個(gè)過程 用戶單擊客戶端 下線 按鈕 客戶端就會向服務(wù)器發(fā)送下線請求 服務(wù)器接收 后就胡釋放與該用戶的相關(guān)資源 并向其他用戶發(fā)送該用戶下線通知以及更新在線用 戶列表 如下圖 網(wǎng)絡(luò)系統(tǒng)集成課程結(jié)業(yè)論文 報(bào)告 4 1 6 服務(wù)關(guān)閉服務(wù)關(guān)閉 當(dāng)服務(wù)器關(guān)閉服務(wù)時(shí) 就向所有在線用戶發(fā)送服務(wù)器關(guān)閉信息 并且關(guān)閉所有與 在線用戶相連接的輸入輸出流和套接字 將且將界面調(diào)整到最初狀態(tài) 如圖 客服端接收到服務(wù)器的關(guān)閉信息時(shí) 它將在界面上顯示服務(wù)器關(guān)閉的提示信息 并且關(guān)閉與服務(wù)相連的輸入輸出流和套接字 刪除在線用戶列表 將界面調(diào)整到最初 狀態(tài) 2 代碼實(shí)現(xiàn)代碼實(shí)現(xiàn) 2 1用戶信息鏈表的實(shí)現(xiàn)用戶信息鏈表的實(shí)現(xiàn) 用戶信息鏈表類 UserInfoList 需要對實(shí)現(xiàn)對用戶信息的添加 刪除和查詢功能 在實(shí)現(xiàn) UserInfoList 類之前 先定義一個(gè)用于描述單個(gè)用戶信息的數(shù)據(jù)結(jié)構(gòu) Node 類 并定義五個(gè)數(shù)據(jù)成員 用戶昵稱 套接字 輸入流 輸出流和 Next 指針 public class Node String username null 用戶昵稱 Socket socket null 套接字 ObjectOutputStream output null 輸出流 ObjectInputStream input null 輸入流 Node next null 保存鏈表中下一個(gè)節(jié)點(diǎn)的指針 在UserInfoList 的構(gòu)造函數(shù)中 對 UserInfoList 的成員變量進(jìn)行初始化 public UserInfoList root new Node root next null 創(chuàng)建鏈表的節(jié)點(diǎn) pointer null count 0 為UserInfoList類添加成員函數(shù)add完成用戶信息節(jié)點(diǎn)的添加功能 public void add Node n pointer root 將游標(biāo)指向根節(jié)點(diǎn) 網(wǎng)絡(luò)系統(tǒng)集成課程結(jié)業(yè)論文 報(bào)告 5 while pointer next null 判斷是否鏈表尾部 pointer pointer next 將游標(biāo)后移 pointer next n 加入節(jié)點(diǎn) n next null count 計(jì)數(shù)器加一 為UserInfoList類添加成員函數(shù)del 完成用戶信息節(jié)點(diǎn)的刪除功能 public void del Node n pointer root 將游標(biāo)指向根節(jié)點(diǎn) while pointer next null 判斷是否鏈表尾部 if pointer next n 判斷當(dāng)前節(jié)點(diǎn)是否為要?jiǎng)h除節(jié)點(diǎn) pointer next n next 刪除節(jié)點(diǎn) count 計(jì)數(shù)器減一 break pointer pointer next 將游標(biāo)指向下一節(jié)點(diǎn) 為UserInfoList類添加成員函數(shù)getCount 提供返回鏈表中元素個(gè)數(shù)的功能 public int getCount return count 返回當(dāng)前鏈表中元素的個(gè)數(shù) 為UserInfoList類添加成員函數(shù)find 完成在用戶信息鏈表中查找指定元素的功能 為方 便起見 定義兩個(gè)find函數(shù) 入口參數(shù)分別為String和int public Node find String username if count 0 return null pointer root while pointer next null pointer pointer next if pointer username equalsIgnoreCase username return pointer return null public Node find int index if count 0 return null if index 0 return null 網(wǎng)絡(luò)系統(tǒng)集成課程結(jié)業(yè)論文 報(bào)告 6 pointer root int i 0 while i index 1 if pointer next null pointer pointer next else return null i return pointer 2 2 服務(wù)器主框類架的實(shí)現(xiàn)服務(wù)器主框類架的實(shí)現(xiàn) 服務(wù)器主框類架類需要實(shí)現(xiàn)人機(jī)交互功能 啟動服務(wù)功能 關(guān)閉服務(wù)功能和發(fā)送系統(tǒng)信息功能 需要為工程添加服務(wù)器主框類 名命為 ServerFrame 在 ServerFrame 類的構(gòu)造函數(shù)中 完成為服務(wù)器控件的添加 控件位置 的調(diào)整 控件的初始狀態(tài)的設(shè)定及控件事件偵聽函數(shù)的添加 添加函數(shù) init 在其中完 成界面的初始化功能 在 init 函數(shù)中 除對事件進(jìn)行添加和調(diào)整外 還對控件添加事 件偵聽器 如為關(guān)閉服務(wù)按鈕添加偵聽器 jbStopServer addActionListener new ActionListener public void actionPerformed ActionEvent e stopServer 在 ServerFrame 中添加 starrServer 函數(shù)完成啟動服務(wù)的功能 添加 stopServer 函數(shù)完成 關(guān)閉服務(wù)的功能 添加 sendStopToAll 函數(shù)依次向所有在線用戶發(fā)送服務(wù)器關(guān)閉消息 添加 sendSystemMessage 函數(shù)實(shí)現(xiàn)發(fā)送系統(tǒng)消息的功能 添加 sendMsgToAll 函數(shù)完成 向所有在線用戶發(fā)送系統(tǒng)消息的功能 2 3服務(wù)器偵聽線程類的實(shí)現(xiàn)服務(wù)器偵聽線程類的實(shí)現(xiàn) 服務(wù)器偵聽線程類需要偵聽客戶端的登錄請求 并且為該用戶啟動專用線程 處理 與此用戶相關(guān)的操作 首先為工程添加 ServerListenThread 類 繼承其父類 Thread 在 這個(gè)類中實(shí)現(xiàn)偵聽客戶端連接 并響應(yīng)用戶登錄的功能 在這個(gè)類中最重要的功能是 網(wǎng)絡(luò)系統(tǒng)集成課程結(jié)業(yè)論文 報(bào)告 7 在 run 函數(shù)中實(shí)現(xiàn)的 當(dāng)服務(wù)器啟動時(shí) 就會建立偵聽線程 并且調(diào)用 start 函數(shù)開始 運(yùn)行線程 public void run 服務(wù)器偵聽線程的執(zhí)行體 while isStop 創(chuàng)建用戶信息節(jié)點(diǎn) client socket server accept 偵聽客戶端連接 client output newObjectOutputStream client socket getOutputStream client output flush 得到套接字輸入輸出流 client input newObjectInputStream client socket getInputStream client username String client input readObject combobox addItem client username 讀取用戶昵稱 userInfoList add client textarea append user client username 上線 n 顯示提示信息 textfield setText 在線用戶 userInfoList getCount 人 n recvThread new ServerReceiveThread textarea textfield combobox client userInfoList recvThread start 啟動線程 2 42 4服務(wù)器接收線程類的實(shí)現(xiàn)服務(wù)器接收線程類的實(shí)現(xiàn) 服務(wù)器接收線程類需要接收客戶端發(fā)來的信息 并且對該信息進(jìn)行分析 執(zhí)行 相應(yīng)操作 當(dāng)服務(wù)器收到客戶端發(fā)來的聊天信息 對發(fā)送對象進(jìn)行判斷 如果發(fā)送 對象是所有用戶 則將信息發(fā)給所有用戶 同理 如果是特定用戶就發(fā)給特定用戶 當(dāng)服務(wù)器接收到客戶端發(fā)來的下線請求時(shí) 就關(guān)閉所有與該用戶相關(guān)的輸入輸出流 和套接字 并將該用戶從在線列表中刪除 此外 還需向所有在線用戶發(fā)送該用戶 下線的通知和重發(fā)最新的列表 首先為工程添加新類 命名為ServerReceiveThread 實(shí)現(xiàn)接收客戶端信息的功能 方法和偵聽線程差不多 它的成員函數(shù)sendToAll實(shí)現(xiàn)向所有在線用戶轉(zhuǎn)發(fā)聊天信息 的功能 另一個(gè)成員函數(shù)sendUserList實(shí)現(xiàn)向所有在線用戶發(fā)送用戶列表的功能 public void sendToAll String msg int count userInfoList getCount int i 0 while i count Node node userInfoList find i 網(wǎng)絡(luò)系統(tǒng)集成課程結(jié)業(yè)論文 報(bào)告 8 if node null i continue try node output writeObject 聊天信息 node output flush node output writeObject msg node output flush 2 5客戶端主框類的實(shí)現(xiàn)客戶端主框類的實(shí)現(xiàn) 客戶端主框架類實(shí)現(xiàn)了人機(jī)交互 用戶登錄 用戶下線和聊天信息的發(fā)送功能 客戶端的主框架類為 ClientFrame 其構(gòu)造函數(shù)完成為客戶端控件的添加 控件位置的 調(diào)整 控件初始狀態(tài)的設(shè)定以及控件事件偵聽函數(shù)的添加 成員函數(shù) LogIn 完成用戶 登錄功能 LogOut 完成用戶下線功能 SendMessage 用于發(fā)送聊天信息 public void SendMessage String toSomebody combobox getSelectedItem toString String status if checkbox isSelected status 悄悄話 String action actionlist getSelectedItem toString String message textfield getText if socket isClosed return try output writeObject 聊天信息 發(fā)送聊天信息標(biāo)識 output flush output writeObject toSomebody 發(fā)送聊天對象 output flush output writeObject status 發(fā)送聊天狀態(tài) output flush output writeObject action 發(fā)送聊天表情 output flush output writeObject message 發(fā)送聊天信息內(nèi)容 output flush 網(wǎng)絡(luò)系統(tǒng)集成課程結(jié)業(yè)論文 報(bào)告 9 2 62 6客戶端接收線程類的實(shí)現(xiàn)客戶端接收線程類的實(shí)現(xiàn) 客戶端接收線程類需要接收服務(wù)器發(fā)來的信息 并對該信息進(jìn)行分析 執(zhí)行相應(yīng) 的操作 客戶端收到的服務(wù)器信息有三種 聊天信息和系統(tǒng)信息 用戶列表信息 服務(wù)關(guān)閉信息 客戶端將接收到聊天信息和系統(tǒng)信息顯示在界面上 接收到用戶信息 就對信息進(jìn)行分析 獲取在線用戶列表 顯示在界面組合框中 接收到關(guān)閉信息 則 執(zhí)行關(guān)閉操作 命名客戶端接收線程類為 ClientReceiveThread 實(shí)現(xiàn)接收服務(wù)器信息的功能 主 要在 run 函數(shù)中實(shí)現(xiàn)該類的功能 public void run 線程執(zhí)行體 sendUserList while isStop 讀取服務(wù)器發(fā)送的聊天信息 if type equalsIgnoreCase 聊天信息 處理聊天信息 String toSomebody String client input readObject 獲取系統(tǒng)信息內(nèi)容 String status String client input readObject String action String client input readObject String message String client input readObject String msg client username action 對 toSomebody 說 message n 顯示信息 3附 源程序附 源程序 附 錄 10 參考文獻(xiàn) Java 語言與面向?qū)ο蟪绦蛟O(shè)計(jì) Java 編程案例解析 計(jì)算機(jī)網(wǎng)絡(luò)通信教程 附 錄 附 錄 11 宋體小四號 1 25 倍行距 要求 附錄的有無根據(jù)情況需要而定 內(nèi)容一般包 括正文內(nèi)不便列出的冗長公式推導(dǎo) 符號說明 含縮寫 計(jì)算機(jī)程序 圖紙 表格等 附錄中有程序源代碼的因篇幅限制可酌情考慮內(nèi)容的字號 用戶信息鏈表類 import import java io public class Node String username null Socket socket null ObjectOutputStream output null ObjectInputStream input null Node next null import javax swing import import java io public class UserInfoList Node root Node pointer int count public UserInfoList root new Node root next null pointer null count 0 public void add Node n pointer root 附 錄 12 while pointer next null pointer pointer next pointer next n n next null count public void del Node n pointer root while pointer next null if pointer next n pointer next n next count break pointer pointer next public int getCount return count public Node find String username if count 0 return null pointer root while pointer next null pointer pointer next if pointer username equalsIgnoreCase username 附 錄 13 return pointer return null public Node find int index if count 0 return null if index 0 return null pointer root int i 0 while i index 1 if pointer next null pointer pointer next else return null i return pointer 服務(wù)端主框架類 import java awt import java awt event 附 錄 14 import javax swing import javax swing event import javax swing border import java io import public class ServerFrame extends JFrame private JButton jbStartServer private JButton jbStopServer private JButton b1 private JScrollPane js private Border border private ServerSocket serverSocket private JComboBox combobox private JTextArea textarea private JTextField textfield private JTextField systemMsg private UserInfoList userInfoList private ServerListenThread listenThread public ServerFrame super 服務(wù)端 init setSize 370 570 show public void init 初始化 Border bevelBorder BorderFactory createBevelBorder BevelBorder RAISED Color white Color white Color white Color white Border emptyBorder BorderFactory createEmptyBorder 1 1 1 1 border BorderFactory createCompoundBorder bevelBorder emptyBorder jbStartServer new JButton 啟動 jbStopServer new JButton 關(guān)閉 附 錄 15 b1 new JButton 發(fā)送 jbStopServer setEnabled false jbStartServer addActionListener new ActionListener public void actionPerformed ActionEvent e startServer jbStopServer addActionListener new ActionListener public void actionPerformed ActionEvent e stopServer combobox new JComboBox combobox insertItemAt 所有人 0 combobox setSelectedIndex 0 textarea new JTextArea 20 20 textarea setEditable false js new JScrollPane textarea textfield new JTextField 40 textfield setEditable false systemMsg new JTextField 40 systemMsg setEnabled false systemMsg addActionListener new ActionListener 附 錄 16 public void actionPerformed ActionEvent e sendSystemMessage this addWindowListener new WindowAdapter public void windowClosing WindowEvent e stopServer System exit 0 Container c getContentPane c setLayout null jbStartServer setBorder border jbStartServer setBounds 30 30 90 30 jbStopServer setBorder border jbStopServer setBounds 150 30 90 30 textfield setBounds 30 75 100 20 js setBounds 30 110 300 300 combobox setBounds 30 425 90 30 systemMsg setBounds 30 470 200 50 b1 setBounds 250 470 60 50 c add b1 null c add jbStartServer null c add jbStopServer null c add textfield null c add js null c add combobox null 附 錄 17 c add systemMsg null public void startServer 啟動服務(wù) try serverSocket new ServerSocket 8000 10 textarea append 服務(wù)在 8000 端口啟動 n jbStartServer setEnabled false jbStopServer setEnabled true systemMsg setEnabled true catch Exception e userInfoList new UserInfoList listenThread new ServerListenThread serverSocket combobox textarea textfield userInfoList listenThread start public void stopServer 實(shí)現(xiàn)關(guān)閉服務(wù)功能 try sendStopToAll 向所有在線用戶發(fā)送關(guān)閉信息 listenThread isStop true serverSocket close int count userInfoList getCount 附 錄 18 int i 0 while i count Node node userInfoList find i node input close node output close node socket close i jbStartServer setEnabled true jbStopServer setEnabled false systemMsg setEnabled false combobox removeAllItems combobox addItem 所有人 catch Exception e public void sendStopToAll 向所有在線用戶發(fā)送服務(wù)關(guān)閉消息 int count userInfoList getCount int i 0 while i count Node node userInfoList find i if node null i continue try 附 錄 19 node output writeObject 服務(wù)關(guān)閉 node output flush catch Exception e i public void sendSystemMessage 實(shí)現(xiàn)發(fā)送系統(tǒng)消息功能 String toSomebody combobox getSelectedItem toString String message systemMsg getText if toSomebody equalsIgnoreCase 所有人 sendMsgToAll message n else Node node userInfoList find toSomebody try node output writeObject 系統(tǒng)信息 node output flush node output writeObject message n node output flush catch Exception e 附 錄 20 public void sendMsgToAll String msg int count userInfoList getCount int i 0 while i count Node node userInfoList find i if node null i continue try node output writeObject 系統(tǒng)信息 node output flush node output writeObject msg node output flush catch Exception e i public static void main String args new ServerFrame 服務(wù)器偵聽線程類 import java awt 附 錄 21 import java awt event import javax swing import javax swing event import java io import public class ServerListenThread extends Thread ServerSocket server JComboBox combobox JTextArea textarea JTextField textfield UserInfoList userInfoList Node client ServerReceiveThread recvThread public boolean isStop public ServerListenThread ServerSocket server JComboBox combobox JTextArea textarea JTextField textfield UserInfoList userInfoList this server server bobox combobox this textarea textarea this textfield textfield this userInfoList userInfoList isStop false 附 錄 22 public void run while isStop client socket server accept client output new ObjectOutputStream client socket getOutputStream client output flush client input new ObjectInputStream client socket getInputStream client username String client input readObject combobox addItem client username userInfoList add client textarea append user client username 上線 n textfield setText 在線用戶 userInfoList getCount 人 n recvThread new ServerReceiveThread textarea textfield combobox client userInfoList recvThread start catch Exception e 服務(wù)器接收線程類 import javax swing 附 錄 23 import java io import public class ServerReceiveThread extends Thread JTextArea textarea JTextField textfield JComboBox combobox Node client UserInfoList userInfoList public boolean isStop public ServerReceiveThread JTextArea textarea JTextField textfield JComboBox combobox Node client UserInfoList userInfoList this textarea textarea this textfield textfield this client client this userInfoList userInfoList bobox combobox isStop false public void run sendUserList while isStop 附 錄 24 if type equalsIgnoreCase 聊天信息 String toSomebody String client input readObject String status String client input readObject String action String client input readObject String message String client input readObject String msg client username action 對 toSomebody 說 message n if status equalsIgnoreCase 悄悄話 msg 悄悄話 msg textarea append msg if toSomebody equalsIgnoreCase 所有人 sendToAll msg else try client output writeObject 聊天信息 client output flush client output writeObject msg client output flush catch Exception e Node node userInfoList find toSomebody if node null 附 錄 25 node output writeObject 聊天信息 node output flush node output writeObject msg node output flush else if type equalsIgnoreCase 用戶下線 Node node userInfoList find client username userInfoList del node String msg 用戶 client username 下線 n int count userInfoList getCount combobox removeAllItems combobox addItem 所有人 int i 0 while i count node userInfoList find i if node null i continue combobox addItem node username i combobox setSelectedIndex 0 textarea append msg textfield setText 在線用戶 userInfoList getCount 人 n sendToAll msg sendUserList 附 錄 26 break catch Exception e public void sendToAll String msg int count userInfoList getCount int i 0 while i count Node node userInfoList find i if node null i continue try node output writeObject 聊天信息 node output flush node output writeObject msg node output flush catch Exception e i 附 錄 27 public void sendUserList String userlist int count userInfoList getCount int i 0 while i count Node node userInfoList find i if node null i continue userlist node username userlist n i i 0 while i count Node node userInfoList find i if node null i continue try node output writeObject 用戶列表 node output flush node output writeObject userlist node output flush 附 錄 28 catch Exception e i 客戶端主框架類 import java awt import java awt event import javax swing import javax swing event import javax swing border import java io import public class ClientFrame extends JFrame private JComboBox combobox private JTextArea textarea private JTextField textfield private JCheckBox checkbox private JComboBox actionlist private JButton login private JButton logout private Border border private JTextField seperater private JScrollPane js private JButton b1 Socket socket ObjectOutputStream output ObjectInputStream input ClientReceiveThread recvThread 附 錄 29 public ClientFrame super 客戶端 Border bevelBorder BorderFactory createBevelBorder BevelBorder RAISED Color white Color white Color white Color white Border emptyBorder BorderFactory createEmptyBorder 1 1 1 1 border BorderFactory createCompoundBorder bevelBorder emptyBorder login new JButton 上線 logout new JButton 下線 b1 new JButton 發(fā)送 logout setEnabled false combobox new JComboBox combobox addItem 所有人 combobox setSelectedIndex 0 textarea new JTextArea 20 20 textarea setEditable false js new JScrollPane textarea textfield new JTextField 請先輸入你的昵稱 再上線 20 seperater new JTextField 20 seperater setEditable false checkbox new JCheckBox 悄悄話 checkbox setSelected false actionlist new JComboBox actionlist addItem 微笑地 actionlist addItem 生氣地 actionlist addItem 小心地 actionlist setSelectedIndex 0 附 錄 30 login setBorder border login setBounds 30 30 90 30 logout setBorder border logout setBounds 150 30 90 30 seperater setBounds 30 75 300 20 js setBounds 30 110 300 300 combobox setBounds 30 425 90 30 actionlist setBounds 150 425 90 30 checkbox setBounds 270 425 90 30 textfield setBounds 30 470 200 50 b1 setBounds 250 470 60 50 Container c getContentPane c setLayout null c add b1 null c add login null c add logout null c add seperater null c add js null c add combobox null c add actionlist null c add checkbox null c add textfield null login addActionListener new ActionListener public void actionPerformed ActionEvent e LogIn logout addActionListener 附 錄 31 new ActionListener public void actionPerformed ActionEvent e LogOut b1 addActionListener new ActionListener public void actionPerformed ActionEvent e SendMessage textfield addActionListener new ActionListener public void actionPerformed ActionEvent e SendMessage this addWindowListener new WindowAdapter public void windowClosing WindowEvent e LogOut System exit 0 setSize 370 570 show 附 錄 32 public void LogIn try socket new Socket 192 168 2 145 8000 注意服務(wù)器 IP output new ObjectOutputStream socket getOutputStream output flush input new ObjectInputStream socket getInputStream output writeObject textfield getText output flush recvThread new
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 高效備考市政工程考試攻略試題及答案
- 高效執(zhí)行的2025年工程經(jīng)濟(jì)試題及答案
- 行政管理經(jīng)濟(jì)法課外閱讀材料試題及答案
- 零售行業(yè)智慧門店建設(shè)與管理方案
- 分享經(jīng)驗(yàn)2025年工程項(xiàng)目管理試題及答案
- 工程投資決策中的市場環(huán)境分析技巧試題及答案
- 應(yīng)用文寫作考試試題及答案
- 高效會議管理的策略計(jì)劃
- 加強(qiáng)自我學(xué)習(xí)與知識更新的途徑計(jì)劃
- 展會營銷與品牌推廣計(jì)劃
- 初中英語2023年中考專題訓(xùn)練任務(wù)型閱讀-完成表格篇
- 數(shù)據(jù)中臺-項(xiàng)目需求規(guī)格說明書
- 田徑運(yùn)動會檢查員報(bào)告表
- 高級政工師職稱面試題
- 老年人能力評估師高級第六章-需求評估
- 業(yè)主維權(quán)授權(quán)委托書范文
- 第四代EGFR-C797S藥物管線及專利調(diào)研報(bào)告
- 骨科基礎(chǔ)知識解剖篇
- 梁山伯與祝英臺小提琴譜樂譜
- 有機(jī)硅化學(xué)課件-有機(jī)硅化合物的化學(xué)鍵特性
- 蒸汽和飽和蒸汽熱焓表
評論
0/150
提交評論