java聊天小程序课程设计文档格式.docx
《java聊天小程序课程设计文档格式.docx》由会员分享,可在线阅读,更多相关《java聊天小程序课程设计文档格式.docx(22页珍藏版)》请在冰点文库上搜索。
3.用户可以配置连接后显示的用户名。
4.当服务器端开启的话,用户可以随时登录和注销。
5.用户可以向所有人或某一个人发送消息。
三、系统运行环境
本程序基于java和Eclipse开发环境,可以在一般的装有windows系统的主机上运行。
Eclipse是一个开放源代码的软件开发项目,专注于为高度集成的工具开发提供一个全功能的、具有商业品质的工业平台。
它主要由Eclipse项目、Eclipse工具项目和Eclipse技术项目三个项目组成,具体包括四个部分组成——EclipsePlatform、JDTCDT和PDEJDT支持Java幵发、CDT支持C幵发、PDE用来支持
发平台。
它提供建造块和构造并运行集成软件幵发工具的基础。
四、功能需求描述
本系统针对局域网进行联机聊天。
聊天室分为服务器端和客户端两部分,服务器端程序主要负责侦听客户端发来的信息,客户端需要登陆到服务器端才可以实现正常的聊天功能。
本系统主要实现如下功能:
其界面主要采用了包,包以及包等。
程序实现了聊天室的基本功能,其中有:
1、启动服务器。
2、进入客户端界面。
3、登录聊天室。
4、发送信息(实现按回车键发送消息)。
5、关闭服务器。
服务器:
在特定端口上进行侦听,等待客户端连接。
向已经连接到服务器端的用户发送系统消息。
当停止服务时,断幵所有的用户连接。
系统运行稳定、安全可靠。
一台主机只能启动一个服务器。
客户端:
连接到已经幵启聊天服务的服务器端。
当服务器端幵启后,用户可以发表聊天信息。
用户可以向所有人或某一个人发送消息。
系统运行稳定、安全可靠
五、总体设计
聊天系统的设计跟普通网站设计有着许多不同的地方,普通网站设计所考虑的因素,例如,普通网站需要对布局进入大量美化以及动画设计等等,而聊天室只要提供满足访客双方直接实时聊天即可。
因此,在设计聊天系统的过程中,必须要考虑好以下几个设计要点:
在Internet上的聊天程序一般都是以服务器提供服务端连接响应,使用者通
过客户端程序登录到服务器,就可以与登录在同一服务器上的用户交谈,这是一个面向连接的通信过程。
因此,程序要在TCP/IP环境下,实现服务器端和客户端
两部分程序。
1.首先通过继承JFrame类构造服务器界面。
2.设计客户端程序使其能够完成正常的通信功能
3.设计侦听程序,使其显示登录的ip地址端口号,以及用户昵称,通过侦
听按钮可以调用客户端程序,实现聊天功能。
六、
系统模块结构图
主要功能框架如下图所示:
代码:
客户端A
客户端C
丿'
・T、
上服务器发
客户端.
>
J
退
出
线纟
线送\
服
聊
1)packagechat;
import.*;
publicclassActionProcessimplementsRunnable{
privateChatClientclient;
Stringname="
"
;
Stringsendtoname="
StringsendtoID;
Stringfilename="
Stringfilepath="
publicActionProcess(ChatClientclient){
=client;
}
publicvoidaction(ActionEvente)throwsException{if()==
{
try
else
Stringaddr=("
请输入服务器IP:
);
InetAddressaddress=(addr);
InetSocketAddresssocketAddress=newInetSocketAddress(address,5555);
=newThread(this);
catch(IOExceptionee){}}
if()==
filesendServerfilesendserver=newfilesendServer();
();
catch(Exceptionee){();
}}
if()=={
try{
filesendClientfilesendclient=newfilesendClient();
}catch(Exceptionee){}
}catch(IOExceptione1){
(0);
if("
表情"
)){
}else{em+="
着"
}if()==
if(s!
=null)
catch(IOExceptione1){}
sendtoID=("
请输入对方ID:
sendtoname=("
请输入对方姓名:
if(s!
{}
}}publicvoidrun()
Strings=null;
while(true)
}catch(IOExceptione)
}catch(Exceptionep){}break;
(2)packagechat;
import.*;
publicclassClient{publicstaticvoidmain(Stringargs[])
newChatClient();
classChatClientextendsFrameimplementsActionListener,Runnable{publicButtonlisten,connection,sendtoall,sendtoone,exit,filesend;
publicJComboBoxemote;
publicTextFieldinputName,inputContent;
publicTextAreachatResult;
publicSocketsocket=null;
publicDataInputStreamin=null;
publicDataOutputStreamout=null;
publicThreadthread;
ChatClient()
socket=newSocket();
Boxbox1=();
listen=newButton("
作为服务器监听"
connection=newButton("
连接服务器"
filesend=newButton("
发送文件"
exit=newButton("
退出"
sendtoone=newButton("
私聊"
(false);
inputName=newTextField(6);
(newColor(162,231,250));
inputContent=newTextField(22);
chatResult=newTextArea("
17,20,;
JLabeljlname=newJLabel("
输入昵称"
(jlname);
(inputName);
(listen);
(connection);
(filesend);
(exit);
Boxbox2=();
emote=newJComboBox();
(newDefaultComboBoxModel(newString[]{"
表情"
"
微笑"
甜笑"
惊喜"
嘻嘻"
扮酷"
嘿嘿"
傻笑"
好奇"
媚眼"
鬼脸"
陶醉"
害
羞"
生气"
嚷嚷"
发怒"
伤心"
高明"
菜鸟"
问号"
狂笑大哭"
示爱"
呻吟"
想想"
}));
JLabeljlintput=newJLabel("
输入聊天内容"
(jlintput);
(inputContent);
(emote);
(sendtoall);
(sendtoone);
(this);
Boxbox3=();
(chatResult);
thread=newThread(this);
Boxbox0=();
(10));
(box1);
(box3);
(box2);
add(box0);
setBounds(10,30,500,400);
setBackground(newColor(80,212,248));
setVisible(true);
validate();
addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){(0);
});
publicvoidactionPerformed(ActionEvente){
ActionProcessactionProcess=newActionProcess(this);
(e);
}catch(Exceptione1){
publicvoidrun()
while(true){
s=();
("
\n"
+s);
catch(IOExceptione)
与服务器断开连接"
}catch(Exceptionep){}
break;
(3)
packagechat;
publicclassChatServer
publicstaticvoidmain(String[]args)throwsException{ServerSocketsvSocket=null;
Vectorthreads=newVector();
svSocket=newServerSocket(5555);
}catch(Exceptionex){
return;
try{
intnid=0;
while(true){
Socketsocket=();
ServerThreadst=newServerThread(socket,threads);
(nid++);
(st);
newThread(st).start();
for(inti=0;
i<
();
i++){
ServerThreadtemp=(ServerThread)(i);
}catch(Exceptionex){
classServerThreadimplementsRunnable{
privateVectorthreads;
privateSocketsocket=null;
privateDataInputStreamin=null;
privateDataOutputStreamout=null;
privateintnid;
publicServerThread(Socketsocket,Vectorthreads){=socket;
=threads;
in=newDataInputStream());
out=newDataOutputStream());
catch(Exceptionex){
publicvoidrun(){try{while(true){Stringreceive=();
if(receive==null)return;
if("
黯然下线了"
)){for(inti=0;
i++){ServerThreadst=(ServerThread)(i);
("
***"
+receive+"
}elseif("
上线了"
ServerThreadst=(ServerThread)(i);
<
+getID()+"
:
"
+receive);
elseif("
监听中"
)){
***"
说"
大家"
else{
inttemp=("
inttemp1=("
StringtempS=(temp+1,temp1);
inti=(tempS);
st=(ServerThread)(getID());
ServerThreadst=(ServerThread)(getID());
try{();
publicvoidwrite(Stringmsg){
synchronized(out){try{
(msg);
publicintgetID(){
return;
publicvoidsetID(intnid){
=nid;
(4)
publicclassfilesendClient{
publicvoidfilesend()throwsException{
StringsendtoIP=("
请输入对方IP:
Socketsocket=newSocket(sendtoIP,1234);
BufferedReaderbr=newBufferedReader(newInputStreamReader()));
PrintStreamps=newPrintStream());
Filefile=getFile();
());
Stringmsg=();
已存在"
.equals(msg)){
(null,"
文件已存在,请不要重复上传!
return;
longfinishLen=(msg);
FileInputStreamfis=newFileInputStream(file);
OutputStreamout=();
byte[]buffer=newbyte[1024];
intlen;
(finishLen);
while((len=(buffer))!
=-1)
(buffer,0,len);
publicFilegetFile()throwsException{Filefile=null;
booleanflag=false;
while(flag==false){
Stringfilepath=("
请输入要上传的路径:
file=newFile(filepath);
if(!
()){
您输入的路径不存在,请重新输入!
flag=false;
elseif()){
占不支持文件夹上传!
请输入一个文件路径!
elseflag=true;
returnfile;
(5)
publicclassfilesendServer{
publicvoidfilereceive()throwsException{
ServerSocketserverSocket=newServerSocket(1234);
服务已启动,绑定1234端口!
newfileServerThread(socket).start();
classfileServerThreadextendsThread{
Socketsocket;
publicfileServerThread(Socketsocket){
=socket;
publicvoidrun(){
FileOutputStreamfos=null;
BufferedReaderbr=newBufferedReader(newInputStreamReader()));
StringfileName=();
longfileLen=());
Filedir=newFile("
upload"
Filefile=newFile(dir,fileName);
if()&
&
()==fileLen){("
Stringtime=newSimpleDateFormat("
yyyy-MM-ddHH:
mm:
ss"
).format(newDate());
longstart=();
InputStreamin=();
fos=newFileOutputStream(file,true);
=-1){
if()==fileLen)
longend=();
time=newSimpleDateFormat("
上传成功"
}catch(IOExceptione){
if(fos!
=null)
运行结果:
将5个Java源文件:
、、、、。
分别编译这5个Java源文件,或运行“javac*.java命令,编译全部的源文件,然后运行连接服务器的主类,即。
服务器启动成功结果分析:
在进入聊天室之前必须先启动服务器,实现服务器端的连接。
然后运行
结果分析: