ImageVerifierCode 换一换
格式:DOCX , 页数:21 ,大小:319.67KB ,
资源ID:10687816      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/10687816.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(网络程序设计考试大作业讲解学习.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

网络程序设计考试大作业讲解学习.docx

1、网络程序设计考试大作业讲解学习网络程序设计考试大作业 题目:聊天室程序 班级: 学号: 姓名: 成绩:一所使用的背景知识、主要函数的描述背景:根据现在最流行的聊天工具QQ,模仿一部分主要功能来完成。主要函数:public class Server;服务器的创建。public class Client;客户端的创建。public class Main extends JFrame;登录界面的显示。public class Regist extends JDialog;注册界面的显示。public class UserInformation;用户信息的保存和验证。public class AllT

2、alkFrame extends JFrame;登录后进入群聊界面。public class PointToPointTalkFrame extends JFrame;私聊界面。二程序设计思想及程序设计流程框图设计思想:利用socket与server socket在客户端与客户端之间的通信,InputStream InputStreamReader输入输出流进行信息的发送与接收。程序设计流程:主页面:输入账号与密码,点击登录或者注册进入下一页面。登录:判定是否正确,正确则进去聊天界面。注册:进去注册界面,成功则返回主页面。进入聊天室:能发送信息让在线的所有人看到。私聊界面:能与一个人单独聊天,

3、信息只能被双方看到。主页面登录三主要代码及代码运行结果1.启动服务器代码:public class Server ServerSocket server; static int clientNum = 0; / 存放与服务器连接上的对应的Socket,作用是保存服务器与客户端之间的流,便于服务器给每个客户端进行回发消息 List clientConnection = new ArrayList(); public Server() try server = new ServerSocket(9999); System.out.println(服务器已经启动); catch (IOExcepti

4、on e) e.printStackTrace(); System.out.println(服务器启动失败); / 内部类,监听客户端是否有连接到服务器,并将此客户端的Socket传递给HandleSocket进行处理,同时将client存放到List中,即clientConnection中 class SocketListener implements Runnable public void run() Socket client; try while (true) client = server.accept(); / 连接上一个就压入List中,即clientConnection中 c

5、lientConnection.add(client); HandleSocket hs = new HandleSocket(client); / 连接上就让HandleSocket去处理 new Thread(hs).start(); catch (IOException e) System.out.println(客户连接服务器失败); / 内部类 处理一个Socket,接收一个Client发送过来的消息,并且服务器原封不动的返回给所有客户端,客户端对消息进行过滤 class HandleSocket implements Runnable Socket client; HandleSo

6、cket(Socket client) this.client = client; public void run() try clientNum+; / 启用输入流 InputStream is = client.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); System.out.println(第 + clientNum + 个客户端连接进入服务器); boolean flag = true; String s

7、; do / 对用户发来的消息进行群发给客户端 s = br.readLine(); System.out.println(接受到一个客户端消息: + s); for (int i = 0; i clientConnection.size(); i+) Socket client = clientConnection.get(i); OutputStream os = client.getOutputStream(); PrintStream ps = new PrintStream(os); ps.println(s); while (flag); client.close(); catch

8、 (IOException e) System.out.println(有一个客户断开与服务器的连接); 界面: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.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JL

9、abel;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 /组件的内容 private JLabel userId; private JLabel userPassword; private JTextField inputId; priv

10、ate JPasswordField inputPassword; private JButton btLogin; private JButton btRegist; Main() userId = new JLabel(帐号); userPassword = new JLabel(密码); inputId = new JTextField(6); inputPassword = new JPasswordField(); btLogin = new JButton(登陆); btRegist = new JButton(注册); / 设置窗体属性 Toolkit tk = Toolkit.

11、getDefaultToolkit(); Dimension screenSize = tk.getScreenSize();/得到当前屏幕的长和宽 int x = (int) screenSize.getWidth(); int y = (int) screenSize.getHeight(); this.setBounds(x - 240) / 2, (y - 600) / 2, 240, 600);/窗口显示的大小 ,位置 this.setResizable(false);/窗口大小不能改变 this.setLayout(null);/默认的格式 this.setBackground(C

12、olor.BLACK);/ 窗口的颜色 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/退出程序 / 设置JLabel属性 userId.setBounds(30, 160, 40, 20); userPassword.setBounds(30, 200, 40, 20); / 设置文本域属性 inputId.setBounds(90, 160, 100, 20); inputPassword.setBounds(90, 200, 100, 20); inputPassword.setEchoChar(*);/用*显示代替你输入的密码

13、/ 设置JButton属性 btLogin.setBounds(50, 240, 60, 20); btRegist.setBounds(120, 240, 60, 20); / 注册“登陆”按钮监听器 btLogin.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) UserInformation user = new UserInformation(); String userName = inputId.getText(); String userPassword = new

14、 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).equals( userPassword) new AllTalkFrame(user

15、Name).setVisible(true);/ 判断成功后new一个群聊窗口 Main.this.dispose(); else JOptionPane.showMessageDialog(null, 此用户名不存在或者密码不正确); ); / 注册“注册”按钮监听器 btRegist.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) new Regist();/注册页面 ); this.add(userId); this.add(userPassword); this.add(

16、inputId); this.add(inputPassword); this.add(btLogin); this.add(btRegist); this.setVisible(true); public static void main(String args) new Main(); 界面:3.注册 代码: / 注册“提交”按钮的监听器 btSubmit.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) String userName = inputId.getText();

17、 String userPassword = new String(inputPassword.getPassword(); String userPasswordConfirm = new String(inputPasswordConfirm .getPassword(); System.out.println(您点击了提交按钮); if (userName.equals() JOptionPane.showMessageDialog(null, 用户名不能为空); else if (.equals(userPassword) | .equals(userPasswordConfirm)

18、JOptionPane.showMessageDialog(null, 密码和密码重复都不能为空); else if (!userPassword.equals(userPasswordConfirm) JOptionPane.showMessageDialog(null, 密码和密码重复不一致); else UserInformation user = new UserInformation(); if (user.isExist(userName) JOptionPane.showMessageDialog(null, 此用户名已存在); else JOptionPane.showMess

19、ageDialog(null, 注册成功); user.insert(userName, userPassword);/UserInformation类 Regist.this.dispose(); ); / 注册“取消”按钮的监听器 btCancel.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) System.out.println(您点击了取消按钮); Regist.this.dispose(); );界面:4.登录和注册判定代码: /注册一个用户 public void

20、insert(String userName, String userPassword) try userInfomation = new Properties(); InputStream is; OutputStream os; is = new FileInputStream(c:/userInfo.properties); os = new FileOutputStream(c:/userInfo.properties, true); userInfomation.load(is); / 将用户名和密码存储到内存中 userInfomation.setProperty(userName

21、, userPassword); / 将用户名和密码保存到文件中 userInfomation.store(os, null); catch (FileNotFoundException e1) System.out.println(文件userInfo.properties没有找到 ); catch (IOException e) System.out.println(写 userInfo.properties 出错); /判断此用户名是否存在 public boolean isExist(String userName) try userInfomation = new Propertie

22、s(); InputStream is; is = new FileInputStream(c:/userInfo.properties); userInfomation.load(is); if (userInfomation.containsKey(userName) return true; catch (FileNotFoundException e1) System.out.println(文件userInfo.properties没有找到 ); catch (IOException e) System.out.println(写 userInfo.properties 出错); r

23、eturn false; 5.进入聊天界面代码:class showOldMessageThread implements Runnable public void run() boolean flag = true; while (flag) try / 接收群聊服务器端回发过来的消息 String serverOutput = client.br.readLine() + rn; if (!serverOutput.startsWith(私聊) & !serverOutput.startsWith(*) & !(serverOutput.substring(serverOutput .in

24、dexOf(:) + 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.inde

25、xOf(说); / 如果JList中有相同名字的用户,则不添加,否则添加 if (!users.contains(listName) System.out.println(用户 + listName + 上线了); users.add(listName); userList.setListData(users); / 判断服务器回发过来的消息是不是以私聊开头的,是的话就提取出这两个用户名 if (serverOutput.startsWith(私聊) String siliaoName1 = serverOutput.substring( serverOutput.indexOf(*) + 1

26、, serverOutput .indexOf(和); String siliaoName2 = serverOutput.substring( serverOutput.indexOf(和) + 1, serverOutput .indexOf(r); String siliaoBenshen = ; String siliaoDuixiangName = ; if (siliaoName1.equals(clientName) siliaoBenshen = siliaoName1; siliaoDuixiangName = siliaoName2; else siliaoBenshen

27、= siliaoName2; siliaoDuixiangName = siliaoName1; / 判断这两个名字中是否有与自己同名的,有的话就弹出个私聊窗口 if (siliaoName1.equals(clientName) | siliaoName2.equals(clientName) new PointToPointTalkFrame(siliaoBenshen + 和 + siliaoDuixiangName).setVisible(true); catch (IOException e1) System.out.println(读取服务器端消息出错); / 注册JList的点击事件,进入私聊界面 userList.addMouseListener(new MouseAdapter() public void mouseClicked(MouseEve

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1