java課程設(shè)計(jì)報(bào)告(java聊天室)_第1頁
java課程設(shè)計(jì)報(bào)告(java聊天室)_第2頁
java課程設(shè)計(jì)報(bào)告(java聊天室)_第3頁
java課程設(shè)計(jì)報(bào)告(java聊天室)_第4頁
java課程設(shè)計(jì)報(bào)告(java聊天室)_第5頁
已閱讀5頁,還剩36頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上Java聊天室課程設(shè)計(jì)一、 需求分析1.1開發(fā)背景在信息化社會的今天,網(wǎng)絡(luò)飛速發(fā)展,人們對網(wǎng)絡(luò)的依賴越來越多,越來越離不開網(wǎng)絡(luò),由此而產(chǎn)生的聊天工具越來越多,類似MSN、QQ,網(wǎng)絡(luò)聊天時一類的聊天系統(tǒng)的發(fā)展日新月異,因此產(chǎn)生了制作一個類似QQ的網(wǎng)絡(luò)聊天工具的想法,且通過制作該程序還能更好的學(xué)習(xí)網(wǎng)絡(luò)軟件編程知識。網(wǎng)絡(luò)編程的目的就是指直接或間接地通過網(wǎng)絡(luò)協(xié)議與其他計(jì)算機(jī)進(jìn)行通訊。網(wǎng)編程中有兩個主要的問題,一個是如何準(zhǔn)確的定位網(wǎng)絡(luò)上一臺或多臺主機(jī),另一個就是找到主機(jī)后如何可靠高效的進(jìn)行數(shù)據(jù)傳輸。在TCP/IP協(xié)議中IP層主要負(fù)責(zé)網(wǎng)絡(luò)主機(jī)的定位,數(shù)據(jù)傳輸?shù)穆酚?,由IP地址可以

2、唯一地確定Internet上的一臺主機(jī)。而TCP層則提供面向應(yīng)用的可靠的或非可靠的數(shù)據(jù)傳輸機(jī)制,這是網(wǎng)絡(luò)編程的主要對象,一般不需要關(guān)心IP層是如何處理數(shù)據(jù)的。目前較為流行的網(wǎng)絡(luò)編程模型是客戶機(jī)/服務(wù)器(C/S)結(jié)構(gòu)。即通信雙方一方作為服務(wù)器等待客戶提出請求并予以響應(yīng)??蛻魟t在需要服務(wù)時向服務(wù)器提出申請。服務(wù)器一般作為守護(hù)進(jìn)程始終運(yùn)行,監(jiān)聽網(wǎng)絡(luò)端口,一旦有客戶請求,就會啟動一個服務(wù)進(jìn)程來響應(yīng)該客戶,同時自己繼續(xù)監(jiān)聽服務(wù)端口,使后來的客戶也得到響應(yīng)的服務(wù)。1.2設(shè)計(jì)要求本課程設(shè)計(jì)的目標(biāo)是利用套接字socket()設(shè)計(jì)一個聊天程序,該程序基于C/S模式,客戶機(jī)器向服務(wù)器發(fā)聊天請求,服務(wù)器應(yīng)答并能顯示

3、客戶機(jī)發(fā)過來的信息。1.3設(shè)計(jì)目的通過設(shè)計(jì)一個網(wǎng)絡(luò)聊天程序,對套接字、數(shù)據(jù)報(bào)通訊、URL、與URLConnectiom的相關(guān)知識有詳細(xì)的了解和充分的認(rèn)識。能將相關(guān)的只是運(yùn)用到相關(guān)的實(shí)踐中去。1.4功能實(shí)現(xiàn)聊天室共分為客戶端和服務(wù)端兩部分,服務(wù)器程序主要負(fù)責(zé)偵聽客戶端發(fā)來的消息,客戶端需要登錄到相應(yīng)的服務(wù)器才可以實(shí)現(xiàn)正常的聊天功能。服務(wù)器的主要功能有1) 在特定端口上進(jìn)行偵聽,等待客戶連接2) 用戶可以配置服務(wù)器的真挺端口3) 向已經(jīng)連接服務(wù)器的客戶發(fā)送系統(tǒng)消息4) 統(tǒng)計(jì)在線人數(shù)5) 當(dāng)停止服務(wù)時,斷開所有用戶的連接客戶端的主要功能1) 連接到已經(jīng)開啟聊天服務(wù)的服務(wù)端2) 用戶可以配置要連接服務(wù)

4、器端的ip地址和端口號3) 用戶可以配置連接后顯示的用戶名4) 當(dāng)服務(wù)器開啟時。用戶可以隨時登陸和注銷5) 用戶可以向所有人或一個人發(fā)送消息二、 總體設(shè)計(jì)2.1設(shè)計(jì)思想套接字對象在網(wǎng)絡(luò)編程中扮演者重要的角色,可以用套接字技術(shù)編寫一個聊天室,服務(wù)器為每個客戶啟動一個線程。在該線程中通過套接字和客戶交流信息,當(dāng)客戶向服務(wù)器發(fā)送一條聊天信息“大家好”時,服務(wù)器要讓所有的這些線程中的輸入流寫入信息大家好,這樣所有的客戶的套接字的輸入流就都讀取到了這一條信息。如果把信息“你好”發(fā)送給特定用戶,服務(wù)器就讓特定線程中的輸出流寫入信息“你好”,那么只有特定客戶的套機(jī)字的輸入流可以讀取到這條信息。在聊天室中需要

5、對用戶上線下線的狀態(tài)進(jìn)行修改,進(jìn)而統(tǒng)計(jì)在線人數(shù)、查找某用戶等。因而需要用到j(luò)ava鏈表來實(shí)現(xiàn)。由于Java語言不像c或c+一樣可以利用線性表的鏈?zhǔn)酱鎯Y(jié)構(gòu),用節(jié)點(diǎn)和指針來表示,在Java中是沒有指針的,但是可以通過使用對象的引用來實(shí)現(xiàn)鏈表。鏈表的結(jié)點(diǎn)個數(shù)稱為鏈表的長度。因此在Java中可以定義兩個類來實(shí)現(xiàn)鏈表的操作,分別為節(jié)點(diǎn)類和鏈表類。在本設(shè)計(jì)中對用戶的存儲就是利用鏈表來實(shí)現(xiàn)的。2.2基本設(shè)計(jì)概念和處理流程本系統(tǒng)運(yùn)行用JAVA開發(fā),采用CS結(jié)構(gòu),系統(tǒng)分為客服端和服務(wù)端兩大模塊,使用Socket類建立套接字實(shí)現(xiàn)客服端和服務(wù)端得通訊。處理流程客戶端服務(wù)端發(fā)送請求處理請求服務(wù)端 客戶端一個Serv

6、erSocket對象和一個Socket對象 一個Socketd對象ServerSocket(port)創(chuàng)建ServerSocket對象提供TCP連接服務(wù)Accept()在指定端口等待客戶端的連接請求連接成功,獲得一個已連接的socket對象InputStream讀取Socket對象的輸入流OutputStream寫入Socket對象的輸出流InputStream讀取Socket對象的輸入流OutputStream寫入Socket對象的輸出流Socket(host,port)創(chuàng)建Socket對象,向指定主機(jī)端口發(fā)出連接請求Socket.close()關(guān)閉TCP連接ServerSocket.clo

7、se()停止提供TCP連接服務(wù)Socket.close()關(guān)閉TCP連接建立Socket連接申請連接通過流傳送數(shù)據(jù)TCP Socket通信流程2.3總體結(jié)構(gòu)網(wǎng)絡(luò)聊天室服務(wù)端客服端2.4功能分配客戶端向所有人或一個人發(fā)送消息客戶端連接服務(wù)端配置服務(wù)端登錄與注銷服務(wù)端服務(wù)端等待客戶連接配置服務(wù)器偵聽端口向用戶發(fā)送系統(tǒng)消息統(tǒng)計(jì)在線人數(shù)、斷開連接等2.5接口設(shè)計(jì)2.5.1用戶接口提供一個用戶操作界面:包括用戶可以再界面中登錄聊天室、輸入消息、瀏覽聊天內(nèi)容和聊天對象。同時提供一個服務(wù)器操作界面,通過服務(wù)器操作界面可以修改服務(wù)器的配置,知道用戶的當(dāng)前狀態(tài),并可以給用戶發(fā)送指定系統(tǒng)信息。2.5.2內(nèi)部接口S

8、ocket(string hont ,int port );客戶端使用Socket類建立與服務(wù)器的套接字連接。ServerSocket(int port);建立接收客戶的套接字的服務(wù)器套接字。2.6主要模塊2.6.1聊天室服務(wù)器端模塊聊天室服務(wù)器端模塊主要有以下幾部分組成1、 主框架類(ChatServer.java)該文件包含名為ChatServer的public類,其主要功能是定義服務(wù)器的界面,添加事件偵聽魚事件處理。ChatServer類調(diào)用ServerListen類來實(shí)現(xiàn)服務(wù)端用戶上線與下線的偵聽,調(diào)用ServerReceive類來實(shí)現(xiàn)服務(wù)器端消息的轉(zhuǎn)發(fā)。2、 服務(wù)器用戶上線與下線偵聽

9、類。該類對用戶上線與下線的偵聽是通過調(diào)用用戶鏈表類來實(shí)現(xiàn)的,當(dāng)用戶的上線與下線情況發(fā)生改變時,該類會對主類的界面進(jìn)行相應(yīng)的修改。3、 服務(wù)器消息收發(fā)類該類分別定義了向某用戶及向所有人發(fā)送消息的方法,發(fā)送的消息會顯示在主界面類的界面上。4用戶修改配置的類。該類繼承自JDialog。使用戶對服務(wù)器端口進(jìn)行修改配置的類。5節(jié)點(diǎn)類定義了鏈表中的用戶6鏈表類該類通過構(gòu)造函數(shù)構(gòu)造用戶鏈表,定義了添加用戶、刪除用戶、返回用戶數(shù)、根據(jù)用戶名查找用戶和各根據(jù)索引查找用戶等方法。7服務(wù)器幫助類、2.6.2聊天室服務(wù)器端模塊算法描述ActionListenerChatServer-port:public stati

10、c in-serverSocket:ServerSocket-messageShow:JTextArea-userLinkList:UserLinkList-showStatus:JTextField+ ChatServer():public+init():public void +startService():public void+stopService():public void +sendStopToAll():public void+sendMsgToAll(String):public void+ sendSystemMessage():public voidJFrameThrea

11、dServerListen- server: ServerSocket- userLinkList :UserLinkList- isStop: public Boolean+ServerListen(ServerSocket,JComboBox,JTextArea,JTextField,UserLinkList)ServerReceive-textarea:JTextArea- userLinkList :UserLinkList- isStop: public Boolean+ServerListen(ServerSocket,JComboBox,JTextArea,JTextField,

12、UserLinkList)服務(wù)器端的ChatServer類繼承自JFrame并實(shí)現(xiàn)相應(yīng)的事件監(jiān)聽接口,因此它定義了服務(wù)器的主框架,及各個按鈕的事件監(jiān)聽。它分別調(diào)用ServerListen類來實(shí)現(xiàn)服務(wù)端用戶上線與下線的偵聽,調(diào)用ServerReceive類來實(shí)現(xiàn)服務(wù)器端消息的轉(zhuǎn)發(fā)。同時服務(wù)器可以響應(yīng)多個客戶的請求,當(dāng)一個客戶發(fā)送請求時,服務(wù)器就為它增加一個線程,同時服務(wù)器利用UserLinkList類為客戶端設(shè)置一個請求隊(duì)列,如果服務(wù)器不能馬上響應(yīng)客戶端的請求,就要把這個請求放到請求隊(duì)列在中,等服務(wù)器將當(dāng)前的請求處理完,會自動到請求隊(duì)列中按照先后順序取出請求進(jìn)行處理。2.6.3聊天室客戶端模塊客

13、戶端主要有以下幾個文件,功能如下:1客戶端主框架類該類主要定義客戶端的界面,添加事件偵聽與事件處理。該類定義了與服務(wù)器實(shí)現(xiàn)連接與斷開連接的方法。當(dāng)用戶登錄到指定的服務(wù)器上時,該類條用客戶端實(shí)現(xiàn)消息收發(fā)的類實(shí)現(xiàn)消息的收發(fā)。同時該類定義了向所有用戶發(fā)送消息的方法。2客戶端消息收發(fā)的類該類實(shí)現(xiàn)了服務(wù)器與客戶端消息的收發(fā)3用戶修改配置的類該類繼承自JDialog,是用戶對要連接的服務(wù)器IP及偵聽端口進(jìn)行修改配置的類。4幫助類客戶端用戶程序的幫助類2.6.4聊天室客戶端模塊主要算法描述ActionListenerChatClient-port:public static in-serverSocket:

14、ServerSocket-messageShow:JTextArea-userLinkList:UserLinkList-showStatus:JTextField+ ChatClient():public+init():public void +Connect():public void+DisConnect():public void +SendMessage():public void JFrameThreadClientReceive- socket: Socket-output:ObjectOutputStream-input:ObjectInputStream +ClientRec

15、eive(Socket,ObjectOutputStream,ObjectIn JComboBox,JTextArea,JTextField )客戶端ChatClient類繼承了JFrame并實(shí)現(xiàn)相應(yīng)的事件監(jiān)聽接口。它實(shí)現(xiàn)了客戶端的主界面及相應(yīng)按鈕的事件偵聽。該類調(diào)用ClientReceive類實(shí)現(xiàn)消息的收發(fā)。同時該類定義了向所有用戶發(fā)送消息的方法。通過相應(yīng)的輸入輸出流與服務(wù)器進(jìn)行數(shù)據(jù)的傳遞與交流。三、 主要代碼實(shí)現(xiàn)3.1服務(wù)器主要代碼實(shí)現(xiàn)1、Chatserver類:包含名為ChatServer的public類,其主要功能為定義服務(wù)器端的界面,添加事件偵聽與事件處理。調(diào)用ServerListe

16、n類來實(shí)現(xiàn)服務(wù)端用戶上線與下線的偵聽,調(diào)用ServerReceive類來實(shí)現(xiàn)服務(wù)器端的消息的收發(fā)。import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import .*;import java.io.*;/* * 聊天服務(wù)端的主框架類 */public class ChatServer extends JFrame implements ActionListenerpublic static int port = 8888;/服務(wù)端的偵聽端口ServerSocket

17、serverSocket;/服務(wù)端SocketImage icon;/程序圖標(biāo)JComboBox combobox;/選擇發(fā)送消息的接受者JTextArea messageShow;/服務(wù)端的信息顯示JScrollPane messageScrollPane;/信息顯示的滾動條JTextField showStatus;/顯示用戶連接狀態(tài)JLabel sendToLabel,messageLabel;JTextField sysMessage;/服務(wù)端消息的發(fā)送JButton sysMessageButton;/服務(wù)端消息的發(fā)送按鈕UserLinkList userLinkList;/用戶鏈表

18、/建立菜單欄JMenuBar jMenuBar = new JMenuBar(); /建立菜單組JMenu serviceMenu = new JMenu ("服務(wù)(V)"); /建立菜單項(xiàng)JMenuItem portItem = new JMenuItem ("端口設(shè)置(P)");JMenuItem startItem = new JMenuItem ("啟動服務(wù)(S)");JMenuItem stopItem=new JMenuItem ("停止服務(wù)(T)");JMenuItem exitItem=new JM

19、enuItem ("退出(X)");JMenu helpMenu=new JMenu ("幫助(H)");JMenuItem helpItem=new JMenuItem ("幫助(H)");/建立工具欄JToolBar toolBar = new JToolBar();/建立工具欄中的按鈕組件JButton portSet;/啟動服務(wù)端偵聽JButton startServer;/啟動服務(wù)端偵聽JButton stopServer;/關(guān)閉服務(wù)端偵聽JButton exitButton;/退出按鈕/框架的大小Dimension fac

20、eSize = new Dimension(400, 600);ServerListen listenThread;JPanel downPanel ;GridBagLayout girdBag;GridBagConstraints girdBagCon;/* * 服務(wù)端構(gòu)造函數(shù) */public ChatServer()init();/初始化程序/添加框架的關(guān)閉事件處理this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.pack();/設(shè)置框架的大小this.setSize(faceSize);/設(shè)置運(yùn)行時窗口的位置Dimensi

21、on screenSize = Toolkit.getDefaultToolkit().getScreenSize();this.setLocation( (int) (screenSize.width - faceSize.getWidth() / 2, (int) (screenSize.height - faceSize.getHeight() / 2);this.setResizable(false);this.setTitle("聊天室服務(wù)端"); /設(shè)置標(biāo)題/程序圖標(biāo)icon = getImage("icon.gif");this.setIc

22、onImage(icon); /設(shè)置程序圖標(biāo)show();/為服務(wù)菜單欄設(shè)置熱鍵'V'serviceMenu.setMnemonic('V');/為端口設(shè)置快捷鍵為ctrl+pportItem.setMnemonic ('P'); portItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_P,InputEvent.CTRL_MASK);/為啟動服務(wù)快捷鍵為ctrl+sstartItem.setMnemonic ('S'); startItem.setAccelerat

23、or (KeyStroke.getKeyStroke (KeyEvent.VK_S,InputEvent.CTRL_MASK);/為端口設(shè)置快捷鍵為ctrl+TstopItem.setMnemonic ('T'); stopItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_T,InputEvent.CTRL_MASK);/為退出設(shè)置快捷鍵為ctrl+xexitItem.setMnemonic ('X'); exitItem.setAccelerator (KeyStroke.getKeyStroke

24、 (KeyEvent.VK_X,InputEvent.CTRL_MASK);/為幫助菜單欄設(shè)置熱鍵'H'helpMenu.setMnemonic('H');/為幫助設(shè)置快捷鍵為ctrl+phelpItem.setMnemonic ('H'); helpItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_H,InputEvent.CTRL_MASK);/* * 程序初始化函數(shù) */public void init()Container contentPane = getContentPa

25、ne();contentPane.setLayout(new BorderLayout();/添加菜單欄serviceMenu.add (portItem);serviceMenu.add (startItem);serviceMenu.add (stopItem);serviceMenu.add (exitItem);jMenuBar.add (serviceMenu); helpMenu.add (helpItem);jMenuBar.add (helpMenu); setJMenuBar (jMenuBar);/初始化按鈕portSet = new JButton("端口設(shè)置&

26、quot;);startServer = new JButton("啟動服務(wù)");stopServer = new JButton("停止服務(wù)" );exitButton = new JButton("退出" );/將按鈕添加到工具欄toolBar.add(portSet);toolBar.addSeparator();/添加分隔欄toolBar.add(startServer);toolBar.add(stopServer);toolBar.addSeparator();/添加分隔欄toolBar.add(exitButton);c

27、ontentPane.add(toolBar,BorderLayout.NORTH);/初始時,令停止服務(wù)按鈕不可用stopServer.setEnabled(false);stopItem .setEnabled(false);/為菜單欄添加事件監(jiān)聽portItem.addActionListener(this);startItem.addActionListener(this);stopItem.addActionListener(this);exitItem.addActionListener(this);helpItem.addActionListener(this);/添加按鈕的事件

28、偵聽portSet.addActionListener(this);startServer.addActionListener(this);stopServer.addActionListener(this);exitButton.addActionListener(this);combobox = new JComboBox();combobox.insertItemAt("所有人",0);combobox.setSelectedIndex(0);messageShow = new JTextArea();messageShow.setEditable(false);/添

29、加滾動條messageScrollPane = new JScrollPane(messageShow,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);messageScrollPane.setPreferredSize(new Dimension(400,400);messageScrollPane.revalidate();showStatus = new JTextField(35);showStatus.setEditable(false)sysMessage =

30、new JTextField(24);sysMessage.setEnabled(false);sysMessageButton = new JButton();sysMessageButton.setText("發(fā)送");/添加系統(tǒng)消息的事件偵聽sysMessage.addActionListener(this);sysMessageButton.addActionListener(this);sendToLabel = new JLabel("發(fā)送至:");messageLabel = new JLabel("發(fā)送消息:");do

31、wnPanel = new JPanel();girdBag = new GridBagLayout();downPanel.setLayout(girdBag);girdBagCon = new GridBagConstraints();girdBagCon.gridx = 0;girdBagCon.gridy = 0;girdBagCon.gridwidth = 3;girdBagCon.gridheight = 2;girdBagCon.ipadx = 5;girdBagCon.ipady = 5;JLabel none = new JLabel(" ");girdB

32、ag.setConstraints(none,girdBagCon);downPanel.add(none);girdBagCon = new GridBagConstraints();girdBagCon.gridx = 0;girdBagCon.gridy = 2;girdBagCon.insets = new Insets(1,0,0,0);girdBagCon.ipadx = 5;girdBagCon.ipady = 5;girdBag.setConstraints(sendToLabel,girdBagCon);downPanel.add(sendToLabel);girdBagCo

33、n = new GridBagConstraints();girdBagCon.gridx =1;girdBagCon.gridy = 2;girdBagCon.anchor = GridBagConstraints.LINE_START;girdBag.setConstraints(combobox,girdBagCon);downPanel.add(combobox);girdBagCon = new GridBagConstraints();girdBagCon.gridx = 0;girdBagCon.gridy = 3;girdBag.setConstraints(messageLa

34、bel,girdBagCon);downPanel.add(messageLabel);girdBagCon = new GridBagConstraints();girdBagCon.gridx = 1;girdBagCon.gridy = 3;girdBag.setConstraints(sysMessage,girdBagCon);downPanel.add(sysMessage);girdBagCon = new GridBagConstraints();girdBagCon.gridx = 2;girdBagCon.gridy = 3;girdBag.setConstraints(s

35、ysMessageButton,girdBagCon);downPanel.add(sysMessageButton);girdBagCon = new GridBagConstraints();girdBagCon.gridx = 0;girdBagCon.gridy = 4;girdBagCon.gridwidth = 3;girdBag.setConstraints(showStatus,girdBagCon);downPanel.add(showStatus);contentPane.add(messageScrollPane,BorderLayout.CENTER);contentP

36、ane.add(downPanel,BorderLayout.SOUTH);/關(guān)閉程序時的操作this.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)stopService();System.exit(0);/* * 事件處理 */public void actionPerformed(ActionEvent e) Object obj = e.getSource();if (obj = startServer | obj = startItem) /啟動服務(wù)端startService(

37、);else if (obj = stopServer | obj = stopItem) /停止服務(wù)端int j=JOptionPane.showConfirmDialog(this,"真的停止服務(wù)嗎?","停止服務(wù)",JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE);if (j = JOptionPane.YES_OPTION)stopService();else if (obj = portSet | obj = portItem) /端口設(shè)置/調(diào)出端口設(shè)置的對話框PortConf portC

38、onf = new PortConf(this);portConf.show();else if (obj = exitButton | obj = exitItem) /退出程序int j=JOptionPane.showConfirmDialog(this,"真的要退出嗎?","退出",JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE);if (j = JOptionPane.YES_OPTION)stopService();System.exit(0);else if (obj = helpIt

39、em) /菜單欄中的幫助/調(diào)出幫助對話框Help helpDialog = new Help(this);helpDialog.show();else if (obj = sysMessage | obj = sysMessageButton) /發(fā)送系統(tǒng)消息sendSystemMessage();/* * 啟動服務(wù)端 */public void startService()tryserverSocket = new ServerSocket(port,10);messageShow.append("服務(wù)端已經(jīng)啟動,在"+port+"端口偵聽.n");s

40、tartServer.setEnabled(false);startItem.setEnabled(false);portSet.setEnabled(false);portItem.setEnabled(false);stopServer .setEnabled(true);stopItem .setEnabled(true);sysMessage.setEnabled(true);catch (Exception e)/System.out.println(e);userLinkList = new UserLinkList();listenThread = new ServerListe

41、n(serverSocket,combobox,messageShow,showStatus,userLinkList);listenThread.start();/* * 關(guān)閉服務(wù)端 */public void stopService()try/向所有人發(fā)送服務(wù)器關(guān)閉的消息sendStopToAll();listenThread.isStop = true;serverSocket.close();int count = userLinkList.getCount();int i =0;while( i < count)Node node = userLinkList.findUser

42、(i);node.input .close();node.output.close();node.socket.close();i +;stopServer .setEnabled(false);stopItem .setEnabled(false);startServer.setEnabled(true);startItem.setEnabled(true);portSet.setEnabled(true);portItem.setEnabled(true);sysMessage.setEnabled(false);messageShow.append("服務(wù)端已經(jīng)關(guān)閉n"

43、;);combobox.removeAllItems();combobox.addItem("所有人");catch(Exception e)/System.out.println(e);/* * 向所有人發(fā)送服務(wù)器關(guān)閉的消息 */public void sendStopToAll()int count = userLinkList.getCount();int i = 0;while(i < count)Node node = userLinkList.findUser(i);if(node = null) i +;continue;trynode.output.w

44、riteObject("服務(wù)關(guān)閉");node.output.flush();catch (Exception e)/System.out.println("$"+e);i+;/* * 向所有人發(fā)送消息 */public void sendMsgToAll(String msg)int count = userLinkList.getCount();/用戶總數(shù)int i = 0;while(i < count)Node node = userLinkList.findUser(i);if(node = null) i +;continue;tryn

45、ode.output.writeObject("系統(tǒng)信息");node.output.flush();node.output.writeObject(msg);node.output.flush();catch (Exception e)/System.out.println(""+e);i+;sysMessage.setText("");/* * 向客戶端用戶發(fā)送消息 */public void sendSystemMessage()String toSomebody = combobox.getSelectedItem().toS

46、tring();String message = sysMessage.getText() + "n"messageShow.append(message);/向所有人發(fā)送消息if(toSomebody.equalsIgnoreCase("所有人")sendMsgToAll(message);else/向某個用戶發(fā)送消息Node node = userLinkList.findUser(toSomebody);trynode.output.writeObject("系統(tǒng)信息");node.output.flush();node.out

47、put.writeObject(message);node.output.flush();catch(Exception e)/System.out.println("!"+e);sysMessage.setText("");/將發(fā)送消息欄的消息清空/* * 通過給定的文件名獲得圖像 */Image getImage(String filename) URLClassLoader urlLoader = (URLClassLoader)this.getClass().getClassLoader();URL url = null;Image image

48、= null;url = urlLoader.findResource(filename);image = Toolkit.getDefaultToolkit().getImage(url);MediaTracker mediatracker = new MediaTracker(this);try mediatracker.addImage(image, 0);mediatracker.waitForID(0);catch (InterruptedException _ex) image = null;if (mediatracker.isErrorID(0) image = null;re

49、turn image;public static void main(String args) ChatServer app = new ChatServer();(2)ServerListen.java:該類實(shí)現(xiàn)服務(wù)端用戶上線與下線的偵聽。該類對用戶上線下線的偵聽是通過調(diào)用用戶鏈表類(UserLinkList)來實(shí)現(xiàn)的,當(dāng)用戶上線與下線情況發(fā)生變化時,該類會對主類的界面進(jìn)行相應(yīng)的修改。import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import java.io.

50、*;import .*;/* * 服務(wù)端的偵聽類 */public class ServerListen extends Thread ServerSocket server;JComboBox combobox;JTextArea textarea;JTextField textfield;UserLinkList userLinkList;/用戶鏈表Node client;ServerReceive recvThread;public boolean isStop;/* * 聊天服務(wù)端的用戶上線于下線偵聽類 */public ServerListen(ServerSocket server

51、,JComboBox combobox,JTextArea textarea,JTextField textfield,UserLinkList userLinkList)this.server = server;bobox = combobox;this.textarea = textarea;this.textfield = textfield;this.userLinkList = userLinkList;isStop = false;public void run()while(!isStop && !server.isClosed()tryclient = new

52、Node();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);userLinkList.addUser(client);textarea.append("用戶 " + client.username + " 上線" + "n");textfield.se

溫馨提示

  • 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)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論