




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、.網(wǎng)絡(luò)程序設(shè)計(jì)考試大作業(yè) 題目:聊天室程序 班級: 學(xué)號: 姓名: 成績:網(wǎng)絡(luò)程序設(shè)計(jì)考試大作業(yè)1一所使用的背景知識、主要函數(shù)的描述3二 程序設(shè)計(jì)思想及程序設(shè)計(jì)流程框圖3三主要代碼及代碼運(yùn)行結(jié)果41.啟動(dòng)服務(wù)器42. 登錄63. 注冊104. 登錄和注冊判定125. 進(jìn)入聊天界面136. 私聊頁面17一所使用的背景知識、主要函數(shù)的描述背景:根據(jù)現(xiàn)在最流行的聊天工具QQ,模仿一部分主要功能來完成。主要函數(shù):public class Server;服務(wù)器的創(chuàng)建。public class Client;客戶端的創(chuàng)建。public class Main extends JFrame;登錄界面的顯示。p
2、ublic class Regist extends JDialog;注冊界面的顯示。public class UserInformation;用戶信息的保存和驗(yàn)證。public class AllTalkFrame extends JFrame;登錄后進(jìn)入群聊界面。public class PointToPointTalkFrame extends JFrame;私聊界面。二程序設(shè)計(jì)思想及程序設(shè)計(jì)流程框圖設(shè)計(jì)思想:利用socket與server socket在客戶端與客戶端之間的通信,InputStream InputStreamReader輸入輸出流進(jìn)行信息的發(fā)送與接收。程序設(shè)計(jì)流程:主頁
3、面:輸入賬號與密碼,點(diǎn)擊登錄或者注冊進(jìn)入下一頁面。登錄:判定是否正確,正確則進(jìn)去聊天界面。注冊:進(jìn)去注冊界面,成功則返回主頁面。進(jìn)入聊天室:能發(fā)送信息讓在線的所有人看到。私聊界面:能與一個(gè)人單獨(dú)聊天,信息只能被雙方看到。主頁面注冊登錄進(jìn)入聊天室點(diǎn)擊名字進(jìn)入私聊三主要代碼及代碼運(yùn)行結(jié)果1.啟動(dòng)服務(wù)器代碼:public class Server ServerSocket server;static int clientNum = 0;/ 存放與服務(wù)器連接上的對應(yīng)的Socket,作用是保存服務(wù)器與客戶端之間的流,便于服務(wù)器給每個(gè)客戶端進(jìn)行回發(fā)消息List clientConnection = new
4、 ArrayList();public Server() try server = new ServerSocket(9999);System.out.println(服務(wù)器已經(jīng)啟動(dòng)); catch (IOException e) e.printStackTrace();System.out.println(服務(wù)器啟動(dòng)失敗);/ 內(nèi)部類,監(jiān)聽客戶端是否有連接到服務(wù)器,并將此客戶端的Socket傳遞給HandleSocket進(jìn)行處理,同時(shí)將client存放到List中,即clientConnection中class SocketListener implements Runnable publi
5、c void run() Socket client;try while (true) client = server.accept();/ 連接上一個(gè)就壓入List中,即clientConnection中clientConnection.add(client);HandleSocket hs = new HandleSocket(client);/ 連接上就讓HandleSocket去處理new Thread(hs).start(); catch (IOException e) System.out.println(客戶連接服務(wù)器失敗);/ 內(nèi)部類 處理一個(gè)Socket,接收一個(gè)Client
6、發(fā)送過來的消息,并且服務(wù)器原封不動(dòng)的返回給所有客戶端,客戶端對消息進(jìn)行過濾class HandleSocket implements Runnable Socket client;HandleSocket(Socket client) this.client = client;public void run() try clientNum+;/ 啟用輸入流InputStream is = client.getInputStream();InputStreamReader isr = new InputStreamReader(is);BufferedReader br = new Buffer
7、edReader(isr);System.out.println(第 + clientNum + 個(gè)客戶端連接進(jìn)入服務(wù)器);boolean flag = true;String s;do / 對用戶發(fā)來的消息進(jìn)行群發(fā)給客戶端s = br.readLine();System.out.println(接受到一個(gè)客戶端消息: + s);for (int i = 0; i clientConnection.size(); i+) Socket client = clientConnection.get(i);OutputStream os = client.getOutputStream();Prin
8、tStream ps = new PrintStream(os);ps.println(s); while (flag);client.close(); catch (IOException e) System.out.println(有一個(gè)客戶斷開與服務(wù)器的連接);界面:2. 登錄代碼:package com.qq.main;import java.awt.Color;import java.awt.Dimension;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionL
9、istener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPasswordField;import javax.swing.JTextField;import com.qq.regist.Regist;import com.qq.regist.UserInformation;/* * 主界面 */public class Main extends JFrame /組件的內(nèi)容pri
10、vate JLabel userId;private JLabel userPassword;private JTextField inputId;private JPasswordField inputPassword;private JButton btLogin;private JButton btRegist;Main() userId = new JLabel(帳號);userPassword = new JLabel(密碼);inputId = new JTextField(6);inputPassword = new JPasswordField();btLogin = new
11、JButton(登陸);btRegist = new JButton(注冊);/ 設(shè)置窗體屬性Toolkit tk = Toolkit.getDefaultToolkit();Dimension screenSize = tk.getScreenSize();/得到當(dāng)前屏幕的長和寬int x = (int) screenSize.getWidth();int y = (int) screenSize.getHeight();this.setBounds(x - 240) / 2, (y - 600) / 2, 240, 600);/窗口顯示的大小 ,位置this.setResizable(fa
12、lse);/窗口大小不能改變this.setLayout(null);/默認(rèn)的格式this.setBackground(Color.BLACK);/ 窗口的顏色this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/退出程序/ 設(shè)置JLabel屬性userId.setBounds(30, 160, 40, 20);userPassword.setBounds(30, 200, 40, 20);/ 設(shè)置文本域?qū)傩詉nputId.setBounds(90, 160, 100, 20);inputPassword.setBounds(90, 200,
13、 100, 20);inputPassword.setEchoChar(*);/用*顯示代替你輸入的密碼/ 設(shè)置JButton屬性btLogin.setBounds(50, 240, 60, 20);btRegist.setBounds(120, 240, 60, 20);/ 注冊“登陸”按鈕監(jiān)聽器btLogin.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) UserInformation user = new UserInformation();String userName
14、 = inputId.getText();String userPassword = new String(inputPassword.getPassword();if (userName.equals() JOptionPane.showMessageDialog(null, 用戶名不能為空); else if (.equals(userPassword) JOptionPane.showMessageDialog(null, 密碼不能為空); else if (user.isExist(userName)& user.userInfomation.getProperty(userName)
15、.equals(userPassword) new AllTalkFrame(userName).setVisible(true);/ 判斷成功后new一個(gè)群聊窗口Main.this.dispose(); else JOptionPane.showMessageDialog(null, 此用戶名不存在或者密碼不正確););/ 注冊“注冊”按鈕監(jiān)聽器btRegist.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) new Regist();/注冊頁面);this.add(userI
16、d);this.add(userPassword);this.add(inputId);this.add(inputPassword);this.add(btLogin);this.add(btRegist);this.setVisible(true);public static void main(String args) new Main();界面:3. 注冊代碼:/ 注冊“提交”按鈕的監(jiān)聽器btSubmit.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) String us
17、erName = inputId.getText();String userPassword = new String(inputPassword.getPassword();String userPasswordConfirm = new String(inputPasswordConfirm.getPassword();System.out.println(您點(diǎn)擊了提交按鈕);if (userName.equals() JOptionPane.showMessageDialog(null, 用戶名不能為空); else if (.equals(userPassword)| .equals(
18、userPasswordConfirm) JOptionPane.showMessageDialog(null, 密碼和密碼重復(fù)都不能為空); else if (!userPassword.equals(userPasswordConfirm) JOptionPane.showMessageDialog(null, 密碼和密碼重復(fù)不一致); else UserInformation user = new UserInformation();if (user.isExist(userName) JOptionPane.showMessageDialog(null, 此用戶名已存在); else
19、JOptionPane.showMessageDialog(null, 注冊成功);user.insert(userName, userPassword);/UserInformation類Regist.this.dispose(););/ 注冊“取消”按鈕的監(jiān)聽器btCancel.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) System.out.println(您點(diǎn)擊了取消按鈕);Regist.this.dispose(););界面:4. 登錄和注冊判定代碼:/注冊一個(gè)用戶
20、public void insert(String userName, String userPassword) try userInfomation = new Properties();InputStream is;OutputStream os;is = new FileInputStream(c:/userIperties);os = new FileOutputStream(c:/userIperties, true);userInfomation.load(is);/ 將用戶名和密碼存儲(chǔ)到內(nèi)存中userInfomation.setProperty(use
21、rName, userPassword);/ 將用戶名和密碼保存到文件中userInfomation.store(os, null); catch (FileNotFoundException e1) System.out.println(文件userIperties沒有找到 ); catch (IOException e) System.out.println(寫 userIperties 出錯(cuò));/判斷此用戶名是否存在public boolean isExist(String userName) try userInfomation = new Properti
22、es();InputStream is;is = new FileInputStream(c:/userIperties);userInfomation.load(is);if (userInfomation.containsKey(userName) return true; catch (FileNotFoundException e1) System.out.println(文件userIperties沒有找到 ); catch (IOException e) System.out.println(寫 userIperties 出錯(cuò));retur
23、n false;5. 進(jìn)入聊天界面代碼:class showOldMessageThread implements Runnable public void run() boolean flag = true;while (flag) try / 接收群聊服務(wù)器端回發(fā)過來的消息String serverOutput = client.br.readLine() + rn;if (!serverOutput.startsWith(私聊)& !serverOutput.startsWith(*)& !(serverOutput.substring(serverOutput.indexOf(:) +
24、 1).equals(rn) String s1 = serverOutput.replace(說, );String s = s1.replaceAll(, rn );oldMessageTextArea.append(s);/ 添加客戶端的用戶在線列表if (!serverOutput.startsWith(*)& !serverOutput.startsWith(私聊)& (serverOutput.indexOf(說) != -1) String listName = serverOutput.substring(0,serverOutput.indexOf(說);/ 如果JList中
25、有相同名字的用戶,則不添加,否則添加if (!users.contains(listName) System.out.println(用戶 + listName + 上線了);users.add(listName);userList.setListData(users);/ 判斷服務(wù)器回發(fā)過來的消息是不是以私聊開頭的,是的話就提取出這兩個(gè)用戶名if (serverOutput.startsWith(私聊) String siliaoName1 = serverOutput.substring(serverOutput.indexOf(*) + 1, serverOutput.indexOf(和
26、);String siliaoName2 = serverOutput.substring(serverOutput.indexOf(和) + 1, serverOutput.indexOf(r);String siliaoBenshen = ;String siliaoDuixiangName = ;if (siliaoName1.equals(clientName) siliaoBenshen = siliaoName1;siliaoDuixiangName = siliaoName2; else siliaoBenshen = siliaoName2;siliaoDuixiangName
27、 = siliaoName1;/ 判斷這兩個(gè)名字中是否有與自己同名的,有的話就彈出個(gè)私聊窗口if (siliaoName1.equals(clientName)| siliaoName2.equals(clientName) new PointToPointTalkFrame(siliaoBenshen + 和+ siliaoDuixiangName).setVisible(true); catch (IOException e1) System.out.println(讀取服務(wù)器端消息出錯(cuò));/ 注冊JList的點(diǎn)擊事件,進(jìn)入私聊界面userList.addMouseListener(new
28、 MouseAdapter() public void mouseClicked(MouseEvent e) if (e.getClickCount() = 2) if (AllTalkFrame.this.userList.getSelectedValue().toString().equals(clientName) JOptionPane.showMessageDialog(null, 不能和自己聊天); else String PToPMemberName = 私聊+ *+ clientName+ 和+ AllTalkFrame.this.userList.getSelectedValue().toString();client.ps.println(PToPMemberName););界面:6. 私聊頁面代碼:/ 線程:只要服務(wù)器端有消息,就將消息顯示到oldMessageTextAreaclass showOldMessageThread implements Runnable public void run()
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 《幾何基礎(chǔ):空間幾何習(xí)題講解與練習(xí)》
- 承包個(gè)人餐廳協(xié)議
- 工程例會(huì)制度完整版
- 2025年宿州蕭縣交通投資有限責(zé)任公司招聘6人筆試參考題庫附帶答案詳解
- 2024-2025學(xué)年第二學(xué)期天域全國名校協(xié)作體高三3月聯(lián)考 生物試卷(含答案)
- 2025年上半年安徽蕪湖鳩江區(qū)所屬事業(yè)單位招聘擬聘用人員易考易錯(cuò)模擬試題(共500題)試卷后附參考答案
- 2025年上半年安徽省馬鞍山市博望區(qū)政府部門招聘派遣制人員14人易考易錯(cuò)模擬試題(共500題)試卷后附參考答案
- 2024年火車制品項(xiàng)目資金申請報(bào)告代可行性研究報(bào)告
- 2025年上半年安徽皖維集團(tuán)限責(zé)任公司招聘1名易考易錯(cuò)模擬試題(共500題)試卷后附參考答案
- 2024年溶栓藥項(xiàng)目資金需求報(bào)告代可行性研究報(bào)告
- 2024年人力資源管理師三級考試真題及答案
- 2024年中國遠(yuǎn)洋海運(yùn)集團(tuán)有限公司招聘筆試沖刺題(帶答案解析)
- 商品房施工組織設(shè)計(jì)
- 科目一知識大全課件
- 2016-2023年大慶醫(yī)學(xué)高等??茖W(xué)校高職單招(英語/數(shù)學(xué)/語文)筆試歷年參考題庫含答案解析
- 泛微協(xié)同OA與SAP集成應(yīng)用解決方案V講訴
- 探討電磁感應(yīng)現(xiàn)象對電能轉(zhuǎn)化效率的影響
- EHS法律法規(guī)清單及合規(guī)性評估
- 橋梁定期檢查-主要部件檢查要點(diǎn)與評定標(biāo)準(zhǔn)
- 長途汽車客運(yùn)站調(diào)研報(bào)告
- 陜西各市(精確到縣區(qū))地圖PPT課件(可編輯版)
評論
0/150
提交評論