網(wǎng)絡(luò)聊天室課程設(shè)計(jì)報(bào)告(內(nèi)附j(luò)ava代碼)_第1頁
網(wǎng)絡(luò)聊天室課程設(shè)計(jì)報(bào)告(內(nèi)附j(luò)ava代碼)_第2頁
網(wǎng)絡(luò)聊天室課程設(shè)計(jì)報(bào)告(內(nèi)附j(luò)ava代碼)_第3頁
網(wǎng)絡(luò)聊天室課程設(shè)計(jì)報(bào)告(內(nèi)附j(luò)ava代碼)_第4頁
網(wǎng)絡(luò)聊天室課程設(shè)計(jì)報(bào)告(內(nèi)附j(luò)ava代碼)_第5頁
已閱讀5頁,還剩43頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、課程設(shè)計(jì)網(wǎng)絡(luò)聊天室院 (系) 別信電學(xué)院班級計(jì)算 102學(xué)號(hào)姓名指導(dǎo)教師時(shí)間2013-4-182013-5-10課程設(shè)計(jì)任務(wù)書題目網(wǎng)絡(luò)聊天室課程設(shè)計(jì)系(部)信電學(xué)院專業(yè)計(jì)算機(jī)科學(xué)與技術(shù)班級計(jì)算 102學(xué)生姓名學(xué)號(hào)4月 18日至5月10日 共4 周指導(dǎo)教師 (簽字 )系 主 任(簽字)年月日一、 設(shè)計(jì)內(nèi)容及要求二、設(shè)計(jì)原始資料1)電子工業(yè)出版社JSP實(shí)用教程,鄭阿奇編著2)電子工業(yè)出版社JAVA程序設(shè)計(jì)實(shí)用教程,葉核亞編著三、設(shè)計(jì)完成后提交的文件和圖表1計(jì)算說明書部分JSP界面設(shè)計(jì)2圖紙部分:程序流程圖四、進(jìn)程安排五、主要參考資料1)電子工業(yè)出版社JSP實(shí)用教程,鄭阿奇編著2)電子工業(yè)出版社JA

2、VA程序設(shè)計(jì)實(shí)用教程,葉核亞編著考資料成績評定表作品成績報(bào)告成績口試(答辯)總評成績成績目 錄課程設(shè)計(jì)概述21.1 問題描述21.2 功能需求分析21.3 環(huán)境需求分析2學(xué)生信息管理系統(tǒng)軟件總體設(shè)計(jì)32.1 整體思路32.2 總體設(shè)計(jì)流程圖4學(xué)生信息管理軟件詳細(xì)設(shè)計(jì)53.1.界面設(shè)計(jì)53.2 通信功能實(shí)現(xiàn)方法7參考文獻(xiàn)錯(cuò)誤!未定義書簽。課程設(shè)計(jì)概述1.1 問題描述隨著計(jì)算機(jī)網(wǎng)絡(luò)日新月異的發(fā)展,人們的交流方式越來越多,傳統(tǒng)的交流方式,如:信件、電報(bào)、電話等已經(jīng)難以滿足人們的交流要求,在互聯(lián)網(wǎng)上即時(shí)的和好友取得聯(lián)系,已經(jīng)成為當(dāng)今社會(huì)人們主流的聯(lián)系方式。因此,兩臺(tái)計(jì)算機(jī)之間進(jìn)行即時(shí)通訊、發(fā)送文件等交

3、流方式已經(jīng)成為必然潮流。因此出現(xiàn)了等聊天工具,然而等聊天工具雖然方便、實(shí)用,但是,娛樂功能太多,有很多吸引人的娛樂功能,從一定程度上來說,是一種娛樂工具,不能作為用于即時(shí)通訊、文件共享的專用工具。目前,用于實(shí)現(xiàn)單一的即時(shí)通訊、文件共享的軟件實(shí)在太少,而且,它們中大部分都是絕對的 C/S 模式,對遠(yuǎn)程服務(wù)器有非常大的依賴性,沒有徹底實(shí)現(xiàn)點(diǎn)對點(diǎn)的交流。為此,我決定開發(fā)一個(gè)專用于實(shí)現(xiàn)兩臺(tái)計(jì)算機(jī)之間即時(shí)通訊、文件共享的軟件,以方便兩臺(tái)計(jì)算機(jī)之間信息的交流。避免了服務(wù)器忙或與服務(wù)器無法連接時(shí),浪費(fèi)過多時(shí)間用于和服務(wù)器建立連接。因此,這個(gè)軟件是極具適應(yīng)性和實(shí)用性的即時(shí)通訊軟件。1.2 功能需求分析實(shí)現(xiàn)網(wǎng)絡(luò)

4、聊天的功能,采用 Windows Socket(或 Java Socket)編程,服務(wù)器與客戶端采用了 TCP/IP連接方式, 在設(shè)計(jì)聊天方案時(shí), 可將所有信息發(fā)往服務(wù)器端, 再由服務(wù)器進(jìn)行處理,服務(wù)器端是所有信息的中心。服務(wù)器端要保存用戶信息,可利用數(shù)據(jù)庫來實(shí)現(xiàn)這一功能,需要建立用戶信息數(shù)據(jù)庫。在客戶端可采用文件系統(tǒng)保存聊天紀(jì)錄和用戶號(hào)碼??赏ㄟ^閃動(dòng)托盤圖標(biāo)和播放不同的音樂提示信息到來及好友上線等信息??赏ㄟ^建立消息鏈表來保存用戶接收的各種消息。1.3 環(huán)境需求分析運(yùn)行環(huán)境: Windows 9x 、 2000、 XP、 Windows 7必要環(huán)境: JDK 1.6以上、 eclipse 開

5、發(fā)環(huán)境硬件環(huán)境: CPU 400MHz以上 ,內(nèi)存 512MB 以上學(xué)生信息管理系統(tǒng)軟件總體設(shè)計(jì)2.1 整體思路該 C/S 模式的聊天系統(tǒng),就是服務(wù)器來提供服務(wù)端連接響應(yīng),客戶端主動(dòng)的發(fā)起連接請求。當(dāng)多個(gè)用戶同時(shí)連接到服務(wù)器時(shí),經(jīng)由服務(wù)器的接收與轉(zhuǎn)發(fā)便可以實(shí)現(xiàn)客戶之間的通信。對于整體系統(tǒng)的流程,也是分為兩部分, 即客戶端的發(fā)起請求,服務(wù)器響應(yīng)請求。首先是客戶端的登陸,客戶端在登陸時(shí),必須設(shè)置所想要連接的服務(wù)器,然后客戶端就會(huì)根據(jù)設(shè)置的服務(wù)器 IP 地址,向服務(wù)器發(fā)起建立連接請求。 而服務(wù)器端是始終在監(jiān)聽網(wǎng)絡(luò)狀態(tài)的,主要檢測到有一個(gè)連接請求,那么服務(wù)器就會(huì)給當(dāng)前的發(fā)起端分配一個(gè)服務(wù)線程,用于處理

6、它的各種請求。對于客戶端的各種請求, 實(shí)際上都是通過在客戶發(fā)往服務(wù)器的各種字符流區(qū)分的,具體的方法就是在消息的頭部以及尾部添加特殊字符串,從而實(shí)現(xiàn)服務(wù)器對消息請求的識(shí)別。比如對于登陸信息,消息的頭部就是“ PEOPLO” ,而對于私聊中的消息,頭部就是“ SEC”,其他的同理都添加了頭部。當(dāng)然,對于客戶端來說,這些都是透明的,用戶的操作并沒有受到任何影響。在服務(wù)器端,消息被檢測分析后,變回根據(jù)具體的目的進(jìn)行處理,比如是私聊消息,服務(wù)器便會(huì)根據(jù)其尾部的目的信息,向目標(biāo)端轉(zhuǎn)發(fā)該條消息。這里額外要說的是,該系統(tǒng)特別的對用戶發(fā)來的消息進(jìn)行了加密處理,即用戶的各種具體信息,不僅能夠防止向第三方的泄漏,而

7、且即使服務(wù)器端出現(xiàn)異常,信息也不會(huì)泄漏。對于私聊的方式,該系統(tǒng)所采用的方法類似于QQ 的通信方式,由發(fā)起方主動(dòng)連接,當(dāng)目的方接收到連接請求后,會(huì)主動(dòng)建立一個(gè)私聊窗口,從而實(shí)現(xiàn)私聊。2.2 總體設(shè)計(jì)流程圖ServerClient建立服務(wù)器建立連接等待并接收創(chuàng)建 socket接收請求后創(chuàng)建socket 連接開始通信InputStreamInputStreamOutputStreamOutputStream關(guān)閉 socket結(jié)束通信關(guān)閉 socket學(xué)生信息管理軟件詳細(xì)設(shè)計(jì)3.1. 界面設(shè)計(jì)由于這里界面設(shè)計(jì)源代碼比較簡單,這里不再列出。3.1.1 服務(wù)器界面設(shè)計(jì)服務(wù)器界面3.1.2 客戶端界面設(shè)計(jì)登

8、陸界面注冊界面聊天室界面3.2 通信功能實(shí)現(xiàn)方法列舉部分實(shí)現(xiàn)通信功能的主要代碼AppServer.java 文件publicclassAppServerextendsThread privateServerSocketserverSocket;privateServerFramesFrame ;privatestaticVectoruserOnline=new Vector(1, 1);privatestaticVectorv =new Vector(1, 1);/* 創(chuàng)建服務(wù)器 啟動(dòng)服務(wù)監(jiān)聽 1001 端口*/publicAppServer() sFrame =new ServerFrame

9、();tryserverSocket=new ServerSocket(1001);/ 獲取服務(wù)器的主機(jī)名和 IP 地址InetAddress address = InetAddress. getLocalHost sFrame . txtServerName .setText(address.getHostName(); sFrame . txtIP .setText(address.getHostAddress(); sFrame . txtPort .setText( 1001 ); catch (IOException e) ();fail(e, 不能啟動(dòng)服務(wù)! );sFrame .

10、txtStatus.setText( 已啟動(dòng) .);this.start();/啟動(dòng)線程/* 退出服務(wù)器* param e*異常* param str*退出信息*/publicstaticvoidfail(Exception e, String str) System. out .println(str +。 + e);/*監(jiān)聽客戶的請求,當(dāng)有用戶請求時(shí)創(chuàng)建Connection線程*/publicvoidrun() trywhile( true ) / 監(jiān)聽并接受客戶的請求Socket client =serverSocket.accept();new Connection(sFrame ,

11、client,userOnline,v);/支持多線程/ System.out.println(userLength:+userLength);catch(IOException e) System. out .println( 不能監(jiān)聽 );/* 啟動(dòng)服務(wù)器*/publicstaticvoidmain(String args) new AppServer();ServerFrame.java文件publicclassServerFrameextendsJFrameimplementsActionListener publicJListlist;/*/privatestaticfinallon

12、gserialVersionUID=-8936397327038098620L;/ 服務(wù)器信息面板JPanelpnlServer,pnlServerInfo;JLabellblStatus,lblNumber,lblMax ,lblServerName,lblProtocol,lblIP,lblPort,lblLog;publicJTextFieldtxtStatus,txtNumber,txtMax ,txtServerName,txtProtocol,txtIP,txtPort;JButtonbtnStop ,btnSaveLog ;publicTextAreataLog ;JTabbed

13、PanetpServer;publicTextAreataMessage ;/ 用戶信息面板JPanelpnlUser;publicJLabellblMessage,lblUser,lblNotice,lblUserCount;JListlstUser;JScrollPanespUser ;JTextFieldtxtNotice;JButtonbtnSend ,btnKick;publicStringti= ;publicStringserverMessage= ;publicServerFrame() / 服務(wù)器窗口Toolkit.super ( HappyChat聊天服務(wù)器 );setSi

14、ze(550, 500);setDefaultCloseOperation(JFrame.setResizable(false);Dimension scr =getDefaultToolkit().getScreenSize();Dimension fra =this.getSize();if(fra.width scr.width ) fra.width= scr.width ;EXIT_ON_CLOSE);/在屏幕居中顯示if(fra.height scr.height) fra.height= scr.height;this.setLocation(scr. (scr. height-

15、 fra.width- fra.width ) / 2,height) / 2);/ =服務(wù)器信息面板 =pnlServer=new JPanel();pnlServer.setLayout(null);pnlServerInfo=new JPanel(new GridLayout(14, 1);pnlServerInfo.setBorder(BorderFactory.createCompoundBorderBorderFactory.createTitledBorder( ), BorderFactory. createEmptyBorder(1, 1, 1, 1);(lblStatust

16、xtStatustxtStatus=new JLabel( 當(dāng)前狀態(tài) : );=new JTextField(10);.setEditable(false);lblNumber=new JLabel( 當(dāng)前在線人數(shù): );txtNumber = new JTextField( txtNumber .setEditable( false0);人 , 10);lblMax=new JLabel( 最多在線人數(shù): );txtMax = new JTextField( txtMax .setEditable( false50);人 , 10);lblServerNametxtServerNametxt

17、ServerName=new JLabel( 服務(wù)器名稱 : );=new JTextField(10);.setEditable(false);lblProtocoltxtProtocoltxtProtocol=new JLabel( 訪問協(xié)議 : );=new JTextField(HTTP , 10);.setEditable(false);lblIP=new JLabel( 服務(wù)器 IP:);txtIP=new JTextField(10);txtIP.setEditable(false);lblPort=new JLabel( 服務(wù)器端口 : );txtPort=new JTextF

18、ield(8000 , 10);txtPort.setEditable(false);btnStop=new JButton( 關(guān)閉服務(wù)器 (C) );btnStop.addActionListener(new ActionListener() publicvoidactionPerformed(ActionEvent arg0) closeServer(););lblLog=new JLabel( 服務(wù)器日志 );taLog=new TextArea(20, 50);btnSaveLog=new JButton( 保存日志 (S) );btnSaveLog .addActionListene

19、r(new ActionListener() publicvoidactionPerformed(ActionEvent arg0) saveLog(););pnlServerInfo.add(lblStatus);pnlServerInfo.add(txtStatus);pnlServerInfo.add(lblNumber);pnlServerInfo.add(txtNumber);pnlServerInfo.add(lblMax);pnlServerInfo.add(txtMax);pnlServerInfo.add(lblServerName);pnlServerInfo.add(tx

20、tServerName);pnlServerInfo.add(lblProtocol);pnlServerInfo.add(txtProtocol);pnlServerInfo.add(lblIP);pnlServerInfo.add(txtIP);pnlServerInfo.add(lblPort);pnlServerInfo.add(txtPort);pnlServerInfo.setBounds(5, 5, 100, 400);lblLog.setBounds(110, 5, 100, 30);taLog .setBounds(110, 35, 400, 370);btnStop.set

21、Bounds(200, 410, 120, 30);btnSaveLog.setBounds(320, 410, 120, 30);pnlServer.add(pnlServerInfo);pnlServer.add(lblLog);pnlServer.add(taLog );pnlServer.add(btnStop);pnlServer.add(btnSaveLog);/ = 在線用戶面板 =pnlUser = new JPanel();pnlUser.setLayout(null);lblMessage=new JLabel( 用戶消息 );taMessage=new TextArea(

22、20, 20);lblNotice=new JLabel(通知:);txtNotice=new JTextField(20);btnSend=new JButton( 發(fā)送 (S) );btnSend.setEnabled(true);btnSend.addActionListener(new ActionListener() publicvoid actionPerformed(ActionEvent arg0) serverMessage(););lblUserCount=new JLabel( 在線總?cè)藬?shù)0人 );btnKick=new JButton( 踢人 (K);btnKick.a

23、ddActionListener(new ActionListener() publicvoidactionPerformed(ActionEvent arg0) tiRen(););lblUser=new JLabel( 在線用戶列表 );lstUser=new JList();lstUser.setVisibleRowCount(17);lstUser.setFixedCellWidth(180);/ 指定的固定單元格寬度值,而不是從列表元素計(jì)算而來的寬度值lstUser.setFixedCellHeight(18);spUser =new JScrollPane();spUser .se

24、tVerticalScrollBarPolicy(JScrollPane.R_ALWAYS);spUser .getViewport().setView(lstUser);VERTICAL_SCROLLBAlblMessage.setBounds(5, 5, 100, 25);taMessage .setBounds(5, 35, 300, 360);lblUser.setBounds(310, 5, 100, 25);spUser .setBounds(310, 35, 220, 360);lblNotice.setBounds(5, 410, 40, 25);txtNotice.setBo

25、unds(50, 410, 160, 25);btnSend .setBounds(210, 410, 80, 25);lblUserCount.setBounds(320, 410, 100, 25);btnKick.setBounds(440, 410, 80, 25);pnlUser.add( lblMessage);pnlUser.add( taMessage );pnlUser.add( lblUser);pnlUser.add( spUser );list=new JList();list.setListData(new String );spUser .setViewportVi

26、ew(list);pnlUser.add(lblNotice);pnlUser.add(txtNotice);pnlUser.add(btnSend );pnlUser.add(lblUserCount );pnlUser.add(btnKick );/ =主標(biāo)簽面板 =tpServer= new JTabbedPane(JTabbedPane.TOP);tpServer.add( 服務(wù)器管理 ,pnlServer);tpServer.add( 用戶信息管理 , pnlUser );this .getContentPane().add(tpServer);setVisible(true );p

27、rotectedvoidserverMessage() / TODO自動(dòng)生成方法存根this. serverMessage=txtNotice.getText();txtNotice.setText( );protectedvoidcloseServer() / TODO自動(dòng)生成方法存根this.dispose();/this.resprotectedvoidsaveLog() / TODO自動(dòng)生成方法存根tryFileOutputStream fileoutput =newFileOutputStream(log.txt,true );String temp =taMessage .getT

28、ext();fileoutput.write(temp.getBytes();fileoutput.close();JOptionPane.showMessageDialog( null, 記錄保存在log.txt);catch(Exception e) System. out .println(e);protectedvoidtiRen() / TODO自動(dòng)生成方法存根ti=list.getSelectedValue().toString();log(Ti:+ti );privatevoidlog(String string) / TODO自動(dòng)生成方法存根String newta =taMe

29、ssage .getText();newta += (n+string);taMessage .setText(newta);publicvoidactionPerformed(ActionEvent evt) publicstaticvoidmain(String args) new ServerFrame();Connection.java文件publicclassConnectionextendsThread /* 與客戶端通訊 Socket*/privateSocketnetClient;/* 在線用戶列表*/privateVectoruserOnline;/* 聊天信息*/priva

30、teVectoruserChat;/* 從客戶到服務(wù)器 輸入流*/privateObjectInputStreamfromClient;/* 傳到客戶端 打印流*/privatePrintStreamtoClient;/* 注冊用戶列表*/privatestaticVectorvList=new Vector();/* 臨時(shí)對象*/privateObjectobj ;/* 服務(wù)器日志窗體*/privateServerFramesFrame ;SuppressWarnings( unchecked)/* 創(chuàng)建與客戶端通訊*/publicConnection(ServerFrame frame,

31、Socket client, Vector u,Vector c) netClient= client;userOnline= u;userChat= c;sFrame = frame;try/ 發(fā)生雙向通信/ 檢索客戶輸入fromClient=newObjectInputStream(netClient.getInputStream();/ 服務(wù)器寫到客戶toClient=new PrintStream(catch(IOException e) trynetClient.close();catch(IOException e1) netClient.getOutputStream();Sys

32、tem. out .println( 不能建立流 + e1);return;this.start();/* 處理與客戶端的通訊線程*/publicvoidrun() /try / obj是 Object 類的對象obj = (Object)fromClient.readObject();if( obj .getClass().getName().equals(serverLogin();serverLoginDB();Customer) if( obj .getClass().getName().equals(Register_Customer)/serverRegiste();serverR

33、egisteDB();if( obj .getClass().getName().equals(Message ) serverMessage();if( obj .getClass().getName().equals(Chat) serverChat();if( obj .getClass().getName().equals(Exit) serverExit();catch(IOException e) System. out .println(e);catch(ClassNotFoundException e1) System. out .println( 讀對象發(fā)生錯(cuò)誤! + e1)

34、;finallytrynetClient.close();catch(IOException e) System. out .println(e);/*登錄處理*/SuppressWarnings( deprecationpublicvoidserverLogin() )tryCustomer clientMessage2 = (Customer)obj ;/讀文件FileInputStream file3 =new FileInputStream(user.txt);ObjectInputStream objInput1 =newObjectInputStream(file3);vList=

35、 (Vector) objInput1.readObject();intfind = 0;/查找判斷標(biāo)志/ System.out.println(find);for( inti = 0; i vList.size(); i+) Register_Customer reg = (Register_Customer)vList.elementAt(i);if(reg.custName .equals(clientMessage2.find = 1;ifcustName ) (!reg.custPassword.equals(clientMessage2.custPassword) toClient.println( 密碼不正確 );break ; else / 判斷是否已經(jīng)登錄intlogin_flag = 0;for( inta = 0; a = 50) toClient.println( 登錄人數(shù)過多,請稍候再試 );break ;if(login_flag = 0) clientMessage2.custHead= reg.head ; /getUserHeadByName(clientMessage2.custName);userOnline.ad

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(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ǔ)空間,僅對用戶上傳內(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論