Java網(wǎng)絡(luò)通信技術(shù)實現(xiàn)基于CS模式的聊天室_第1頁
Java網(wǎng)絡(luò)通信技術(shù)實現(xiàn)基于CS模式的聊天室_第2頁
Java網(wǎng)絡(luò)通信技術(shù)實現(xiàn)基于CS模式的聊天室_第3頁
Java網(wǎng)絡(luò)通信技術(shù)實現(xiàn)基于CS模式的聊天室_第4頁
Java網(wǎng)絡(luò)通信技術(shù)實現(xiàn)基于CS模式的聊天室_第5頁
免費預(yù)覽已結(jié)束,剩余24頁可下載查看

下載本文檔

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

文檔簡介

1、僅供個人參考學(xué)號5114武漢科技大學(xué)城市學(xué)院課程設(shè)計報告課程設(shè)計名稱Java課程設(shè)計采用Java網(wǎng)絡(luò)通信技術(shù)實現(xiàn)基于C/S模題目式的聊天空院系信息工程學(xué)部專 業(yè)信息管理與信息系統(tǒng)班級 1>姓名 張峰指導(dǎo)教師 于海平2010年 01月 07 日編號:036題目:采用Java網(wǎng)絡(luò)通信技術(shù)實現(xiàn)基于C/S模式的聊天室1 課程設(shè)計教學(xué)條件要求eclipse/ netbeans/ Jbuilder2課程設(shè)計任務(wù)編寫一個簡易CS版聊天室,多個客戶端可以同時發(fā)送信息,服務(wù)器端監(jiān)聽并顯 示到客戶端頁面。1 .要求可以實現(xiàn)多人同時在線聊天。2 .要求客戶端和服務(wù)器端的界面采用 Swing或SWT實現(xiàn)3 .服

2、務(wù)器可以向多個客戶進行廣播通信。3課程設(shè)計報告書主要內(nèi)容1需求分析2總體設(shè)計2.1設(shè)計的總體思想與算法描述2.2模塊結(jié)構(gòu)圖2.3各功能模塊的功能與處理流程描述2.4界面設(shè)計3各功能模塊程序設(shè)計按照功能模塊的功能與處理流程描述給出詳細的程序代碼,并給出重點語句的注釋.4小結(jié)4課程設(shè)計要求1按時到機房簽到,在指定機位上機。遵守機房紀律。2 獨立完成課程設(shè)計任務(wù)。指導(dǎo)教師到機位上指導(dǎo)學(xué)生和分時段驗收學(xué)生完成的程序。3 按時提交打印的課程設(shè)計報告書。5課程設(shè)計參考書1許文憲懂子建.Java程序設(shè)計教程與實訓(xùn).北京:北京大學(xué)出版社,2005.2辛運幃.Java程序設(shè)計.北京:清華大學(xué)出版社,20043美

3、Echel,B .Java編程思想(第三版).北京:機械工業(yè)出版社,2005撰寫者:江偉 指導(dǎo)老師:于海平不得用于商業(yè)用途僅供個人參考1 需求分析 錯誤! 未定義書簽。2 總體設(shè)計 錯誤! 未定義書簽。2 1設(shè)計的總體思想與算法描述 錯誤!未定義書簽。2 2模塊結(jié)構(gòu)圖 錯誤!未定義書簽。2. 3 界面設(shè)計 錯誤!未定義書簽。2.3.1 管理員界面錯誤.!2.3.2 登陸界面錯誤.!2.3.3 聊天界面錯誤.!2.3.4 文件發(fā)送界面錯誤.!2.3.5 接受提示界面錯誤.!2.3.6 選擇保存界面錯誤.!2.3.7 傳輸成功提示界面 錯誤.!3 程序設(shè)計 錯誤 ! 未定義書簽。4 小結(jié) 錯誤!

4、未定義書簽。參考文獻錯誤 ! 未定義書簽。1 需求分析本系統(tǒng)是實現(xiàn)c/s 模式的聊天室首先要建立偵聽服務(wù),這個需求是服務(wù)器端的需求。其次還應(yīng)實現(xiàn):用戶登錄, 包括客戶端請求登錄以及服務(wù)器端響應(yīng)登錄兩個過程。收發(fā)聊天信息,包括客戶端發(fā)送聊天信息以及服務(wù)器端轉(zhuǎn)發(fā)聊天信息兩個過程。收發(fā)系統(tǒng)信息,由服務(wù)器端發(fā)送給客戶端的信息。用戶下線,包括客戶端請求下線以及服務(wù)器響應(yīng)請求兩個過程,這個與用戶登錄類似, 服務(wù)器關(guān)閉。其中我還加入了文件傳輸功能,方便了用戶文件的共享。For personal use only in study and research; not for commercial use不得用

5、于商業(yè)用途僅供個人參考2總體設(shè)計2 . 1設(shè)計的總體思想與算法描述依據(jù)需求分析的結(jié)果,該系統(tǒng)應(yīng)設(shè)計成一個可以實現(xiàn)多人同時在線聊天功能 的聊天軟件,下面從軟件的整體結(jié)構(gòu)設(shè)計、界面設(shè)計、變量設(shè)計等方面闡述一下 系統(tǒng)的總體設(shè)計。依據(jù)需求分析結(jié)果,設(shè)計應(yīng)實現(xiàn)多人同時在線聊天,就應(yīng)該分 為服務(wù)端和用戶端兩個端口。用戶端把信息發(fā)送到服務(wù)端,而后服務(wù)端再把信息 分發(fā)到各個用戶端。3 . 2模塊結(jié)構(gòu)圖圖2.1模塊結(jié)構(gòu)圖不得用于商業(yè)用途2. 3界面設(shè)計2.3.1管理員界面圖2.2管理員界面2.3.2登陸界面圖2.3登陸界面2.3.3聊天界面圖2.4聊天界面2.3.4文件發(fā)送界面圖2.5文件發(fā)送界面2.3.5接受

6、提示界面圖2.6接受提示界面2.3.6選擇保存界面圖2.7選擇保存界面2.3.7傳輸成功提示界面圖2.8傳輸成功提示界面僅供個人參考4 程序設(shè)計package com.ff;import java.io.*;import .*;import java.util.*;import javax.swing.*;import java.awt.*;import ;public class StartServer public static void main(String args) StartServer startServer = new StartServer();StartServer.Se

7、rver serverFrame=startServer.new Server();class Server extends JFrameprivate JScrollPane jp_rec;private JScrollPane jp_send;private JScrollPane jp_list;private JTextArea jta_rec;private JTextArea jta_send;private JLabel lbl_rec;private JLabel lbl_edit;private JList jlist;private JButton btnSend;/ 發(fā)送

8、消息按鈕private JButton btnT;private DefaultListModel listModel;private HashMap<String,SocketThread> allUserLink = new HashMap<String,SocketThread>();public Server() init();try ServerSocket server_socket = new ServerSocket(9999);while (true) Socket client_socket = server_socket.accept();Sock

9、etThread dealThread = new SocketThread( client_socket);new Thread(dealThread).start(); catch (Exception ex) ex.printStackTrace();public void init() this.setTitle("管理員平臺");this.setSize(800, 400);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setLocationRelativeTo(this);Container c = this

10、.getContentPane();c.setLayout(null);jta_rec = new JTextArea("");jta_send = new JTextArea("");jp_rec = new JScrollPane();jp_send = new JScrollPane();jp_list = new JScrollPane();lbl_rec = new JLabel("-消息記錄-");lbl_edit = new JLabel("-文本編輯-");btnSend = new JButton

11、('發(fā)送");btnT = new JButton("T 除成員 ");listModel=new DefaultListModel();jlist = new JList(listModel);jlist.setBorder(BorderFactory.createTitledBorder(在線成員");jp_rec.getViewport().add(jta_rec);jp_send.getViewport().add(jta_send);jp_list.getViewport().add(jlist);jta_rec.setLineWrap

12、(true);jta_send.setLineWrap(true);jta_rec.setEditable(false);lbl_rec.setBounds(30, 10, 80, 20);jp_rec.setBounds(30, 30, 450, 100);lbl_edit.setBounds(30, 130, 80, 20);jp_send.setBounds(30, 150, 450, 100);btnSend.setBounds(350, 260, 80, 20);jp_list.setBounds(500, 30, 120, 220);btnT.setBounds(520, 260,

13、 80, 20);c.add(lbl_rec);c.add(lbl_edit);c.add(jp_rec);c.add(jp_send);c.add(btnSend);c.add(jp_list);c.add(btnT);this.setVisible(true);class SocketThread extends BaseTrans private boolean threadON=true;public SocketThread(Socket client_socket) super(client_socket);btnSend.addActionListener(new ActionL

14、istener()public void actionPerformed(ActionEvent e) messageEvent("code=001;msg=< X X X 公告 "+new Date().toLocaleString()+" rn"+jta_send.getText();jta_send.setText(""););btnT.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) if(jlist.getS

15、electedValue()!=null)SocketThreadst=allUserLink.get(jlist.getSelectedValue().toString().trim();allUserLink.remove(jlist.getSelectedValue().toString().trim();st.sendMessage("code=0");messageEvent("code=001;msg<公告 "+new Date().toLocaleString()+" rn"+jlist.getSelectedVa

16、lue()+"被管理員踢了出去!");st.closeLink();threadON=false;listModel.remove(jlist.getSelectedIndex(););public void messageEvent(String msg) StringUtil stringUtil = new StringUtil();stringUtil.AnalysisMessage(msg);String code = stringUtil.getValueByKey("code");if (code.equalsIgnoreCase(&quo

17、t;000") "進入了 001");listModel.addElement(stringUtil.getValueByKey("userName");for (Iterator<String> it =allUserLink.keySet().iterator(); it.hasNext();) allUserLink.get(it.next().sendMessage("code=001;msg*公 告 "+new Date().toLocaleString()+" rn"+string

18、Util.getValueByKey("userName") +"進入了聊天室,大家歡迎他(她 )吧 !");allUserLink.put(stringUtil.getValueByKey("userName"),this);jta_rec.setText(jta_rec.getText()+"rn"+"X X X X 公告"+new Date().toLocaleString()+" rn"+stringUtil.getValueByKey("userName&

19、quot;)+" 進入了聊天室,大家歡迎他(她 )吧 !");jta_rec.setCaretPosition(jta_rec.getText().length();else if (code.equalsIgnoreCase("001") "進入了 001");for (Iterator<String> it =allUserLink.keySet().iterator(); it.hasNext();) allUserLink.get(it.next().sendMessage(msg);jta_rec.setText

20、(jta_rec.getText()+"rn"+stringUtil.getValueByKey("msg");jta_rec.setCaretPosition(jta_rec.getText().length();else if (code.equalsIgnoreCase("010") "進入了010");this.setFileName(stringUtil.getValueByKey("fileName");this.setFileLength(Integer.parseInt(stri

21、ngUtil.getValueByKey("fileSize");initFileOutputStream("./");sendMessage("code=011"); else if (code.equalsIgnoreCase("012") "進入了012");sendMessage("code=013");setSendFileFlag(true); else if (code.equalsIgnoreCase("014") "進入了014

22、");for (Iterator<String> it =allUserLink.keySet().iterator(); it.hasNext();) if (this != allUserLink.get(it.next() SocketThread st=allUserLink.get(it.next();st.setFileName(getFileName();st.setFileLength(getFileLength();st.sendMessage("code=015;username="+ stringUtil.getValueByKe

23、y("username")+ "fileName= + getFileName() + "fileSize=" + getFileLength(); else if (code.equalsIgnoreCase("016") "進入了 016");sendFileData("./" + this.getFileName();sendMessage("code=018"); else if (code.equalsIgnoreCase("017")

24、 for (Iterator<String> it =allUserLink.keySet().iterator();it.hasNext();) if (this != allUserLink.get(it.next() allUserLink.get(it.next().initNet();public void run() while (threadON) receive();客戶端代碼package com.ff;import java.io.*;import .*;import ;import java.awt.*;import ;import javax.swing.*

25、;public class StartClient public static void main(String args)StartClient sc=new StartClient();StartClient.Client client=sc.new Client();class Clientpublic Client()init();public void init()trySocketclient_socket=newSocket(InetAddress.getLocalHost(),9999);new Thread(new ChatPanel(client_socket).start

26、();catch(Exception ex)ex.printStackTrace();class ChatPanel extends BaseTransprivate String userName;private JFramejThis;private JLabellbl_rec;private JLabellbl_edit;private JTextArea jta_rec;/K收消息框private JTextArea jta_send;/發(fā)送消息框private JTextField jtxt_file;private JScrollPane jp_rec;private JScrol

27、lPane jp_send;private JButton btnSelFile;/他擇文件private JButton btnSend;/發(fā)送消息按鈕private JFileChooser jfc;/3t件選擇器public ChatPanel(Socket client_socket)super(client_socket);userName=JOptionPane.showInputDialog(B輸入用戶名:");init();public void init()jThis=new JFrame();jThis.setTitle("你好:"+userN

28、ame+”歡迎進入聊天程序");jThis.setSize(600,400);jThis.setDefaultCloseOperation(jThis.EXIT_ON_CLOSE);jThis.setLocationRelativeTo(jThis);Container c=jThis.getContentPane();c.setLayout(null);lbl_rec=new JLabel("-消息記錄-");lbl_edit=new JLabel("-文本編輯-");jtxt_file=new JTextField(""

29、);jta_rec=new JTextArea("");jta_send=new JTextArea("");jp_rec=new JScrollPane();jp_send=new JScrollPane();btnSelFile=new JButtonC文件.");btnSend=new JButton('發(fā)送");jfc=new JFileChooser();jp_rec.getViewport().add(jta_rec);jp_send.getViewport().add(jta_send);jta_rec.setL

30、ineWrap(true);jta_send.setLineWrap(true);jtxt_file.setEditable(false);jta_rec.setEditable(false);jfc.setDialogTitle(" 請選擇要傳送的文件");lbl_rec.setBounds(30,10,80,20);jp_rec.setBounds(30,30, 450, 100);lbl_edit.setBounds(30,130,80,20);jp_send.setBounds(30,150,450,100);jtxt_file.setBounds(30,260,1

31、50,20);btnSelFile.setBounds(180,260,80,20);btnSend.setBounds(350,260,80,20);btnSelFile.addActionListener(new ActionListener()public void actionPerformed(ActionEvent ae)jfc.showOpenDialog(null);jtxt_file.setText(jfc.getSelectedFile().getPath()+""););c.add(lbl_rec);c.add(lbl_edit);c.add(jp_r

32、ec);c.add(jp_send);c.add(jtxt_file);c.add(btnSelFile);c.add(btnSend);btnSend.addActionListener(new ActionListener()public void actionPerformed(ActionEvent ae)sendMessage("code=001;msg="+userName+" "+newDate().toLocaleString()+"rn"+jta_send.getText();jta_send.setText(&qu

33、ot;");if(jtxt_file.getText().length()>4)File file=new File(jtxt_file.getText().trim();sendMessage("code=010;fileName="+file.getName()+"fileSize="+file.length();sendMessage("code=000;username="+userName);jThis.setVisible(true);public void messageEvent(String msg)

34、 StringUtil stringUtil=new StringUtil();stringUtil.AnalysisMessage(msg);String code=stringUtil.getValueByKey("code");if(code.equalsIgnoreCase("0")this.closeLink();JOptionPane.showMessageDialog(jThis,您已近被管理員 T 走!");jThis.dispose();System.exit(0);else if(code.equalsIgnoreCase(

35、"001")"進入到001");jta_rec.setText(jta_rec.getText()+"rn"+stringUtil.getValueByKey("msg");jta_rec.setCaretPosition(jta_rec.getText().length();else if(code.equalsIgnoreCase("011")"進入到011");sendMessage("code=012");else if(code.equalsIg

36、noreCase("013")"進入到了013");sendFileData(jtxt_file.getText().trim();this.initNet();jtxt_file.setText("");sendMessage("code=014;username="+userName);else if(code.equalsIgnoreCase("015")"進入到了015");setFileName(stringUtil.getValueByKey("file

37、Name");setFileLength(Integer.parseInt(stringUtil.getValueByKey("fileSize");intresult=JOptionPane.showConfirmDialog(null,userName+","+stringUtil.getValueByK ey("username")+"發(fā) 文件【"+getFileName()+”】 給你,要接受嗎?","提示 ",JOptionPane.OK_CANCEL_OPTION

38、);if(result=JOptionPane.OK_OPTION)JFileChooser jfc=new JFileChooser();jfc.setDialogTitle(" 請選擇保存的目錄.");jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);jfc.showOpenDialog(jThis);String path=jfc.getSelectedFile().getPath();initFileOutputStream(path+"/");sendMessage("co

39、de=016");setSendFileFlag(true);else if(result=JOptionPane.CANCEL_OPTION)sendMessage("code=017");else if(code.equalsIgnoreCase("018")JOptionPane.showMessageDialog(jThis,userName+", 文 件 【 "+getFileName()+" 】 傳 輸 成 功 "," 提 示 ",JOptionPane.INFORMATION_MESSAGE);/jta_rec.setText(jta_rec.getText()+"rn"+msg);public void run() while(true)receive();不得用于商業(yè)用途5 小結(jié)每次課程設(shè)計都是一個學(xué)習的升華,這次我做的課題是網(wǎng)絡(luò)通信技術(shù)實現(xiàn)基于 C/S 模式的聊天室程序設(shè)計。為了使課程設(shè)計能夠順利的進行,剛拿到任務(wù)書,我就開始復(fù)習課本和軟件工程中的相關(guān)知識,并去圖書館查閱了很多資料,大致熟悉了課題所需要的知識點。然后擬定JAVA設(shè)計的框架,全面布局設(shè)計的

溫馨提示

  • 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論