网络程序设计考试大作业讲解学习
网络程序设计考试大作业题目:聊天室程序
班级:
学号:
姓名:
成绩:
网络程序设计考试大作业 (1)
一.所使用的背景知识、主要函数的描述 (3)
二.程序设计思想及程序设计流程框图 (3)
三.主要代码及代码运行结果 (4)
1.启动服务器 (4)
2. 登录 (6)
3. 注册 (10)
4. 登录和注册判定 (12)
5. 进入聊天界面 (13)
6. 私聊页面 (17)
一.所使用的背景知识、主要函数的描述
背景:根据现在最流行的聊天工具QQ,模仿一部分主要功能来完成。
主要函数:
public class Server;服务器的创建。
public class Client;客户端的创建。
public class UserInformation;用户信息的保存和验证。
二.程序设计思想及程序设计流程框图
设计思想:
利用socket与server socket在客户端与客户端之间的通信,InputStream InputStreamReader输入输出流进行信息的发送与接收。
程序设计流程:
主页面:输入账号与密码,点击登录或者注册进入下一页面。
登录:判定是否正确,正确则进去聊天界面。
注册:进去注册界面,成功则返回主页面。
进入聊天室:能发送信息让在线的所有人看到。
私聊界面:能与一个人单独聊天,信息只能被双方看到。
三.主要代码及代码运行结果
1.启动服务器
代码:
public class Server {
ServerSocket server;
static int clientNum = 0;
// 存放与服务器连接上的对应的Socket,作用是保存服务器与客户端之间的流,便于服务器给每个客户端进行回发消息
List
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 https://www.360docs.net/doc/d92121425.html,erInformation;
/**
* 主界面
*/
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)
&& https://www.360docs.net/doc/d92121425.html,erInfomation.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() {