网络聊天室课程设计报告内附java代码.docx
- 文档编号:29631649
- 上传时间:2023-07-25
- 格式:DOCX
- 页数:38
- 大小:154.25KB
网络聊天室课程设计报告内附java代码.docx
《网络聊天室课程设计报告内附java代码.docx》由会员分享,可在线阅读,更多相关《网络聊天室课程设计报告内附java代码.docx(38页珍藏版)》请在冰豆网上搜索。
网络聊天室课程设计报告内附java代码
课程设计
网络聊天室
院(系)别信电学院
班级计算102
学号
姓名
指导教师
时间2013-4-18—2013-5-10
课程设计任务书
题目网络聊天室
课程设计
系(部)信电学院
专业计算机科学与技术
班级计算102
学生姓名
学号
4月18日至5月10日共4周
指导教师(签字)
系主任(签字)
年月日
一、设计内容及要求
二、设计原始资料
1)电子工业出版社《JSP实用教程》,郑阿奇编著
2)电子工业出版社《JAVA程序设计实用教程》,叶核亚编著
三、设计完成后提交的文件与图表
1.计算说明书部分
JSP界面设计
2.图纸部分:
程序流程图
四、进程安排
五、主要参考资料
1)电子工业出版社《JSP实用教程》,郑阿奇编著
2)电子工业出版社《JAVA程序设计实用教程》,叶核亚编著考资料
成绩评定表
作品成绩
报告成绩
口试(答辩)成绩
总评成绩
课程设计概述
1、1问题描述
`随着计算机网络日新月异的发展,人们的交流方式越来越多,传统的交流方式,如:
信件、电报、电话等已经难以满足人们的交流要求,在互联网上即时的与好友取得联系,已经成为当今社会人们主流的联系方式。
因此,两台计算机之间进行即时通讯、发送文件等交流方式已经成为必然潮流。
因此出现了QQ等聊天工具,然而QQ等聊天工具虽然方便、实用,但就是,娱乐功能太多,有很多吸引人的娱乐功能,从一定程度上来说,就是一种娱乐工具,不能作为用于即时通讯、文件共享的专用工具。
目前,用于实现单一的即时通讯、文件共享的软件实在太少,而且,它们中大部分都就是绝对的C/S模式,对远程服务器有非常大的依赖性,没有彻底实现点对点的交流。
为此,我决定开发一个专用于实现两台计算机之间即时通讯、文件共享的软件,以方便两台计算机之间信息的交流。
避免了服务器忙或与服务器无法连接时,浪费过多时间用于与服务器建立连接。
因此,这个软件就是极具适应性与实用性的即时通讯软件。
1、2功能需求分析
实现网络聊天的功能,采用Windows Socket(或Java Socket)编程,服务器与客户端采用了TCP/IP连接方式,在设计聊天方案时,可将所有信息发往服务器端,再由服务器进行处理,服务器端就是所有信息的中心。
服务器端要保存用户信息,可利用数据库来实现这一功能,需要建立用户信息数据库。
在客户端可采用文件系统保存聊天纪录与用户号码。
可通过闪动托盘图标与播放不同的音乐提示信息到来及好友上线等信息。
可通过建立消息链表来保存用户接收的各种消息。
1、3环境需求分析
运行环境:
Windows9x、2000、XP、Windows7
必要环境:
JDK1、6以上、eclipse开发环境
硬件环境:
CPU400MHz以上,内存512MB以上
学生信息管理系统软件总体设计
2、1整体思路
该C/S模式的聊天系统,就就是服务器来提供服务端连接响应,客户端主动的发起连接请求。
当多个用户同时连接到服务器时,经由服务器的接收与转发便可以实现客户之间的通信。
对于整体系统的流程,也就是分为两部分,即客户端的发起请求,服务器响应请求。
首先就是客户端的登陆,客户端在登陆时,必须设置所想要连接的服务器,然后客户端就会根据设置的服务器IP地址,向服务器发起建立连接请求。
而服务器端就是始终在监听网络状态的,主要检测到有一个连接请求,那么服务器就会给当前的发起端分配一个服务线程,用于处理它的各种请求。
对于客户端的各种请求,实际上都就是通过在客户发往服务器的各种字符流区分的,具体的方法就就是在消息的头部以及尾部添加特殊字符串,从而实现服务器对消息请求的识别。
比如对于登陆信息,消息的头部就就是“PEOPLO”,而对于私聊中的消息,头部就就是“SEC”,其她的同理都添加了头部。
当然,对于客户端来说,这些都就是透明的,用户的操作并没有受到任何影响。
在服务器端,消息被检测分析后,变回根据具体的目的进行处理,比如就是私聊消息,服务器便会根据其尾部的目的信息,向目标端转发该条消息。
这里额外要说的就是,该系统特别的对用户发来的消息进行了加密处理,即用户的各种具体信息,不仅能够防止向第三方的泄漏,而且即使服务器端出现异常,信息也不会泄漏。
对于私聊的方式,该系统所采用的方法类似于QQ的通信方式,由发起方主动连接,当目的方接收到连接请求后,会主动建立一个私聊窗口,从而实现私聊。
2、2总体设计流程图
学生信息管理软件详细设计
3、1、界面设计
由于这里界面设计源代码比较简单,这里不再列出。
3、1、1服务器界面设计
服务器界面
3、1、2客户端界面设计
登陆界面
注册界面
聊天室界面
3、2通信功能实现方法
列举部分实现通信功能的主要代码
AppServer、java文件
publicclassAppServerextendsThread{
privateServerSocketserverSocket;
privateServerFramesFrame;
privatestaticVectoruserOnline=newVector(1,1);
privatestaticVectorv=newVector(1,1);
/**
*创建服务器启动服务监听1001端口
*
*/
publicAppServer(){
sFrame=newServerFrame();
try{
serverSocket=newServerSocket(1001);
//获取服务器的主机名与IP地址
InetAddressaddress=InetAddress、getLocalHost();
sFrame、txtServerName、setText(address、getHostName());
sFrame、txtIP、setText(address、getHostAddress());
sFrame、txtPort、setText("1001");
}catch(IOExceptione){
fail(e,"不能启动服务!
");
}
sFrame、txtStatus、setText("已启动、、、");
this、start();//启动线程
}
/**
*退出服务器
*
*@parame
*异常
*@paramstr
*退出信息
*/
publicstaticvoidfail(Exceptione,Stringstr){
System、out、println(str+"。
"+e);
}
/**
*监听客户的请求,当有用户请求时创建Connection线程
*/
publicvoidrun(){
try{
while(true){
//监听并接受客户的请求
Socketclient=serverSocket、accept();
newConnection(sFrame,client,userOnline,v);//支持多线程
//System、out、println("userLength:
"+userLength);
}
}catch(IOExceptione){
System、out、println("不能监听");
}
}
/**
*启动服务器
*/
publicstaticvoidmain(Stringargs[]){
newAppServer();
}
}
ServerFrame、java文件
publicclassServerFrameextendsJFrameimplementsActionListener{
publicJListlist;
/**
*
*/
privatestaticfinallongserialVersionUID=-8936397327038098620L;
//服务器信息面板
JPanelpnlServer,pnlServerInfo;
JLabellblStatus,lblNumber,lblMax,lblServerName,lblProtocol,lblIP,
lblPort,lblLog;
publicJTextFieldtxtStatus,txtNumber,txtMax,txtServerName,txtProtocol,txtIP,
txtPort;
JButtonbtnStop,btnSaveLog;
publicTextAreataLog;
JTabbedPanetpServer;
publicTextAreataMessage;
//用户信息面板
JPanelpnlUser;
publicJLabellblMessage,lblUser,lblNotice,lblUserCount;
JListlstUser;
JScrollPanespUser;
JTextFieldtxtNotice;
JButtonbtnSend,btnKick;
publicStringti="";
publicStringserverMessage="";
publicServerFrame(){
//服务器窗口
super("[HappyChat]聊天服务器");
setSize(550,500);
setDefaultCloseOperation(JFrame、EXIT_ON_CLOSE);
setResizable(false);
Dimensionscr=Toolkit、getDefaultToolkit()、getScreenSize();//在屏幕居中显示
Dimensionfra=this、getSize();
if(fra、width>scr、width){
fra、width=scr、width;
}
if(fra、height>scr、height){
fra、height=scr、height;
}
this、setLocation((scr、width-fra、width)/2,
(scr、height-fra、height)/2);
//==========服务器信息面板=========================
pnlServer=newJPanel();
pnlServer、setLayout(null);
pnlServerInfo=newJPanel(newGridLayout(14,1));
pnlServerInfo、setBorder(BorderFactory、createCompoundBorder(
BorderFactory、createTitledBorder(""),BorderFactory
、createEmptyBorder(1,1,1,1)));
lblStatus=newJLabel("当前状态:
");
txtStatus=newJTextField(10);
txtStatus、setEditable(false);
lblNumber=newJLabel("当前在线人数:
");
txtNumber=newJTextField("0人",10);
txtNumber、setEditable(false);
lblMax=newJLabel("最多在线人数:
");
txtMax=newJTextField("50人",10);
txtMax、setEditable(false);
lblServerName=newJLabel("服务器名称:
");
txtServerName=newJTextField(10);
txtServerName、setEditable(false);
lblProtocol=newJLabel("访问协议:
");
txtProtocol=newJTextField("HTTP",10);
txtProtocol、setEditable(false);
lblIP=newJLabel("服务器IP:
");
txtIP=newJTextField(10);
txtIP、setEditable(false);
lblPort=newJLabel("服务器端口:
");
txtPort=newJTextField("8000",10);
txtPort、setEditable(false);
btnStop=newJButton("关闭服务器(C)");
btnStop、addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventarg0){
closeServer();
}
});
lblLog=newJLabel("[服务器日志]");
taLog=newTextArea(20,50);
btnSaveLog=newJButton("保存日志(S)");
btnSaveLog、addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventarg0){
saveLog();
}
});
pnlServerInfo、add(lblStatus);
pnlServerInfo、add(txtStatus);
pnlServerInfo、add(lblNumber);
pnlServerInfo、add(txtNumber);
pnlServerInfo、add(lblMax);
pnlServerInfo、add(txtMax);
pnlServerInfo、add(lblServerName);
pnlServerInfo、add(txtServerName);
pnlServerInfo、add(lblProtocol);
pnlServerInfo、add(txtProtocol);
pnlServerInfo、add(lblIP);
pnlServerInfo、add(txtIP);
pnlServerInfo、add(lblPort);
pnlServerInfo、add(txtPort);
pnlServerInfo、setBounds(5,5,100,400);
lblLog、setBounds(110,5,100,30);
taLog、setBounds(110,35,400,370);
btnStop、setBounds(200,410,120,30);
btnSaveLog、setBounds(320,410,120,30);
pnlServer、add(pnlServerInfo);
pnlServer、add(lblLog);
pnlServer、add(taLog);
pnlServer、add(btnStop);
pnlServer、add(btnSaveLog);
//===========在线用户面板====================
pnlUser=newJPanel();
pnlUser、setLayout(null);
lblMessage=newJLabel("[用户消息]");
taMessage=newTextArea(20,20);
lblNotice=newJLabel("通知:
");
txtNotice=newJTextField(20);
btnSend=newJButton("发送(S)");
btnSend、setEnabled(true);
btnSend、addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventarg0){
serverMessage();
}
});
lblUserCount=newJLabel("在线总人数0人");
btnKick=newJButton("踢人(K)");
btnKick、addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventarg0){
tiRen();
}
});
lblUser=newJLabel("[在线用户列表]");
lstUser=newJList();
lstUser、setVisibleRowCount(17);
lstUser、setFixedCellWidth(180);//指定的固定单元格宽度值,而不就是从列表元素计算而来的宽度值
lstUser、setFixedCellHeight(18);
spUser=newJScrollPane();
spUser、setVerticalScrollBarPolicy(JScrollPane、VERTICAL_SCROLLBAR_ALWAYS);
spUser、getViewport()、setView(lstUser);
lblMessage、setBounds(5,5,100,25);
taMessage、setBounds(5,35,300,360);
lblUser、setBounds(310,5,100,25);
spUser、setBounds(310,35,220,360);
lblNotice、setBounds(5,410,40,25);
txtNotice、setBounds(50,410,160,25);
btnSend、setBounds(210,410,80,25);
lblUserCount、setBounds(320,410,100,25);
btnKick、setBounds(440,410,80,25);
pnlUser、add(lblMessage);
pnlUser、add(taMessage);
pnlUser、add(lblUser);
pnlUser、add(spUser);
list=newJList();
list、setListData(newString[]{""});
spUser、setViewportView(list);
pnlUser、add(lblNotice);
pnlUser、add(txtNotice);
pnlUser、add(btnSend);
pnlUser、add(lblUserCount);
pnlUser、add(btnKick);
//============主标签面板========================
tpServer=newJTabbedPane(JTabbedPane、TOP);
tpServer、add("服务器管理",pnlServer);
tpServer、add("用户信息管理",pnlUser);
this、getContentPane()、add(tpServer);
setVisible(true);
}
protectedvoidserverMessage(){
//TODO自动生成方法存根
this、serverMessage=txtNotice、getText();
txtNotice、setText("");
}
protectedvoidcloseServer(){
//TODO自动生成方法存根
this、dispose();
//this、res
}
protectedvoidsaveLog(){
//TODO自动生成方法存根
try{
=new("log、txt",
true);
Stringtemp=taMessage、getText();
(temp、getBytes());
();
JOptionPane、showMessageDialog(null,"记录保存在log、txt");
}catch(Exceptione){
System、out、println(e);
}
}
protectedvoidtiRen(){
//TODO自动生成方法存根
ti=list、getSelectedValue()、toString();
log("Ti:
"+ti);
}
privatevoidlog(Stringstring){
//TODO自动生成方法存根
Stringnewta=taMessage、getText();
newta+=("\n"+string);
taMessage、setText(newta);
}
publicvoidactionPerformed(ActionEventevt){
}
publicstaticvoidmain(Stringargs[]){
newServerFrame();
}
}
Connection、java文件
publicclassConnectionextendsThread{
/**
*与客户端通讯Socket
*/
privateSocketnetClient;
/
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 网络 聊天室 课程设计 报告 java 代码