VC(MFC)實現(xiàn) 簡單的聊天室程序.doc_第1頁
VC(MFC)實現(xiàn) 簡單的聊天室程序.doc_第2頁
VC(MFC)實現(xiàn) 簡單的聊天室程序.doc_第3頁
VC(MFC)實現(xiàn) 簡單的聊天室程序.doc_第4頁
VC(MFC)實現(xiàn) 簡單的聊天室程序.doc_第5頁
已閱讀5頁,還剩31頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

網(wǎng)絡程序設計課 程 設 計 報 告 書題 目: 簡單的聊天室程序 專 業(yè): 網(wǎng)絡工程 完成日期: 2012年6月7日 一、 題目:簡單的聊天室程序要求:本題是一個簡單的聊天室程序,采用客戶/服務器模式,分為客戶端程序和服務器端程序。由于服務器只能支持一個客戶,實際上是一個點對點通信的程序??蛻舳顺绦蚝头掌鞒绦蛲ㄟ^網(wǎng)絡交換聊天字符串內(nèi)容,并在窗口的列表框中顯示。l 。二、 系統(tǒng)概要設計聊天室是分客戶端和服務端兩個應用程序的, 兩個應用程序要想實現(xiàn)交互必 須編寫相應的指令和識別指令的代碼,我寫的這是個指令依次是 啟動 停止 用戶退出 的命令,但用戶想要進行以上動作中的任何 一個時,在用戶按下按鍵的時候,客戶端都是向服務端發(fā)送相應的指令,再由服 務端實際的執(zhí)行。三、系統(tǒng)詳細設計對概要設計中提到的功能函數(shù)進行詳細設計。服務器端:/ 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);/創(chuàng)建監(jiān)聽套接字端口為6767ListenSocket.Listen();/開始監(jiān)聽m_IDC_BUTTON_STOP.EnableWindow(TRUE);/將停止按鈕激活 void CChatRoomServerDlg:OnButtonStop() / TODO: Add your control notification handler code herem_IDC_BUTTON_STOP.EnableWindow(FALSE);/使停止按鈕無效ListenSocket.Close();/關閉監(jiān)聽套接字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函數(shù)/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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論