




已阅读5页,还剩31页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
网络程序设计课 程 设 计 报 告 书题 目: 简单的聊天室程序 专 业: 网络工程 完成日期: 2012年6月7日 一、 题目:简单的聊天室程序要求:本题是一个简单的聊天室程序,采用客户/服务器模式,分为客户端程序和服务器端程序。由于服务器只能支持一个客户,实际上是一个点对点通信的程序。客户端程序和服务器程序通过网络交换聊天字符串内容,并在窗口的列表框中显示。l 。二、 系统概要设计聊天室是分客户端和服务端两个应用程序的, 两个应用程序要想实现交互必 须编写相应的指令和识别指令的代码,我写的这是个指令依次是 启动 停止 用户退出 的命令,但用户想要进行以上动作中的任何 一个时,在用户按下按键的时候,客户端都是向服务端发送相应的指令,再由服 务端实际的执行。三、系统详细设计对概要设计中提到的功能函数进行详细设计。服务器端:/ ChatRoomServerDlg.cpp : implementation file/#include stdafx.h#include ChatRoomServer.h#include ChatRoomServerDlg.h#include ListenSocket.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialogpublic:CAboutDlg();/ Dialog Data/AFX_DATA(CAboutDlg)enum IDD = IDD_ABOUTBOX ;/AFX_DATA/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); / DDX/DDV support/AFX_VIRTUAL/ Implementationprotected:/AFX_MSG(CAboutDlg)/AFX_MSGDECLARE_MESSAGE_MAP();CAboutDlg:CAboutDlg() : CDialog(CAboutDlg:IDD)/AFX_DATA_INIT(CAboutDlg)/AFX_DATA_INITvoid CAboutDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CAboutDlg)/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CAboutDlg, CDialog)/AFX_MSG_MAP(CAboutDlg)/ No message handlers/AFX_MSG_MAPEND_MESSAGE_MAP()/ CChatRoomServerDlg dialogCChatRoomServerDlg:CChatRoomServerDlg(CWnd* pParent /*=NULL*/): CDialog(CChatRoomServerDlg:IDD, pParent)/AFX_DATA_INIT(CChatRoomServerDlg)/ NOTE: the ClassWizard will add member initialization here/AFX_DATA_INIT/ Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()-LoadIcon(IDR_MAINFRAME);void CChatRoomServerDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CChatRoomServerDlg)DDX_Control(pDX, IDC_BUTTON_STOP, m_IDC_BUTTON_STOP);DDX_Control(pDX, IDC_BUTTON_START, m_IDC_BUTTON_START);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CChatRoomServerDlg, CDialog)/AFX_MSG_MAP(CChatRoomServerDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)/AFX_MSG_MAPEND_MESSAGE_MAP()/ CChatRoomServerDlg message handlersBOOL CChatRoomServerDlg:OnInitDialog()CDialog:OnInitDialog();/ Add About. menu item to system menu./ IDM_ABOUTBOX must be in the system command range.ASSERT(IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX AppendMenu(MF_SEPARATOR);pSysMenu-AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);/ Set the icon for this dialog. The framework does this automatically/ when the applications main window is not a dialogSetIcon(m_hIcon, TRUE);/ Set big iconSetIcon(m_hIcon, FALSE);/ Set small icon/ TODO: Add extra initialization herem_IDC_BUTTON_STOP.EnableWindow(FALSE);return TRUE; / return TRUE unless you set the focus to a controlvoid CChatRoomServerDlg:OnSysCommand(UINT nID, LPARAM lParam)if (nID & 0xFFF0) = IDM_ABOUTBOX)CAboutDlg dlgAbout;dlgAbout.DoModal();elseCDialog:OnSysCommand(nID, lParam);/ If you add a minimize button to your dialog, you will need the code below/ to draw the icon. For MFC applications using the document/view model,/ this is automatically done for you by the framework.void CChatRoomServerDlg:OnPaint() if (IsIconic()CPaintDC dc(this); / device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);/ Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;/ Draw the icondc.DrawIcon(x, y, m_hIcon);elseCDialog:OnPaint();/ The system calls this to obtain the cursor to display while the user drags/ the minimized window.HCURSOR CChatRoomServerDlg:OnQueryDragIcon()return (HCURSOR) m_hIcon;void CChatRoomServerDlg:OnButtonStart() / TODO: Add your control notification handler code herem_IDC_BUTTON_START.EnableWindow(FALSE);/使启动按钮无效ListenSocket.Create(6767);/创建监听套接字端口为6767ListenSocket.Listen();/开始监听m_IDC_BUTTON_STOP.EnableWindow(TRUE);/将停止按钮激活 void CChatRoomServerDlg:OnButtonStop() / TODO: Add your control notification handler code herem_IDC_BUTTON_STOP.EnableWindow(FALSE);/使停止按钮无效ListenSocket.Close();/关闭监听套接字m_IDC_BUTTON_START.EnableWindow(TRUE);/将启动按钮激活 / ChatRoomServer.h : main header file for the CHATROOMSERVER application/#if !defined(AFX_CHATROOMSERVER_H_680EC642_E19B_4D55_88DF_2C9E9B1B30FD_INCLUDED_)#define AFX_CHATROOMSERVER_H_680EC642_E19B_4D55_88DF_2C9E9B1B30FD_INCLUDED_#if _MSC_VER 1000#pragma once#endif / _MSC_VER 1000#ifndef _AFXWIN_H_#error include stdafx.h before including this file for PCH#endif#include resource.h/ main symbols/ CChatRoomServerApp:/ See ChatRoomServer.cpp for the implementation of this class/class CChatRoomServerApp : public CWinApppublic:CChatRoomServerApp();/ Overrides/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CChatRoomServerApp)public:virtual BOOL InitInstance();/AFX_VIRTUAL/ Implementation/AFX_MSG(CChatRoomServerApp)/ NOTE - the ClassWizard will add and remove member functions here./ DO NOT EDIT what you see in these blocks of generated code !/AFX_MSGDECLARE_MESSAGE_MAP();/AFX_INSERT_LOCATION/ Microsoft Visual C+ will insert additional declarations immediately before the previous line.#endif / !defined(AFX_CHATROOMSERVER_H_680EC642_E19B_4D55_88DF_2C9E9B1B30FD_INCLUDED_)/ ChatRoomServerDlg.h : header file/#if !defined(AFX_CHATROOMSERVERDLG_H_5BE648B6_8A9C_4E90_BF1D_20FE943A525F_INCLUDED_)#define AFX_CHATROOMSERVERDLG_H_5BE648B6_8A9C_4E90_BF1D_20FE943A525F_INCLUDED_#include ClientSocketList.h/ Added by ClassView#include ListenSocket.h/ Added by ClassView#if _MSC_VER 1000#pragma once#endif / _MSC_VER 1000/ CChatRoomServerDlg dialogclass CChatRoomServerDlg : public CDialog/ Constructionpublic:CListenSocket ListenSocket;CChatRoomServerDlg(CWnd* pParent = NULL);/ standard constructor/ Dialog Data/AFX_DATA(CChatRoomServerDlg)enum IDD = IDD_CHATROOMSERVER_DIALOG ;CButtonm_IDC_BUTTON_STOP;CButtonm_IDC_BUTTON_START;/AFX_DATA/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CChatRoomServerDlg)protected:virtual void DoDataExchange(CDataExchange* pDX);/ DDX/DDV support/AFX_VIRTUAL/ Implementationprotected:HICON m_hIcon;/ Generated message map functions/AFX_MSG(CChatRoomServerDlg)virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();afx_msg void OnButtonStart();afx_msg void OnButtonStop();/AFX_MSGDECLARE_MESSAGE_MAP();/AFX_INSERT_LOCATION/ Microsoft Visual C+ will insert additional declarations immediately before the previous line.#endif / !defined(AFX_CHATROOMSERVERDLG_H_5BE648B6_8A9C_4E90_BF1D_20FE943A525F_INCLUDED_)#if !defined(AFX_CLIENTSOCKET_H_5B707F31_3AD5_4F47_B58E_ECFC99EB60F0_INCLUDED_)#define AFX_CLIENTSOCKET_H_5B707F31_3AD5_4F47_B58E_ECFC99EB60F0_INCLUDED_#if _MSC_VER 1000#pragma once#endif / _MSC_VER 1000/ ClientSocket.h : header file/ CClientSocket command targetclass CClientSocketList;class CClientSocket : public CSocket/ Attributespublic:/ Operationspublic:CClientSocket(CClientSocketList *);virtual CClientSocket();/ Overridespublic:CClientSocketList *List;CClientSocket * Front;CClientSocket * Next;/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CClientSocket)public:virtual void OnReceive(int nErrorCode);virtual void OnClose(int nErrorCode);/AFX_VIRTUAL/ Generated message map functions/AFX_MSG(CClientSocket)/ NOTE - the ClassWizard will add and remove member functions here./AFX_MSG/ Implementationprotected:;/AFX_INSERT_LOCATION/ Microsoft Visual C+ will insert additional declarations immediately before the previous line.#endif / !defined(AFX_CLIENTSOCKET_H_5B707F31_3AD5_4F47_B58E_ECFC99EB60F0_INCLUDED_)/ ClientSocketList.h: interface for the CClientSocketList class./#if !defined(AFX_CLIENTSOCKETLIST_H_E746355D_FA10_4D12_B544_2FF152C16414_INCLUDED_)#define AFX_CLIENTSOCKETLIST_H_E746355D_FA10_4D12_B544_2FF152C16414_INCLUDED_#include ClientSocket.h#if _MSC_VER 1000#pragma once#endif / _MSC_VER 1000class CClientSocketList public:BOOL Sends(CClientSocket *);BOOL Add(CClientSocket *);CClientSocket * Head;CClientSocketList();virtual CClientSocketList();#endif / !defined(AFX_CLIENTSOCKETLIST_H_E746355D_FA10_4D12_B544_2FF152C16414_INCLUDED_)#if !defined(AFX_LISTENSOCKET_H_5D655304_370E_4680_A556_E417552D24EC_INCLUDED_)#define AFX_LISTENSOCKET_H_5D655304_370E_4680_A556_E417552D24EC_INCLUDED_#if _MSC_VER 1000#pragma once#endif / _MSC_VER 1000/ ListenSocket.h : header file/#include ClientSocketList.h/ CListenSocket command targetclass CListenSocket : public CSocket/ Attributespublic:/ Operationspublic:CClientSocketList CCSL;/客户socket列表CListenSocket();virtual CListenSocket();/ Overridespublic:/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CListenSocket)public:virtual void OnAccept(int nErrorCode);/重载OnAccept函数/AFX_VIRTUAL/ Generated message map functions/AFX_MSG(CListenSocket)/ NOTE - the ClassWizard will add and remove member functions here./AFX_MSG/ Implementationprotected:;/AFX_INSERT_LOCATION/ Microsoft Visual C+ will insert additional declarations immediately before the previous line.#endif / !defined(AFX_LISTENSOCKET_H_5D655304_370E_4680_A556_E417552D24EC_INCLUDED_)/ ChatRoomServer.cpp : Defines the class behaviors for the application./#include stdafx.h#include ChatRoomServer.h#include ChatRoomServerDlg.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CChatRoomServerAppBEGIN_MESSAGE_MAP(CChatRoomServerApp, CWinApp)/AFX_MSG_MAP(CChatRoomServerApp)/ NOTE - the ClassWizard will add and remove mapping macros here./ DO NOT EDIT what you see in these blocks of generated code!/AFX_MSGON_COMMAND(ID_HELP, CWinApp:OnHelp)END_MESSAGE_MAP()/ CChatRoomServerApp constructionCChatRoomServerApp:CChatRoomServerApp()/ TODO: add construction code here,/ Place all significant initialization in InitInstance/ The one and only CChatRoomServerApp objectCChatRoomServerApp theApp;/ CChatRoomServerApp initializationBOOL CChatRoomServerApp:InitInstance()if (!AfxSocketInit()AfxMessageBox(IDP_SOCKETS_INIT_FAILED);return FALSE;AfxEnableControlContainer();/ Standard initialization/ If you are not using these features and wish to reduce the size/ of your final executable, you should remove from the following/ the specific initialization routines you do not need.#ifdef _AFXDLLEnable3dControls();/ Call this when using MFC in a shared DLL#elseEnable3dControlsStatic();/ Call this when linking to MFC statically#endifCChatRoomServerDlg dlg;m_pMainWnd = &dlg;int nResponse = dlg.DoModal();if (nResponse = IDOK)/ TODO: Place code here to handle when the dialog is/ dismissed with OKelse if (nResponse = IDCANCEL)/ TODO: Place code here to handle when the dialog is/ dismissed with Cancel/ Since the dialog has been closed, return FALSE so that we exit the/ application, rather than start the applications message pump.return FALSE;II 客户端: 客户端: / ChatRoomClient.cpp : Defines the class behaviors for the application./#include stdafx.h#include ChatRoomClient.h#include ChatRoomClientDlg.h#include ConnectedDlg.h#include ClientSocket.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endifCClientSocket curSocket;/ CChatRoomClientAppBEGIN_MESSAGE_MAP(CChatRoomClientApp, CWinApp)/AFX_MSG_MAP(CChatRoomClientApp)/ NOTE - the ClassWizard will add and remove mapping macros here./ DO NOT EDIT what you see in these blocks of generated code!/AFX_MSGON_COMMAND(ID_HELP, CWinApp:OnHelp)END_MESSAGE_MAP()/ CChatRoomClientApp constructionCChatRoomClientApp:CChatRoomClientApp()/ TODO: add construction code here,/ Place all significant initialization in InitInstance/ The one and only CChatRoomClientApp objectCChatRoomClientApp theApp;/ CChatRoomClientApp initializationBOOL CChatRoomClientApp:InitInstance()if (!AfxSocketInit()AfxMessageBox(IDP_SOCKETS_INIT_FAILED);return FALSE;AfxEnableControlContainer();/ Standard initialization/ If you are not using these features and wish to reduce the size/ of your final executable, you should remove from the following/ the specific initialization routines you do not need.#ifdef _AFXDLLEnable3dControls();/ Call this when using MFC in a shared DLL#elseEnable3dControlsStatic();/ Call this when linking to MFC statically#endifCConnectedDlg cdlg(&curSocket);if (cdlg.DoModal()=IDCANCEL) return FALSE;CChatRoomClientDlg dlg(&curSocket);m_pMainWnd = &dlg;curSocket.SetDlg(&dlg);int nResponse = dlg.DoModal();if (nResponse = IDOK)/ TODO: Place code here to handle when the dialog is/ dismissed with OKelse if (nResponse = IDCANCEL)/ TODO: Place code here to handle when the dialog is/ dismissed with Cancel/ Since the dialog has been closed, return FALSE so that we exit the/ application, rather than start the applications message pump.return FALSE;/ ChatRoomClientDlg.cpp : implementation file/#include stdafx.h#include ChatRoomClient.h#include ChatRoomClientDlg.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialogpublic:CAboutDlg();/ Dialog Data/AFX_DATA(CAboutDlg)enum IDD = IDD_ABOUTBOX ;/AFX_DATA/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); / DDX/DDV support/AFX_VIRTUAL/ Implementationprotected:/AFX_MSG(CAboutDlg)/AFX_MSGDECLARE_MESSAGE_MAP();CAboutDlg:CAboutDlg() : CDialog(CAboutDlg:IDD)/AFX_DATA_INIT(CAboutDlg)/AFX_DATA_INITvoid CAboutDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CAboutDlg)/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CAboutDlg, CDialog)/AFX_MSG_MAP(CAboutDlg)/ No message handlers/AFX_MSG_MAPEND_MESSAGE_MAP()/ CChatRoomClientDlg dialogCChatRoomClientDlg:CChatRoomClientDlg(CClientSocket *tmp,CWnd* pParent /*=NULL*/): CDialog(CChatRoomCli
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论