java聊天小程序课程设计.docx

上传人:b****4 文档编号:5419574 上传时间:2023-05-08 格式:DOCX 页数:22 大小:22.55KB
下载 相关 举报
java聊天小程序课程设计.docx_第1页
第1页 / 共22页
java聊天小程序课程设计.docx_第2页
第2页 / 共22页
java聊天小程序课程设计.docx_第3页
第3页 / 共22页
java聊天小程序课程设计.docx_第4页
第4页 / 共22页
java聊天小程序课程设计.docx_第5页
第5页 / 共22页
java聊天小程序课程设计.docx_第6页
第6页 / 共22页
java聊天小程序课程设计.docx_第7页
第7页 / 共22页
java聊天小程序课程设计.docx_第8页
第8页 / 共22页
java聊天小程序课程设计.docx_第9页
第9页 / 共22页
java聊天小程序课程设计.docx_第10页
第10页 / 共22页
java聊天小程序课程设计.docx_第11页
第11页 / 共22页
java聊天小程序课程设计.docx_第12页
第12页 / 共22页
java聊天小程序课程设计.docx_第13页
第13页 / 共22页
java聊天小程序课程设计.docx_第14页
第14页 / 共22页
java聊天小程序课程设计.docx_第15页
第15页 / 共22页
java聊天小程序课程设计.docx_第16页
第16页 / 共22页
java聊天小程序课程设计.docx_第17页
第17页 / 共22页
java聊天小程序课程设计.docx_第18页
第18页 / 共22页
java聊天小程序课程设计.docx_第19页
第19页 / 共22页
java聊天小程序课程设计.docx_第20页
第20页 / 共22页
亲,该文档总共22页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

java聊天小程序课程设计.docx

《java聊天小程序课程设计.docx》由会员分享,可在线阅读,更多相关《java聊天小程序课程设计.docx(22页珍藏版)》请在冰点文库上搜索。

java聊天小程序课程设计.docx

java聊天小程序课程设计

课程设计

题目聊天小程序(socket编程+多线程)

学院商学院

专业信息管理与信息系统

班级信息111

学号8

学生姓名张语轩

指导教师余法红

编写日期

、需求分析

当今主流的聊天工具有QQUCMSNMessenger等,国内最热门的当属QQ

天工具。

您可以使用QQ和好友用户进行交流,信息即时发送,即时回复,收发及时、功能全面。

此外QQ还具有BP机网上寻呼、聊天室、传输文件、语音邮件、手机短讯服务等功能,QQ不仅仅是虚拟的网络寻呼机,更可与传统的无线寻呼网、GSM移动电话的短消息系统互联,目前QQ和全国多家寻呼台、移动通信公司有业

务合作。

是国内不可多得的中文网络寻呼机。

QQ支持显示朋友在线信息、即时传

送信息、即时交谈、即时发送文件和网址。

QC可以在Win95/98/NT/2000操作系统下运行,是十分灵活的网络寻呼工具。

它会自动检查您是否已联网,如果您的电脑已连入Internet,可以搜索网友、显示在线网友,可以根据QC号、昵称、姓名、email地址等关键词来查找,找到后可加入到通讯录中。

当您的通讯录中的网友在线时,QQ中朋友的头像就会显示在线,根据提示就可以发送信息,如果对方登记了寻呼机或幵通了GSM手机短消息,即使离线了,您也可及时将信息传递给您的好友。

虽然以上这些软件是免费的,并且功能越来越强大。

但是它们的唯一的不足就是必须要联网或者下载到它们的服务端,要不然是没法脱离Intenet使用的。

为了能在一个没有连接到Intenet的局域网使用聊天工具,那就必须要有服务器端。

本系统就是包括服务器端与客户端,可以在局域网聊天的一个工具。

二、系统概述

我们实现的是局域网中的聊天功能,运行服务程序后,服务器创建成功,然

后使服务器对某一个设定的端口处于监听状态,当有客户端请求建立连接时,服务器就可接受请求,建立连接!

这样服务器和客户端就可相互发送消息,实现聊

天功能!

系统的功能要求

A.服务器端主要功能如下:

1.在特定端口上进行侦听,等待客户端连接。

2.用户可以配置服务器端的侦听端口,默认端口为8888。

3.向已经连接到服务器端的用户发送系统消息。

4.统计在线人数。

5.当停止服务时,断开所有的用户连接。

B.客户端的主要功能如下:

1.连接到已经开启聊天服务的服务器端。

2.用户可以配置要连接的服务器端的IP地址和端口号。

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()==

{

try

{

filesendServerfilesendserver=newfilesendServer();

();

}

catch(Exceptionee){();

}}

if()=={

try{

filesendClientfilesendclient=newfilesendClient();

();

}catch(Exceptionee){}

}

if()==

{

try{

}catch(IOExceptione1){

();

}

(0);

}

if("表情")){

}else{em+="着";

}if()==

{

if(s!

=null)

{

try

{

}

catch(IOExceptione1){}

}

}if()==

{

sendtoID=("请输入对方ID:

");sendtoname=("请输入对方姓名:

");if(s!

=null)

{

try

{}

catch(IOExceptione1){}

}

}

}}publicvoidrun()

{

Strings=null;while(true)

try

{

}catch(IOExceptione)

{

try

{

}catch(Exceptionep){}break;

}

}

}

}

(2)packagechat;import.*;import.*;import.*;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);

(false);

(false);

(false);

inputName=newTextField(6);

(newColor(162,231,250));

inputContent=newTextField(22);

(newColor(162,231,250));

chatResult=newTextArea("",17,20,;

(newColor(162,231,250));

JLabeljlname=newJLabel("输入昵称");

(jlname);

(inputName);

(listen);

(connection);

(filesend);

(exit);

Boxbox2=();

emote=newJComboBox();

(newDefaultComboBoxModel(newString[]{"表情","微笑","甜笑","惊喜","嘻嘻","扮酷","嘿嘿","傻笑","好奇","媚眼","鬼脸","陶醉","害

羞","生气","嚷嚷","发怒","伤心","高明","菜鸟","问号","狂笑大哭","示爱","呻吟","想想"}));

(false);

JLabeljlintput=newJLabel("输入聊天内容");

(jlintput);

(inputContent);

(emote);

(sendtoall);

(sendtoone);

(this);

(this);

(this);

(this);

(this);

(this);

Boxbox3=();

(chatResult);

thread=newThread(this);

Boxbox0=();

(10));

(box1);

(box3);

(10));

(box2);

(10));

add(box0);

setBounds(10,30,500,400);

setBackground(newColor(80,212,248));

setVisible(true);

validate();

addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){(0);

try{

();

}catch(IOExceptione1){

();

}

}

});

}

publicvoidactionPerformed(ActionEvente){

ActionProcessactionProcess=newActionProcess(this);

try{

(e);

}catch(Exceptione1){

();

}

}

publicvoidrun()

{

Strings=null;

while(true){

try

{

s=();

("\n"+s);

}

catch(IOExceptione)

{

("与服务器断开连接");

try

{

();

}catch(Exceptionep){}

break;

}

}

}

}

(3)

packagechat;

import.*;

import.*;

import.*;

publicclassChatServer

{

publicstaticvoidmain(String[]args)throwsException{ServerSocketsvSocket=null;

Vectorthreads=newVector();

try{

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;

try{

in=newDataInputStream());out=newDataOutputStream());

}

catch(Exceptionex){

}

}

publicvoidrun(){try{while(true){Stringreceive=();if(receive==null)return;

if("黯然下线了")){for(inti=0;i<();i++){ServerThreadst=(ServerThread)(i);("***"+receive+"***");

}

}elseif("上线了")){for(inti=0;i<();i++){

ServerThreadst=(ServerThread)(i);

("<"+getID()+">:

"+receive);

}

}

elseif("监听中")){

for(inti=0;i<();i++){

ServerThreadst=(ServerThread)(i);("***"+receive+"***");

}

}

elseif("说")){

if("大家")){

for(inti=0;i<();i++){

ServerThreadst=(ServerThread)(i);

("<"+getID()+">:

"+receive);

}

}

else{

inttemp=("<");

inttemp1=(">");

StringtempS=(temp+1,temp1);

inti=(tempS);

ServerThreadst=(ServerThread)(i);

("<"+getID()+">:

"+receive);

st=(ServerThread)(getID());

("<"+getID()+">:

"+receive);

}

}

else{

ServerThreadst=(ServerThread)(getID());

("***"+receive+"***");

}

}

}catch(Exceptionex){

(this);

();

}

try{();

}catch(Exceptionex){

();

}

}

publicvoidwrite(Stringmsg){

synchronized(out){try{

(msg);

}catch(Exceptionex){

}

}

}

publicintgetID(){

return;

}

publicvoidsetID(intnid){

=nid;

}

}

(4)

packagechat;

publicclassfilesendClient{

publicvoidfilesend()throwsException{

StringsendtoIP=("请输入对方IP:

");Socketsocket=newSocket(sendtoIP,1234);BufferedReaderbr=newBufferedReader(newInputStreamReader()));

PrintStreamps=newPrintStream());

Filefile=getFile();

());

());

Stringmsg=();

if("已存在".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(!

()){

(null,"您输入的路径不存在,请重新输入!

");flag=false;

}

elseif()){

(null,"占不支持文件夹上传!

请输入一个文件路径!

");flag=false;

}

elseflag=true;

}

returnfile;

}

}

(5)

packagechat;

publicclassfilesendServer{

publicvoidfilereceive()throwsException{

ServerSocketserverSocket=newServerSocket(1234);

(null,"服务已启动,绑定1234端口!

");

while(true){

Socketsocket=();

newfileServerThread(socket).start();

}

}

}

classfileServerThreadextendsThread{

Socketsocket;

publicfileServerThread(Socketsocket){

=socket;

}

publicvoidrun(){

FileOutputStreamfos=null;

try{

BufferedReaderbr=newBufferedReader(newInputStreamReader()));

PrintStreamps=newPrintStream());

StringfileName=();

longfileLen=());

Filedir=newFile("upload");

();

Filefile=newFile(dir,fileName);

if()&&()==fileLen){("已存在");

return;

}

else{

());

}

Stringtime=newSimpleDateFormat("yyyy-MM-ddHH:

mm:

ss").format(newDate());

longstart=();

InputStreamin=();

fos=newFileOutputStream(file,true);

byte[]buffer=newbyte[1024];

intlen;

while((len=(buffer))!

=-1){

(buffer,0,len);

if()==fileLen)

break;

}

();

longend=();

time=newSimpleDateFormat("yyyy-MM-ddHH:

mm:

ss").format(newDate());

("上传成功");

();

}catch(IOExceptione){

if(fos!

=null)

try{

();

}catch(IOExceptione1){

();

}

}

}

}

运行结果:

将5个Java源文件:

、、、、。

分别编译这5个Java源文件,或运行“javac*.java命令,编译全部的源文件,然后运行连接服务器的主类,即。

服务器启动成功结果分析:

在进入聊天室之前必须先启动服务器,实现服务器端的连接。

然后运行

结果分析:

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

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2