网络程序设计考试大作业.doc_第1页
网络程序设计考试大作业.doc_第2页
网络程序设计考试大作业.doc_第3页
网络程序设计考试大作业.doc_第4页
网络程序设计考试大作业.doc_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

聊天室程序网络程序设计考试大作业 题目:聊天室程序 班级: 学号: 姓名: 成绩:网络程序设计考试大作业1一所使用的背景知识、主要函数的描述3二 程序设计思想及程序设计流程框图3三主要代码及代码运行结果41.启动服务器42. 登录63. 注册104. 登录和注册判定125. 进入聊天界面136. 私聊页面17一所使用的背景知识、主要函数的描述背景:根据现在最流行的聊天工具QQ,模仿一部分主要功能来完成。主要函数:public class Server;服务器的创建。public class Client;客户端的创建。public class Main extends JFrame;登录界面的显示。public class Regist extends JDialog;注册界面的显示。public class UserInformation;用户信息的保存和验证。public class AllTalkFrame extends JFrame;登录后进入群聊界面。public class PointToPointTalkFrame extends JFrame;私聊界面。二程序设计思想及程序设计流程框图设计思想:利用socket与server socket在客户端与客户端之间的通信,InputStream InputStreamReader输入输出流进行信息的发送与接收。程序设计流程:主页面:输入账号与密码,点击登录或者注册进入下一页面。登录:判定是否正确,正确则进去聊天界面。注册:进去注册界面,成功则返回主页面。进入聊天室:能发送信息让在线的所有人看到。私聊界面:能与一个人单独聊天,信息只能被双方看到。主页面注册登录进入聊天室点击名字进入私聊三主要代码及代码运行结果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 (IOException 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中clientConnection.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;HandleSocket(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;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 (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.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 /组件的内容private 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 JButton(登陆);btRegist = new JButton(注册);/ 设置窗体属性Toolkit tk = Toolkit.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(Color.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(*);/用*显示代替你输入的密码/ 设置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 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(userName).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(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();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) 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.showMessageDialog(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 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);/ 将用户名和密码存储到内存中userInfomation.setProperty(userName, userPassword);/ 将用户名和密码保存到文件中userInfomation.store(os, null); catch (FileNotFoundException e1) System.out.println(文件userIperties没有找到 ); catch (IOException e) System.out.println(写 userIperties 出错);/判断此用户名是否存在public boolean isExist(String userName) try userInfomation = new Properties();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 出错);return 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.indexOf(:) + 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中有相同名字的用户,则不添加,否则添加if (!users.contains(listName) System.out.println(用户 + listName + 上线了);users.add(listName);userList.setListData(users);/ 判断服务器回发过来的消息是不是以私聊开头的,是的话就提取出这两个用户名if (serverOutput.startsWith(私聊) String siliaoName1 = serverOutput.substring(serverOutput.indexOf(*) + 1, 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 = 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(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. 私聊页面代码:/ 线程:只要服务器端有消息,就将消息显示到oldMessageTextAreaclass showOldMessageThread implements Runnable public void run()

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论