![《計(jì)算機(jī)網(wǎng)絡(luò)通信 》課程設(shè)計(jì)報(bào)告_第1頁](http://file2.renrendoc.com/fileroot_temp3/2021-6/11/ebfee582-bfd2-418b-8b3d-170c1bfbb62b/ebfee582-bfd2-418b-8b3d-170c1bfbb62b1.gif)
![《計(jì)算機(jī)網(wǎng)絡(luò)通信 》課程設(shè)計(jì)報(bào)告_第2頁](http://file2.renrendoc.com/fileroot_temp3/2021-6/11/ebfee582-bfd2-418b-8b3d-170c1bfbb62b/ebfee582-bfd2-418b-8b3d-170c1bfbb62b2.gif)
![《計(jì)算機(jī)網(wǎng)絡(luò)通信 》課程設(shè)計(jì)報(bào)告_第3頁](http://file2.renrendoc.com/fileroot_temp3/2021-6/11/ebfee582-bfd2-418b-8b3d-170c1bfbb62b/ebfee582-bfd2-418b-8b3d-170c1bfbb62b3.gif)
![《計(jì)算機(jī)網(wǎng)絡(luò)通信 》課程設(shè)計(jì)報(bào)告_第4頁](http://file2.renrendoc.com/fileroot_temp3/2021-6/11/ebfee582-bfd2-418b-8b3d-170c1bfbb62b/ebfee582-bfd2-418b-8b3d-170c1bfbb62b4.gif)
![《計(jì)算機(jī)網(wǎng)絡(luò)通信 》課程設(shè)計(jì)報(bào)告_第5頁](http://file2.renrendoc.com/fileroot_temp3/2021-6/11/ebfee582-bfd2-418b-8b3d-170c1bfbb62b/ebfee582-bfd2-418b-8b3d-170c1bfbb62b5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、 計(jì)算機(jī)網(wǎng)絡(luò)通信課程設(shè)計(jì)課程設(shè)計(jì) 設(shè)計(jì)題目: 聊天程序 課程編號(hào): 學(xué)生姓名李財(cái)錫,劉偉鎮(zhèn),黃小敏,巫秋燕 學(xué) 號(hào) 所在專業(yè)計(jì)算機(jī)網(wǎng)絡(luò)技術(shù) 所在班級(jí)084 指導(dǎo)教師劉思鳳 成績 設(shè)計(jì)時(shí)間 1 月 4 日至 2010 年 1 月 8 日 目目 錄錄 (采用了 WORD 的目錄功能,請(qǐng)同學(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ā)
2、系統(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 設(shè)計(jì)總說明 這里主要用了 java 的網(wǎng)絡(luò)編程相關(guān)知識(shí),通過學(xué)習(xí)了解了 TCP 和 UDP 協(xié)議的基本 知識(shí)和 Socket 編程的基本概念,以及 TCP 協(xié)議的
3、響應(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)一對(duì)一和多對(duì)多聊天。 3 實(shí)現(xiàn)呼叫功能。當(dāng)客戶端程序連接服務(wù)器時(shí),通過服務(wù)器搜索所要呼叫的客戶, 如果檢測到此用戶
4、且該用戶正處于聯(lián)網(wǎng)狀態(tài),則服務(wù)器通知此用戶的客戶端程序響應(yīng) 主叫方客戶端程序,然后在主叫方和被叫方建立連接后,雙方就可以聊天或進(jìn)行其它 的通信。 4 客戶端程序應(yīng)該可以實(shí)時(shí)顯示目前其它用戶的狀態(tài)(例如好友信息,上、下線) 。 編寫聊天程序 1系統(tǒng)功能介紹系統(tǒng)功能介紹 本程序具有完整的會(huì)話功能,采用 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ù)器接收線程類。 客戶端具有請(qǐng)求登錄、
5、請(qǐng)求下線、發(fā)送聊天信息、接收聊天信息、接收系統(tǒng)信息的 功能。要實(shí)現(xiàn)這些功能也要建立兩個(gè)類客戶端主框架類和客戶端接收線程類。 下面通過聊天系統(tǒng)的演示來具體了解聊天系統(tǒng)的功能。 1.11.1建立偵聽服務(wù):建立偵聽服務(wù): 初始狀態(tài)的服務(wù)器如圖(1)所示,其主框架上具有兩個(gè)按鈕,分別用于啟動(dòng)和 關(guān)閉服務(wù)器。 點(diǎn)擊“啟動(dòng)”按鈕處于允許狀態(tài),點(diǎn)擊“關(guān)閉”按鈕處于禁止?fàn)顟B(tài);服務(wù)器啟 動(dòng)后,就進(jìn)入偵聽狀態(tài),關(guān)閉就結(jié)束偵聽,這里設(shè)計(jì)服務(wù)在 8000 端口啟動(dòng)。 圖(1) 圖(2) 1.21.2 用戶登錄用戶登錄 用戶登錄過程其實(shí)就是客服端請(qǐng)求登錄和服務(wù)器響應(yīng)登錄兩個(gè)過程。初始狀態(tài)的 客戶端如圖(3) ,它的“上
6、線”按鈕用于用戶登錄, “下線”按鈕用于用戶下線,界面 下方的 JTextField 控件,在用戶未登錄時(shí)輸入用戶名稱,在用戶登錄后用于輸入聊天信 息,并按回車確定操作 圖(3) 1.3收發(fā)聊天信息收發(fā)聊天信息 這個(gè)過程包含了客戶端發(fā)送聊天信息和服務(wù)器轉(zhuǎn)發(fā)聊天信息。當(dāng)客戶端登錄后與 其他在線用戶進(jìn)行聊天,只要在界面最底部的 JTextField 控件中輸入聊天信息,按回車 鍵就可以發(fā)送出去了,而那兩個(gè) JComboBox 控件分別用來選擇聊天信息的對(duì)象和發(fā)送 聊天信息的表情動(dòng)作, “悄悄話”復(fù)選框用來標(biāo)識(shí)此信息是否轉(zhuǎn)發(fā)給非目標(biāo)對(duì)象的其他 在線用戶。 1.41.4收發(fā)系統(tǒng)消息收發(fā)系統(tǒng)消息 系統(tǒng)信
7、息的收發(fā)是單方面的,只能有服務(wù)器發(fā)給客戶端,在服務(wù)器主界面輸入 系統(tǒng)信息,按回車鍵,系統(tǒng)就會(huì)將信息發(fā)送到所有客戶端,客戶端收到信息后 就會(huì)顯示在界面上。如下圖 1.51.5用戶下線和通知用戶下線和通知 用戶下線與用戶登錄一樣,要涉及到客戶端請(qǐng)求下線和服務(wù)器響應(yīng)請(qǐng)求兩個(gè)過程 用戶單擊客戶端“下線”按鈕,客戶端就會(huì)向服務(wù)器發(fā)送下線請(qǐng)求,服務(wù)器接收 后就胡釋放與該用戶的相關(guān)資源,并向其他用戶發(fā)送該用戶下線通知以及更新在線用 戶列表,如下圖 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),如
8、圖() 客服端接收到服務(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)需要對(duì)實(shí)現(xiàn)對(duì)用戶信息的添加、刪除和查詢功能。 在實(shí)現(xiàn) UserInfoList 類之前,先定義一個(gè)用于描述單個(gè)用戶信息的數(shù)據(jù)結(jié)構(gòu) Node 類,并定義五個(gè)數(shù)據(jù)成員:用戶昵稱、套接字、輸入流、輸出流和 Next 指針。 public class Node String username=null;/用戶昵稱 Socket soc
9、ket=null;/ 套接字 ObjectOutputStream output=null;/輸出流 ObjectInputStream input=null;/輸入流 Node next=null;/保存鏈表中下一個(gè)節(jié)點(diǎn)的指針 在UserInfoList 的構(gòu)造函數(shù)中,對(duì) 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 ad
10、d(Node n) pointer=root;/將游標(biāo)指向根節(jié)點(diǎn) 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é)
11、點(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
12、=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 (index0) return null; pointer=root; int i=0; while (iindex+1) if (pointer.next!=null) p
13、ointer=pointer.next; else return null; i+; return pointer; 2.2 服務(wù)器主框類架的實(shí)現(xiàn)服務(wù)器主框類架的實(shí)現(xiàn) 服務(wù)器主框類架類需要實(shí)現(xiàn)人機(jī)交互功能、啟動(dòng)服務(wù)功能 關(guān)閉服務(wù)功能和發(fā)送系統(tǒng)信息功能。需要為工程添加服務(wù)器主框類,名命為 ServerFrame,在 ServerFrame 類的構(gòu)造函數(shù)中,完成為服務(wù)器控件的添加、控件位置 的調(diào)整、控件的初始狀態(tài)的設(shè)定及控件事件偵聽函數(shù)的添加。添加函數(shù) init,在其中完 成界面的初始化功能,在 init 函數(shù)中,除對(duì)事件進(jìn)行添加和調(diào)整外,還對(duì)控件添加事 件偵聽器。如為關(guān)閉服務(wù)按鈕添加偵聽器: j
14、bStopServer.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) stopServer(); ); 在 ServerFrame 中添加 starrServer 函數(shù)完成啟動(dòng)服務(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ù)器
15、偵聽線程類的實(shí)現(xiàn)服務(wù)器偵聽線程類的實(shí)現(xiàn) 服務(wù)器偵聽線程類需要偵聽客戶端的登錄請(qǐng)求,并且為該用戶啟動(dòng)專用線程,處理 與此用戶相關(guān)的操作。首先為工程添加 ServerListenThread 類,繼承其父類 Thread,在 這個(gè)類中實(shí)現(xiàn)偵聽客戶端連接,并響應(yīng)用戶登錄的功能。在這個(gè)類中最重要的功能是 在 run 函數(shù)中實(shí)現(xiàn)的,當(dāng)服務(wù)器啟動(dòng)時(shí),就會(huì)建立偵聽線程,并且調(diào)用 start 函數(shù)開始 運(yùn)行線程。 public void run()/服務(wù)器偵聽線程的執(zhí)行體 while (!isStop/創(chuàng)建用戶信息節(jié)點(diǎn) client.socket=server.accept();/偵聽客戶端連接 client
16、.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(us
17、er+client.username+上線+n);/顯示提示信息 textfield.setText(在線用戶+userInfoList.getCount()+人n); recvThread=new ServerReceiveThread( textarea, textfield, combobox, client, userInfoList); recvThread.start();/啟動(dòng)線程 2.42.4服務(wù)器接收線程類的實(shí)現(xiàn)服務(wù)器接收線程類的實(shí)現(xiàn) 服務(wù)器接收線程類需要接收客戶端發(fā)來的信息,并且對(duì)該信息進(jìn)行分析,執(zhí)行 相應(yīng)操作。當(dāng)服務(wù)器收到客戶端發(fā)來的聊天信息,對(duì)發(fā)送對(duì)象進(jìn)行判斷,如果發(fā)送
18、 對(duì)象是所有用戶,則將信息發(fā)給所有用戶,同理,如果是特定用戶就發(fā)給特定用戶。 當(dāng)服務(wù)器接收到客戶端發(fā)來的下線請(qǐng)求時(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 c
19、ount=userInfoList.getCount(); int i=0; while(icount) Node node=userInfoList.find(i); 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,
20、其構(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(); S
21、tring message=textfield.getText(); if(socket.isClosed() return; try output.writeObject(聊天信息);/發(fā)送聊天信息標(biāo)識(shí) output.flush(); output.writeObject(toSomebody);/發(fā)送聊天對(duì)象 output.flush(); output.writeObject(status);/發(fā)送聊天狀態(tài) output.flush(); output.writeObject(action);/發(fā)送聊天表情 output.flush(); output.writeObject(messa
22、ge);/發(fā)送聊天信息內(nèi)容 output.flush(); 2.62.6客戶端接收線程類的實(shí)現(xiàn)客戶端接收線程類的實(shí)現(xiàn) 客戶端接收線程類需要接收服務(wù)器發(fā)來的信息,并對(duì)該信息進(jìn)行分析,執(zhí)行相應(yīng) 的操作。客戶端收到的服務(wù)器信息有三種聊天信息和系統(tǒng)信息、用戶列表信息、 服務(wù)關(guān)閉信息??蛻舳藢⒔邮盏搅奶煨畔⒑拖到y(tǒng)信息顯示在界面上,接收到用戶信息 就對(duì)信息進(jìn)行分析,獲取在線用戶列表,顯示在界面組合框中,接收到關(guān)閉信息,則 執(zhí)行關(guān)閉操作。 命名客戶端接收線程類為 ClientReceiveThread,實(shí)現(xiàn)接收服務(wù)器信息的功能,主 要在 run 函數(shù)中實(shí)現(xiàn)該類的功能。 public void run()/線
23、程執(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();
24、 String msg=client.username+action+對(duì)+toSomebody+ 說+message+n;/顯示信息 3附:源程序附:源程序 參考文獻(xiàn) 【Java 語言與面向?qū)ο蟪绦蛟O(shè)計(jì)】 【Java 編程案例解析】 【計(jì)算機(jī)網(wǎng)絡(luò)通信教程】 附 錄 (宋體小四號(hào),1.25 倍行距) (要求:附錄的有無根據(jù)情況需要而定,內(nèi)容一般包 括正文內(nèi)不便列出的冗長公式推導(dǎo)、符號(hào)說明(含縮寫) 、計(jì)算機(jī)程序、圖紙、表格等。 附錄中有程序源代碼的因篇幅限制可酌情考慮內(nèi)容的字號(hào)。 ) /用戶信息鏈表類 import .*; import java.io.*; public class Node
25、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
26、; count=0; public void add(Node n) pointer=root; 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() r
27、eturn 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) return pointer; return null; public Node find(int index) if (count=0) return null; if (index0) return null; pointer=r
28、oot; int i=0; while (iindex+1) if (pointer.next!=null) pointer=pointer.next; else return null; i+; return pointer; /服務(wù)端主框架類 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; import java.io.*; import .*; public class ServerFrame
29、 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; priva
30、te 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
31、=BorderFactory.createEmptyBorder(1,1,1,1); border=BorderFactory.createCompoundBorder(bevelBorder,emptyBorder); jbStartServer=new JButton(啟動(dòng)); jbStopServer=new JButton(關(guān)閉); b1=new JButton(發(fā)送); jbStopServer.setEnabled(false); jbStartServer.addActionListener( new ActionListener() public void actionPerf
32、ormed(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
33、=new JScrollPane(textarea); textfield=new JTextField(40); textfield.setEditable(false); systemMsg=new JTextField(40); systemMsg.setEnabled(false); systemMsg.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) sendSystemMessage(); ); this.addWindowListener( new WindowAd
34、apter() 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,10
35、0,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); c.add(systemMsg,null); public void
36、 startServer()/啟動(dòng)服務(wù) try serverSocket=new ServerSocket(8000,10); textarea.append(服務(wù)在 8000 端口啟動(dòng).n); jbStartServer.setEnabled(false); jbStopServer.setEnabled(true); systemMsg.setEnabled(true); catch (Exception e) userInfoList=new UserInfoList(); listenThread=new ServerListenThread(serverSocket, combobo
37、x, 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(); int i=0; while (icount) Node node=userInfoList.find(i); node.input.close(); node.output
38、.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 (icount)
39、 Node node=userInfoList.find(i); if(node=null) i+; continue; try 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.equalsIgn
40、oreCase(所有人) 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) public void sendMsgToAll(String msg) int count=userInfoList.getCount(); int i=0; wh
41、ile (icount) 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.*; import jav
42、a.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; publ
43、ic 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; public void
44、 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); u
45、serInfoList.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.*; import java.io.*;
46、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
47、userInfoList) this.textarea=textarea; this.textfield=textfield; this.client=client; this.userInfoList=userInfoList; bobox=combobox; isStop=false; public void run() sendUserList(); while(!isStop if(type.equalsIgnoreCase(聊天信息) String toSomebody=(String)client.input.readObject(); String status=(String)
48、client.input.readObject(); String action=(String)client.input.readObject(); String message=(String)client.input.readObject(); String msg=client.username+action+對(duì)+toSomebody+ 說+message+n; if(status.equalsIgnoreCase(悄悄話) msg=悄悄話+msg; textarea.append(msg); if(toSomebody.equalsIgnoreCase(所有人) sendToAll(
49、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) node.output.writeObject(聊天信息); node.output.flush(); node.output.writeObject(msg); node.output.flus
50、h(); 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(icount) node =userInfoList.find(i); if(node=null) i+;
51、 continue; combobox.addItem(node.username); i+; combobox.setSelectedIndex(0); textarea.append(msg); textfield.setText(在線用戶+userInfoList.getCount()+人n); sendToAll(msg); sendUserList(); break; catch(Exception e) public void sendToAll(String msg) int count=userInfoList.getCount(); int i=0; while(icount
52、) 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+; public void sendUserList() String userlist=; int count=userInfoList.getCount(); int i =0; while(icount) Node
53、 node=userInfoList.find(i); if (node=null) i+; continue; userlist+=node.username; userlist+=n; i+; i=0; while(icount) Node node =userInfoList.find(i); if(node=null) i+; continue; try node.output.writeObject(用戶列表); node.output.flush(); node.output.writeObject(userlist); node.output.flush(); catch(Exc
54、eption 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;
55、 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
56、; 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 JButt
57、on(上線); 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(請(qǐng)先輸入你的昵稱,再上線,20); seperater=
58、 new JTextField(,20); seperater.setEditable(false); checkbox=new JCheckBox(悄悄話); checkbox.setSelected(false); actionlist=new JComboBox(); actionlist.addItem(微笑地); actionlist.addItem(生氣地); actionlist.addItem(小心地); actionlist.setSelectedIndex(0); login.setBorder(border); login.setBounds(30,30,90,30);
59、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); Co
60、ntainer 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 actionPe
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 未來電商模式探索與展望
- 現(xiàn)代企業(yè)財(cái)務(wù)管理的倫理與責(zé)任
- 生產(chǎn)管理策略與工藝優(yōu)化技術(shù)
- 災(zāi)害預(yù)防教育學(xué)校防災(zāi)減災(zāi)的必由之路
- 環(huán)保節(jié)能建筑的設(shè)計(jì)與實(shí)施案例分享
- 2024年重陽節(jié)活動(dòng)策劃方案-11
- 現(xiàn)代物流與科技融合的商業(yè)模式
- 國慶節(jié)房地產(chǎn)促銷方案
- 2024年五年級(jí)英語上冊(cè) Unit 6 In a nature park Part A 第三課時(shí)說課稿 人教PEP
- 2024-2025學(xué)年新教材高中語文 第二單元 5 雷雨(節(jié)選)(1)說課稿 部編版必修下冊(cè)
- 教體局校車安全管理培訓(xùn)
- 湖北省十堰市城區(qū)2024-2025學(xué)年九年級(jí)上學(xué)期期末質(zhì)量檢測綜合物理試題(含答案)
- 網(wǎng)優(yōu)案例匯總
- 冶金廠、軋鋼廠工藝流程圖
- 《民航服務(wù)溝通技巧》教案第15課民航服務(wù)人員下行溝通的技巧
- 中國人婚戀狀況調(diào)查報(bào)告公布
- 早產(chǎn)兒視網(wǎng)膜病變
- GB 10665-1997碳化鈣(電石)
- 《中小學(xué)教育懲戒規(guī)則》重點(diǎn)內(nèi)容學(xué)習(xí)PPT課件(帶內(nèi)容)
- 板帶生產(chǎn)工藝5(熱連軋帶鋼生產(chǎn))課件
- 2022年同等學(xué)力英語考試真題及詳解
評(píng)論
0/150
提交評(píng)論