c++聊天室设计报告

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

计算机科学与技术学部C++课程设计题目聊天室学部计算机科学与技术班级计科1103指导教师李军姓名周荣汪婷婷2012年6月2日聊天室摘要当前是数字信息时代,网络时代,任何一种编辑工具都不能忽视在网络上的应用,并且随着Internet的普及和宽带技术的成熟,与此同时一个新型的Web应用程序为网民之间提供了一个实时通信的功能和场所,这就是聊天室。在网络早期就开始盛行,不论是BBS,IRC都有类似网上聊天室的机制。聊天室为网友提供了实时性对话的渠道,是网络上极为流行的一项服务。聊天室就好比计算机上的一个应用程序正在等待另一个程序打开通信连接,很像您在等待某人给您打电话时,一直在留心电话铃响,与此同时,另一个应用程序试图同第一个应用程序连接,这种试图打开连接的方式就类似于向某人打电话,您拨了电话号码,并希望要找的那个人正在电话线的另一头等待接电话,一旦两个应用程序间建立了连接,它们之间就可以互相发送和接收信息了,正像两个人打电话交谈一样.最后,如果一方或双方完成了对话,连接即被关闭,就像打完电话后把电话挂断一样.关键词:客户端;服务器;CSocket;VC++聊天室课程设计任务分配表表1信息表姓名主要工作组长周荣服务器端、制作Word文档组员汪婷婷客户端、制作Word文档课程设计成绩表表2成绩表姓名学号班级成绩组长周荣11430524816814计科1103组员汪婷婷11430522814059计科1103聊天室目录第1章前言··················································································11.1简介说明··············································································11.1.1设计目的······································································11.1.2可行性分析···································································11.2需求分析·············································································11.3设计内容·············································································2第2章系统功能分析·········································································32.1客户端的实现········································································32.1.1创建一个新类CWCSock,用于与服务器通信·························32.1.2建立与服务器的连接·····················································42.1.3向服务器发送数据························································42.1.4接收服务器发来的数据···················································52.1.5关闭与服务器的连接······················································52.1.6处理自定义消息····························································62.1.7控件的状态更新····························································72.2服务器端的实现·····································································72.2.1创建创建侦听类CLSock··················································72.2.2增加一个读写类CRWSock················································82.2.3接受客户的信息···························································92.2.4处理客户的连接请求·····················································112.2.5启动服务器·································································112.2.6关闭服务器·································································122.2.7控件的状态更新···························································12第3章系统测试·············································································143.1程序运行测试······································································143.2.1程序主界面图·····························································143.2.2程序连接图································································153.2.3服务器没有启动时的连接图···········································173.2.4客户端与服务器端聊天图··············································173.2.5关闭客户端,服务器端显示图········································18总结·······························································································19参考文献·························································································20聊天室1第一章前言1.1简介说明1.1.1设计目的本课程设计为学生提供了一个既动手又动脑,自学,查资料,独立实践的机会。将本学期课本上的理论知识和实际有机的结合起来,锻炼学生实际分析问题和解决问题的能力,提高学生适应实际、实践编程的能力,使对C++系统编程有一个大致的了解。1.1.2可行性分析本课程设计主要解决客户端于客户端的信息交换和客户端于服务器端的信息交换及服务器的信息处理上的管理设计。此程序主要分为两个部分:服务器端和客户端。服务器端用于提供一个网络端口,等待客户端发出请求,登录到此服务器端,然后进行网络通迅和消息的转发;客户端可通过服务器端的端口发送连接请求,然后登录聊天室。先启动服务器聊天程序,这时聊天服务器需要指定一个端口号,客户端则根据这个端口号以及服务器的网络地址进行通信。在这里,把端口号成为聊天频道,在后面的程序代码分析中看到,端口号并不等于聊天频道,而是在聊天频道上增加一个固定的偏移值,使得这个聊天频道不会和系统保留的端口发生冲突。服务器启动后将在这个指定的端口中等待客户的连接。通过分析,该程序完全可以在VisualC++中MFC完成。1.2需求分析设计要求:采用客户端/服务器模式,分为客户端与服务器端程序功能要求:客户端能够与服务器连接,客户端能够发送信息,服务器端能够接收信息。主要功能描述:服务器端:服务器端聊天程序在待定窗口上等待来自客户端的连接请求,这要求可以通过CSocket类提供的功能实现。从CSocket派生出两个类CLSock和聊天室2CRWSock,它们分别用来侦听客户的连接请求与客户的连接和读写客户端发来的信息。接收客户端发送的信息,并显示的一个列表框。客户端:客户端聊天要负责建立和维护与服务器的连接,通过获取服务器的端口与服务器连接,并且随时检测连接的状态。客户端聊天程序要把用户输入的的信息及时发送到聊天服务器。一般情况下,当用户输入一行信息并且按下发送按钮把信息发送出去,才能用时满足交互要求。1.3设计内容1.这个聊天室应用程序是基于客户端/服务器端模型的,一个服务器只能支持一个客户,因此它实际上是一个基于“点对点”模型的应用程序.通过这个应用程序,用户可以在两台计算机之间进行字符串传输—即可以为两个人进行网上聊天提供服务,而且两个人的传输的字符串被保留在程序的列表框中,并且进行分类,分为传送和接收的字符串。2.聊天室的工作流程客户机Socket()Bind()Connect()Send()Receive()CloseSocket()服务器侦听套接字连接套接字Socket()Bind()Listen()Accept()创建连接套接字字Receive()Receive()CloseSocket()CloseSocket()聊天室3第二章系统功能分析2.1客户端的实现下面将依次介绍建立与服务器的连接模块、向服务器发送数据模块、接收服务器发来的数据模块,关闭与服务器的连接。2.1.1创建一个新类CWCSock,用于与服务器通信选择“Insert”菜单的“NewClass”菜单项,在NewClass对话框中,Bassclass列表框中选取CSocket作为基类,单击“确定”按钮,VC++就为ClientChat工程创建了一个新类CClientSock,相应地为工程创建了两个文件CClientSock.h和CClientSock.cpp。图2.0新建CClientSock类聊天室42.1.2建立与服务器的连接聊天室首先要执行的一个模块,只有与服务器端连接好才能通信。voidCCClientChatDlg::OnConnect(){//TODO:AddyourcontrolnotificationhandlercodehereUpdateData(true);if(!sockClient.Create()){AfxMessageBox(CreateWCsocketfailed);PostQuitMessage(0);}if(!sockClient.Connect(m_server_name,m_nPort)){MessageBox(连接失败!);sockClient.Close();return;}m_Dat.m_bOnline=true;GetDlgItem(IDC_CONNECT)-EnableWindow(false);GetDlgItem(IDC_SEND)-EnableWindow(true);GetDlgItem(IDC_CLOSE)-EnableWindow(true);GetDlgItem(IDC_SERVER_PORT)-EnableWindow(false);GetDlgItem(IDC_CLIE

1 / 24
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功