多人聊天系统的设计与实现课程设计说明书.docx
摘要本课程设计主要是设计并实现一个简单的多人聊天程序,该聊天程序能够支持多人聊天,聊天的内容可以仅仅支持文本信息、,聊天程序包括效劳器程序和客户端程序。在课程设计中系统的开发平台为Eclipse程序运行平台为WindowsXPo关键词:聊天程序;效劳器;客户端;Eclipse目录1课题描述错误!未定义书签。2设计过程2效劳器模块32.2 客户端模块32.3 信息处理模块42.4 源代码43测试11总结13参考文献141课题描述随着社会开展,Interneni的迅速普及,人们对实时通信的要求越来越迫切,需求日益增加,网络聊天已经成为了一大时尚,不少人都有这样的经历。客户客户端/效劳器模式是一种能够在基于网络环境的分布处理过程中,使用基于连接的网络通信模型。该通信模型首先在客户机和效劳器之间定义一套通信协议,并创立一个SOCket类,利用这个类来建立一条可靠的链接:然后,客户端/效劳器再在这条连接上可靠地传输数据。客户端发出请求,效劳器发出请求,效劳器监听来自客户机的请求,并为客户端提供相应效劳。客户/效劳模式可靠性高、采用模块化设计所以它的优势很明显。是现代通信不可或缺的一局部。本课题主要是通过对简单聊天程序的设计及QQ原理的分析,了解聊天程序的功能需求及工作原理;熟悉TCP/IP协议以及套节字编程原理;熟悉Eclipse编程环境及SocketZServerSocket编程方法。开发工具:Eclipse2设计过程聊天室的总体设计过程比拟简单主要是构建设计框图。就是当客户端发出信息时,我们将数据写到SoCket,当对方有数据到达时,我们从SOCket读出来并显示给客户。程序关键在于如何监听到有信息到达的过程。应用程序流程图如下列图所示,开始是先创立主窗口,然后初始化窗口,程序下面才开始消息循环处理。如果没有消息循环的活,直接结束掉这个程序。如果有循环的话,要判断这个窗口消息的内容是哪种类型,然后再进行选择处理方式。流程图如图2-1所示图2-1效劳器模块图效劳器模块聊天室主要包括三个模块,效劳器模块、客户端模块、信息处理模块。该聊天室可以支持多人聊天,只支持文本信息。效劳器模块所包含的函数模块如下列图所示图2-2效劳器模块图客户端模块客户端所包含的函数模块如下列图所示图2-3客户端模块图信息处理模块信息处理模块包括注册信息及登录信息的处理模块,其所包含的函数模块如下列图所示图2-4信息处理模块图源代码/ChatSerpackagelee;importjava.util.*;importjava.io,*;publicclassChatServiceprivatestaticChatServicecs;privatePropertiesuserList;privateLinkedList<String>chatMsg;privateChatServiceOpublicstaticChatServiceinstance()if(cs=null)cs=newChatServiceO;returncs;publicbooleanvalidLogin(Stringuser,Stringpass)throwsIOExceptionif(loadUser().getProperty(user)=null)returnftlse;if(loadUser().getProperty(user).equals(pass)returntrue;returnfalse;publicbooleanaddUser(Stringname,Stringpass)throwsExceptionif(userList=null)userList=loadUser();if(userList.containsKey(name)thrownewEXCePtiOn("用户名已经存在,请重新选择用户名");userList.setProperty(name,pass);saveUserList();returntrue;publicStringgetMsg()if(chatMsg=null)chatMsg=newLinkedList<String>();retum"”;Stringresult=,m,;for(Stringimp:chatMsg)result+=tm÷"n"returnresult;publicvoidaddMsg(Stringuser,Stringmsg)if(chatMsg=null)chatMsg=newLinkedList<String>();if(chatMsg.size()>40)chatMsg.removeFirst();chatMsg.add(user+"说:”+msg);/下面是系统的工具方法privatePropertiesIoadUserOthrowsIOExceptionif(userList=null)Filef=newFile(nuserFile.propertiesn);if(!f.exists()EcreateNewFileO;userList=newProperties();userList.load(newFileInputStream(O);returnuserList;privatebooleansaveUserList()throwsIOExceptionif(userList=null)returnfalse;userList.store(newFileoUtPUtSlream("userFile.PrOPerlies”),"userList”);returntrue;/packagelee;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletExcetion;importjavax.servlet.Servlet;importjavax.servlet.ServletRequest;VletResponse;importjavax.servlet.ServletExcetion;importjavax.servlet.ServletContext;importjavax.servlet.RequestDispatcher;importjavax.servlet.Servlet;importjavax.servlet.ServletRequest;importjavax.servlet.ServletResponse;impoiljava.io.PrintWriter;importjava.io.IOException;publicclassChatServletextendsServlet*Constructoroftheobject.*/publicChatServletOsuer();*Destructionoftheservlet,<br>*/publicvoiddestroy()super.destroy();/Justputs"destroy"stringinlogPutyourcodehere*ThedoGetmethodoftheservlet,<br>*Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.*paramrequesttherequestsendbytheclienttotheserver*paramresponsetheresponsesendbytheservertotheclient*()throwsServletExceptionifanerroroccurred*()throwsIOExceptionifanerroroccurredpublicvoiddoGet(ServletRequestrequest,ServletResponseresponse)throwsServletException,IOException*ThedoPostmethodoftheservlet,<br>*Thismethodiscalledwhenaformhasitstagvaluemethodequalstopost.*paramrequesttherequestsendbytheclienttotheserver*(3)paramresponsetheresponsesendbytheservertotheclient*(2)throwsServletExceptionifanerroroccurred*(3)throwsIOExceptionifaneoroccurredpublicvoiddoPost(ServletRequestrequest,ServletResponseresponse)throwsServletException,IOException*Initializationoftheservlet,<br>*(3)throwsServletExceptionifanerroroccurspublicvoidinit()throwsServletExceptionPutyourcodehere)publicvoidservice(ServletRequestrequest,ServletResponseresponse)throwsIOException,ServletExceptionFequest-SetCharacterEncodingCrUTF-S'1);Stringmsg=request.getParameter("cha(Msg");if(msg!=null&&!msg.equals("")Stringuser=(Slring)request.getSession(true).gelAltribute("user");ChatService.instance().addMsg(user,msg);设置中文流response.setContentType(text/html;chaset=GBK,);PrintWriterout=response.getWriter();out.println(ChatService.instance().getMsg();3测试聊天室的程序模块如下列图所示图3-1聊天室程序模块图程序运行后,用户注册界面如下列图所示图3-2用户注册界面用户登录界面如下列图所示图3-3用户登录界面进入聊天室的聊天界面如下列图所示图3-4聊天室界面可以进行多人聊天,多人聊天如下列图所示图3-5多人聊天总结网络的课程设计,之前没有接触过网络编程的语言,所以感觉难度比拟大,也因为如此,在找资料及向老师同学的请教中学到了很多的东西。这次的课程设计是简单聊天程序,在平日的生活学习中,可以说QQ等即时