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

上传人:b****8 文档编号:10687816 上传时间:2023-02-22 格式:DOCX 页数:21 大小:319.67KB
下载 相关 举报
网络程序设计考试大作业讲解学习.docx_第1页
第1页 / 共21页
网络程序设计考试大作业讲解学习.docx_第2页
第2页 / 共21页
网络程序设计考试大作业讲解学习.docx_第3页
第3页 / 共21页
网络程序设计考试大作业讲解学习.docx_第4页
第4页 / 共21页
网络程序设计考试大作业讲解学习.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

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

《网络程序设计考试大作业讲解学习.docx》由会员分享,可在线阅读,更多相关《网络程序设计考试大作业讲解学习.docx(21页珍藏版)》请在冰豆网上搜索。

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

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

网络程序设计考试大作业

 

题目:

聊天室程序

 

班级:

学号:

姓名:

成绩:

 

一.所使用的背景知识、主要函数的描述

背景:

根据现在最流行的聊天工具QQ,模仿一部分主要功能来完成。

主要函数:

publicclassServer;服务器的创建。

publicclassClient;客户端的创建。

publicclassMainextendsJFrame;登录界面的显示。

publicclassRegistextendsJDialog;注册界面的显示。

publicclassUserInformation;用户信息的保存和验证。

publicclassAllTalkFrameextendsJFrame;登录后进入群聊界面。

publicclassPointToPointTalkFrameextendsJFrame;私聊界面。

二.程序设计思想及程序设计流程框图

设计思想:

利用socket与serversocket在客户端与客户端之间的通信,InputStreamInputStreamReader输入输出流进行信息的发送与接收。

程序设计流程:

主页面:

输入账号与密码,点击登录或者注册进入下一页面。

登录:

判定是否正确,正确则进去聊天界面。

注册:

进去注册界面,成功则返回主页面。

进入聊天室:

能发送信息让在线的所有人看到。

私聊界面:

能与一个人单独聊天,信息只能被双方看到。

主页面

登录

三.主要代码及代码运行结果

1.启动服务器

代码:

publicclassServer{

ServerSocketserver;

staticintclientNum=0;

//存放与服务器连接上的对应的Socket,作用是保存服务器与客户端之间的流,便于服务器给每个客户端进行回发消息

ListclientConnection=newArrayList();

publicServer(){

try{

server=newServerSocket(9999);

System.out.println("服务器已经启动");

}catch(IOExceptione){

e.printStackTrace();

System.out.println("服务器启动失败");

}

}

//内部类,监听客户端是否有连接到服务器,并将此客户端的Socket传递给HandleSocket进行处理,同时将client存放到List中,即clientConnection中

classSocketListenerimplementsRunnable{

publicvoidrun(){

Socketclient;

try{

while(true){

client=server.accept();

//连接上一个就压入List中,即clientConnection中

clientConnection.add(client);

HandleSockeths=newHandleSocket(client);

//连接上就让HandleSocket去处理

newThread(hs).start();

}

}catch(IOExceptione){

System.out.println("客户连接服务器失败");

}

}

}

//内部类处理一个Socket,接收一个Client发送过来的消息,并且服务器原封不动的返回给所有客户端,客户端对消息进行过滤

classHandleSocketimplementsRunnable{

Socketclient;

HandleSocket(Socketclient){

this.client=client;

}

publicvoidrun(){

try{

clientNum++;

//启用输入流

InputStreamis=client.getInputStream();

InputStreamReaderisr=newInputStreamReader(is);

BufferedReaderbr=newBufferedReader(isr);

System.out.println("第"+clientNum+"个客户端连接进入服务器");

booleanflag=true;

Strings;

do{

//对用户发来的消息进行群发给客户端

s=br.readLine();

System.out.println("接受到一个客户端消息:

"+s);

for(inti=0;i

Socketclient=clientConnection.get(i);

OutputStreamos=client.getOutputStream();

PrintStreamps=newPrintStream(os);

ps.println(s);

}

}while(flag);

client.close();

}catch(IOExceptione){

System.out.println("有一个客户断开与服务器的连接");

}

}

}

界面:

2.登录

代码:

packagecom.qq.main;

importjava.awt.Color;

importjava.awt.Dimension;

importjava.awt.Toolkit;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjavax.swing.JButton;

importjavax.swing.JFrame;

importjavax.swing.JLabel;

importjavax.swing.JOptionPane;

importjavax.swing.JPasswordField;

importjavax.swing.JTextField;

importcom.qq.regist.Regist;

importcom.qq.regist.UserInformation;

/**

*主界面

*/

publicclassMainextendsJFrame{

//组件的内容

privateJLabeluserId;

privateJLabeluserPassword;

privateJTextFieldinputId;

privateJPasswordFieldinputPassword;

privateJButtonbtLogin;

privateJButtonbtRegist;

Main(){

userId=newJLabel("帐号");

userPassword=newJLabel("密码");

inputId=newJTextField(6);

inputPassword=newJPasswordField();

btLogin=newJButton("登陆");

btRegist=newJButton("注册");

//设置窗体属性

Toolkittk=Toolkit.getDefaultToolkit();

DimensionscreenSize=tk.getScreenSize();//得到当前屏幕的长和宽

intx=(int)screenSize.getWidth();

inty=(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(newActionListener(){

publicvoidactionPerformed(ActionEvente){

UserInformationuser=newUserInformation();

StringuserName=inputId.getText();

StringuserPassword=newString(inputPassword.getPassword());

if(userName.equals("")){

JOptionPane.showMessageDialog(null,"用户名不能为空");

}elseif("".equals(userPassword)){

JOptionPane.showMessageDialog(null,"密码不能为空");

}elseif(user.isExist(userName)

&&user.userInfomation.getProperty(userName).equals(

userPassword)){

newAllTalkFrame(userName).setVisible(true);//判断成功后new一个群聊窗口

Main.this.dispose();

}else{

JOptionPane.showMessageDialog(null,"此用户名不存在或者密码不正确");

}

}

});

//注册“注册”按钮监听器

btRegist.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

newRegist();//注册页面

}

});

this.add(userId);

this.add(userPassword);

this.add(inputId);

this.add(inputPassword);

this.add(btLogin);

this.add(btRegist);

this.setVisible(true);

}

publicstaticvoidmain(String[]args){

newMain();

}

}

界面:

3.注册

代码:

//注册“提交”按钮的监听器

btSubmit.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

StringuserName=inputId.getText();

StringuserPassword=newString(inputPassword.getPassword());

StringuserPasswordConfirm=newString(inputPasswordConfirm

.getPassword());

System.out.println("您点击了提交按钮");

if(userName.equals("")){

JOptionPane.showMessageDialog(null,"用户名不能为空");

}elseif("".equals(userPassword)

||"".equals(userPasswordConfirm)){

JOptionPane.showMessageDialog(null,"密码和密码重复都不能为空");

}elseif(!

userPassword.equals(userPasswordConfirm)){

JOptionPane.showMessageDialog(null,"密码和密码重复不一致");

}else{

UserInformationuser=newUserInformation();

if(user.isExist(userName)){

JOptionPane.showMessageDialog(null,"此用户名已存在");

}else{

JOptionPane.showMessageDialog(null,"注册成功");

user.insert(userName,userPassword);//UserInformation类

Regist.this.dispose();

}

}

}

});

//注册“取消”按钮的监听器

btCancel.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

System.out.println("您点击了取消按钮");

Regist.this.dispose();

}

});

界面:

4.登录和注册判定

代码:

//注册一个用户

publicvoidinsert(StringuserName,StringuserPassword){

try{

userInfomation=newProperties();

InputStreamis;

OutputStreamos;

is=newFileInputStream("c:

/userInfo.properties");

os=newFileOutputStream("c:

/userInfo.properties",true);

userInfomation.load(is);

//将用户名和密码存储到内存中

userInfomation.setProperty(userName,userPassword);

//将用户名和密码保存到文件中

userInfomation.store(os,null);

}catch(FileNotFoundExceptione1){

System.out.println("文件userInfo.properties没有找到");

}catch(IOExceptione){

System.out.println("写userInfo.properties出错");

}

}

//判断此用户名是否存在

publicbooleanisExist(StringuserName){

try{

userInfomation=newProperties();

InputStreamis;

is=newFileInputStream("c:

/userInfo.properties");

userInfomation.load(is);

if(userInfomation.containsKey(userName)){

returntrue;

}

}catch(FileNotFoundExceptione1){

System.out.println("文件userInfo.properties没有找到");

}catch(IOExceptione){

System.out.println("写userInfo.properties出错");

}

returnfalse;

}

5.进入聊天界面

代码:

classshowOldMessageThreadimplementsRunnable{

publicvoidrun(){

booleanflag=true;

while(flag){

try{

//接收群聊服务器端回发过来的消息

StringserverOutput=client.br.readLine()+"\r\n";

if(!

serverOutput.startsWith("私聊")

&&!

serverOutput.startsWith("*")

&&!

(serverOutput.substring(serverOutput

.indexOf(":

")+1).equals("\r\n"))){

Strings1=serverOutput.replace('说','');

Strings=s1.replaceAll("�","\r\n");

oldMessageTextArea.append(s);

}

//添加客户端的用户在线列表

if(!

serverOutput.startsWith("*")

&&!

serverOutput.startsWith("私聊")

&&(serverOutput.indexOf("说")!

=-1)){

StringlistName=serverOutput.substring(0,

serverOutput.indexOf('说'));

//如果JList中有相同名字的用户,则不添加,否则添加

if(!

users.contains(listName)){

System.out.println("用户"+listName+"上线了");

users.add(listName);

userList.setListData(users);

}

}

//判断服务器回发过来的消息是不是以"私聊"开头的,是的话就提取出这两个用户名

if(serverOutput.startsWith("私聊")){

StringsiliaoName1=serverOutput.substring(

serverOutput.indexOf("*")+1,serverOutput

.indexOf("和"));

StringsiliaoName2=serverOutput.substring(

serverOutput.indexOf("和")+1,serverOutput

.indexOf("\r"));

StringsiliaoBenshen="";

StringsiliaoDuixiangName="";

if(siliaoName1.equals(clientName)){

siliaoBenshen=siliaoName1;

siliaoDuixiangName=siliaoName2;

}else{

siliaoBenshen=siliaoName2;

siliaoDuixiangName=siliaoName1;

}

//判断这两个名字中是否有与自己同名的,有的话就弹出个私聊窗口

if(siliaoName1.equals(clientName)

||siliaoName2.equals(clientName)){

newPointToPointTalkFrame(siliaoBenshen+"和"

+siliaoDuixiangName).setVisible(true);

}

}

}catch(IOExceptione1){

System.out.println("读取服务器端消息出错");

}

}

}

}

//注册JList的点击事件,进入私聊界面

userList.addMouseListener(newMouseAdapter(){

publicvoidmouseClicked(MouseEve

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 解决方案 > 学习计划

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

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