java聊天小程序课程设计Word格式.docx
- 文档编号:22470227
- 上传时间:2023-02-04
- 格式:DOCX
- 页数:24
- 大小:45.01KB
java聊天小程序课程设计Word格式.docx
《java聊天小程序课程设计Word格式.docx》由会员分享,可在线阅读,更多相关《java聊天小程序课程设计Word格式.docx(24页珍藏版)》请在冰豆网上搜索。
3.用户可以配置连接后显示的用户名。
4.当服务器端开启的话,用户可以随时登录和注销。
5.用户可以向所有人或某一个人发送消息。
三、系统运行环境
本程序基于java和Eclipse开发环境,可以在一般的装有windows系统的主机上运行。
Eclipse是一个开放源代码的软件开发项目,专注于为高度集成的工具开发提供一个全功能的、具有商业品质的工业平台。
它主要由Eclipse项目、Eclipse工具项目和Eclipse技术项目三个项目组成,具体包括四个部分组成——EclipsePlatform、JDT、CDT和PDE。
JDT支持Java开发、CDT支持C开发、PDE用来支持插件开发,EclipsePlatform则是一个开放的可扩展IDE,提供了一个通用的开发平台。
它提供建造块和构造并运行集成软件开发工具的基础。
四、功能需求描述
本系统针对局域网进行联机聊天。
聊天室分为服务器端和客户端两部分,服务器端程序主要负责侦听客户端发来的信息,客户端需要登陆到服务器端才可以实现正常的聊天功能。
本系统主要实现如下功能:
其界面主要采用了包,包以及包等。
程序实现了聊天室的基本功能,其中有:
1、启动服务器。
2、进入客户端界面。
3、登录聊天室。
4、发送信息(实现按回车键发送消息)。
5、关闭服务器。
服务器:
在特定端口上进行侦听,等待客户端连接。
向已经连接到服务器端的用户发送系统消息。
当停止服务时,断开所有的用户连接。
系统运行稳定、安全可靠。
一台主机只能启动一个服务器。
客户端:
连接到已经开启聊天服务的服务器端。
当服务器端开启后,用户可以发表聊天信息。
用户可以向所有人或某一个人发送消息。
五、总体设计
聊天系统的设计跟普通网站设计有着许多不同的地方,普通网站设计所考虑的因素,例如,普通网站需要对布局进入大量美化以及动画设计等等,而聊天室只要提供满足访客双方直接实时聊天即可。
因此,在设计聊天系统的过程中,必须要考虑好以下几个设计要点:
在Internet上的聊天程序一般都是以服务器提供服务端连接响应,使用者通过客户端程序登录到服务器,就可以与登录在同一服务器上的用户交谈,这是一个面向连接的通信过程。
因此,程序要在TCP/IP环境下,实现服务器端和客户端两部分程序。
1.首先通过继承JFrame类构造服务器界面。
2.设计客户端程序使其能够完成正常的通信功能
3.设计侦听程序,使其显示登录的ip地址端口号,以及用户昵称,通过侦听按钮可以调用客户端程序,实现聊天功能。
六、系统模块结构图
主要功能框架如下图所示:
七、程序模块设计(程序代码设计与分析、运行结果)
代码:
(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("
表情"
)){
em="
}else{
em+="
着"
if(s!
=null)
catch(IOExceptione1){}
sendtoID=("
请输入对方ID:
sendtoname=("
请输入对方姓名:
publicvoidrun()
Strings=null;
while(true)
catch(IOExceptione)
catch(Exceptionep){}
break;
(2)
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("
退出"
sendtoall=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){
});
publicvoidactionPerformed(ActionEvente){
ActionProcessactionProcess=newActionProcess(this);
(e);
}catch(Exceptione1){
publicvoidrun()
while(true){
s=();
("
\n"
+s);
与服务器断开连接"
}catch(Exceptionep){}
}
(3)
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(){
Stringreceive=();
if(receive==null)
if("
黯然下线了"
)){
ServerThreadst=(ServerThread)(i);
***"
+receive+"
elseif("
上线了"
<
+getID()+"
>
:
"
+receive);
监听中"
***"
说"
大家"
else{
inttemp=("
inttemp1=("
StringtempS=(temp+1,temp1);
inti=(tempS);
st=(ServerThread)(getID());
ServerThreadst=(ServerThread)(getID());
publicvoidwrite(Stringmsg){
synchronized(out){
(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,"
文件已存在,请不要重复上传!
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)
packagechat;
publicclassfilesendServer{
publicvoidfilereceive()throwsException{
ServerSocketserverSocket=newServerSocket(1234);
服务已启动,绑定1234端口!
newfileServerThread(socket).start();
classfileServerThreadextendsThread{
Socketsocket;
publicfileServerThread(Socketsocket){
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)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 聊天 程序 课程设计
![提示](https://static.bdocx.com/images/bang_tan.gif)