1、C#网络聊天系统的方案设计书与实现封面作者:PanHongliang仅供个人学习摘 要21世纪人类进入了一个以网络计算机为中心的信息主导型时代,现在人们可以使用计算机在瞬间迅速地处理和传递大量信息。网络的快速发展使得信息交流的速度和方式发生了巨大的变化,人们已经越来越明显地看到了计算机网络带来的社会生活方面的巨大变革。随着互联网技术的飞速进步,有关网络的技术层出不穷,网络聊天系统也成为了一种深受大众喜爱、应用广泛的工具。网络聊天是互联网上一种非常普及的应用,文中以.NET平台作为开发工具,使用Socket作为应用程序开发模型,可方便服务器与客户端建立连接,数据传输和断开连接。此外,在开发的过程
2、中还采用了多线程技术,以完成客户进程间的调度,实现多用户操作。文章中设计了一个简单的桌面聊天系统,包括服务器端和客户端两个部分,服务器和客户端之间通过Socket编程技术实现建立连接的过程,并进行通信。服务器端主要包括如下功能:显示登陆服务器主机及端口号,记录登陆用户并管理用户基本信息。客户端分为用户登陆和聊天两个子模块,两个模块包括如下功能:显示服务器端口、用户登陆、管理用户聊天记录、显示用户状态和私聊模式。关键词:网络聊天系统,服务器端,客户端,Socket,多线程ABSTRACTThe 21st century, mankind has entered a information-ori
3、ented era of network computer as the center , now people can use the computer rapidly processing and transmisting informationin an instant. The rapid development of network makes the exchange of information rate and manner has taken place tremendous changes, it is more and more evident that enormous
4、 changes of the social life brought about by the computer networks.As the internet is developing rapidly, the technology on network emerges one another instantly.The network chat system is also becoming a popular, widely-used tool.Network chat on the internet is a very popular application, in the te
5、xt, we use .NET platform as a development tool, and socket as the application development model, to facilitate the establishment of the client and server connectivity, data transmission and disconnect. In addition, in the process of developing also usesthe multi-threaded technology to complete the p
6、rocessfor scheduling, and multi-user operation. The article designs a simple desktop chat system, including server and client in two parts, between the client and server through Socket programming technology to connect the process, and communications.The server end mainly includes the following func
7、tions: showing landing server host and port number, users log records andmanageuser basic information. The customer end module was divided into two pieces of module, logining and chating, which include the following functions: showing server port, logining, the management of user chats, showing user
8、s state and private model.Keywords:Network Chat System, the server end, the client end, Socket, multi-threading technology目 录第1章引言11.1 课题的背景及意义11.2 课题的研究现状11.3 本文的组织方式2第2章关键技术及开发工具42.1 TCP/IP协议和Socket简介42.1.1 TCP协议简介52.1.2 端口简介62.1.3 名称空间62.1.4 Socket编程技术72.2 多线程技术92.3 开发工具介绍10第3章网络聊天系统分析123.1 系统实现目
9、标及基本原理123.2 需求分析123.2.1 功能需求分析123.2.2 性能需求分析133.3 系统体系结构13第4章系统设计154.1 系统模块分析154.2 服务器端的设计174.3 客户端的设计18第5章系统实现205.1 服务器端的实现205.1.1 服务器初始化215.1.2 建立与客户端连接225.1.3 与客户端通信235.1.4 向客户端发送命令235.2 客户端的实现245.2.1 登陆子模块的实现245.2.2 客户端聊天子模块的实现26结论30参考文献31致谢331.3本文的组织方式本文对网络聊天系统进行了详细的分析研究,并采用当今比较主流的技术进行了设实现计。本文共
10、有5章。第1章 引言 主要是给出课题的研究背景、研究的现状,并且概括了论文主要内容。第2章 关键技术及开发工具 介绍了聊天系统所采用的关键技术,并对系统开发工具进行了简单的介绍。第3章 系统分析与设计 首先介绍系统实现的基本原理,给出了要实现的目标并对其进行需求分析,并重点给出了系统的体系结构。第4章 系统详细设计 首先对系统的功能模块进行划分,并着重给出了系统的详细设计方案。第5章 系统实现 针对系统功能模块分别进行实现,包括服务器部分和客户端部分。总结 对所做的研究和设计进行总结。最后附上致谢和参考文献!本聊天系统的总体框架如图4.1所示:图4.2服务器端程序流程图图5.4用户沈亚坤的初始
11、窗口5.2.2客户端聊天子模块的实现在ChatClient工程中将Form1.cs重命名为ChatClientForm.cs,为了使用Socket对象和Thread对象,在代码文件中加入名字空间System.Net、System.Net.Sockets和System.Thread的引用。当加载窗体时,便会进入ChatClientForm_Load处理程序,在ChatClientForm_Load处理程序中,首先显示用户登录窗口,如图5.3所示。如果登录成功,那么获取与服务器的连接并得到与服务器数据交互的流通道,向服务器发送CONN请求命令,同时启动一个新的线程用于响应从服务器发回的信息(在方法
12、ServerResponse()中实现)。ServerResponse()方法用于和服务器进行数据通信,主要是接收从服务器发回的信息,根据不同的命令,执行相应的操作。当选中“悄悄话”复选框时,便会进入priCheckBox_CheckedChanged处理程序。用户进行私聊时的程序流程图如图5.5所示:图5.5私聊流程图在priCheckBox_CheckedChanged处理程序中主要对privatemode布尔属性进行设置,具体代码如下:private void priCheckBox_CheckedChange(object sender,System.EventArgs e)If(pr
13、iCheckBox.Checked) Privatemode=true;ElsePrivatemode=false;当单击“发送”按钮时,便会进入btnSend_Click处理程序。在btnSend_Click处理程序中,如果privatemode布尔属性值为false(说明不是私聊),将CHAT命令发送给服务器;否则(为私聊),将PRIV命令发送给服务器,注意命令格式一定要与服务器端的命令格式一致,用户私聊模式如图5.6所示。图5.6卢胜宝收到沈亚坤的信息后的窗口当单击“离开”按钮时,便进入了btnExit_Click处理程序。在btnExit_Click处理程序中,将EXIT命令发送给服务
14、器,此命令格式要与服务器端的命令格式一致,具体代码如下:Private void btnExit_Click(Object sender , System.EventArgs e)String message=”EXIT|”+UserAlias+”|;/将字符串转化为字符数组Byteoutbytes=System.Text.Encoding.ASCII.GetBytes(message.ToCharArray();/利用NetworkStream的Write方法发送Strm.Write(outbytes,0,outbytes.Length);用户离开后其他在线用户的窗口显示如图5.7所示:图5
15、.7 用户崔小东离开后卢胜宝的窗口从图中可以看出,用户崔小东离开后,卢胜宝的在线用户列表中的他的名字已经被删除,并在信息栏里给出提示。Powered by 版权申明本文部分内容,包括文字、图片、以及设计等在网上搜集整理。版权为潘宏亮个人所有This article includes some parts, including text, pictures, and design. Copyright is Pan Hongliangs personal ownership.用户可将本文的内容或服务用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定
16、,不得侵犯本网站及相关权利人的合法权利。除此以外,将本文任何内容或服务用于其他用途时,须征得本人及相关权利人的书面许可,并支付报酬。Users may use the contents or services of this article for personal study, research or appreciation, and other non-commercial or non-profit purposes, but at the same time, they shall abide by the provisions of copyright law and other
17、relevant laws, and shall not infringe upon the legitimate rights of this website and its relevant obligees. In addition, when any content or service of this article is used for other purposes, written permission and remuneration shall be obtained from the person concerned and the relevant obligee.转载
18、或引用本文内容必须是以新闻性或资料性公共免费信息为使用目的的合理、善意引用,不得对本文内容原意进行曲解、修改,并自负版权等法律责任。Reproduction or quotation of the content of this article must be reasonable and good-faith citation for the use of news or informative public free information. It shall not misinterpret or modify the original intention of the content of this article, and shall bear legal liability such as copyright.
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2