《网络程序设计课程设计vc(mfc)实现简单的聊天室程序》由会员分享,可在线阅读,更多相关《网络程序设计课程设计vc(mfc)实现简单的聊天室程序(35页珍藏版)》请在金锄头文库上搜索。
1、 网络程序设计课 程 设 计 报 告 书题 目: 简单的聊天室程序 专 业: 网络工程 完成日期: 一、题目:简单的聊天室程序要求:本题是一个简单的聊天室程序,采用客户/服务器模式,分为客户端程序和服务器端程序。由于服务器只能支持一个客户,实际上是一个点对点通信的程序。客户端程序和服务器程序通过网络交换聊天字符串内容,并在窗口的列表框中显示。二、 系统概要设计聊天室是分客户端和服务端两个应用程序的, 两个应用程序要想实现交互必 须编写相应的指令和识别指令的代码,我写的这是个指令依次是 启动 停止 用户退出 的命令,但用户想要进行以上动作中的任何 一个时,在用户按下按键的时候,客户端都是向服务端
2、发送相应的指令,再由服 务端实际的执行。三、系统详细设计对概要设计中提到的功能函数进行详细设计。服务器端:/ ChatRoomServerDlg.cpp : implementation file /#include “stdafx.h“ #include “ChatRoomServer.h“ #include “ChatRoomServerDlg.h“ #include “ListenSocket.h“ #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE = _FILE_; #endif/ /
3、CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog public: 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/D
4、DV support /AFX_VIRTUAL/ Implementation protected: /AFX_MSG(CAboutDlg) /AFX_MSG DECLARE_MESSAGE_MAP() ;CAboutDlg:CAboutDlg() : CDialog(CAboutDlg:IDD) /AFX_DATA_INIT(CAboutDlg) /AFX_DATA_INIT void CAboutDlg:DoDataExchange(CDataExchange* pDX) CDialog:DoDataExchange(pDX); /AFX_DATA_MAP(CAboutDlg) /AFX_
5、DATA_MAP BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) /AFX_MSG_MAP(CAboutDlg) / No message handlers /AFX_MSG_MAP END_MESSAGE_MAP()/ / CChatRoomServerDlg dialogCChatRoomServerDlg:CChatRoomServerDlg(CWnd* pParent /*=NULL*/) : CDialog(CChatRoomServerDlg:IDD, pParent) /AFX_DATA_INIT(CChatRoomServerDlg) / NOTE:
6、 the ClassWizard will add member initialization here /AFX_DATA_INIT / Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()-LoadIcon(IDR_MAINFRAME); void CChatRoomServerDlg:DoDataExchange(CDataExchange* pDX) CDialog:DoDataExchange(pDX); /AFX_DATA_MAP(CChatRoomSe
7、rverDlg) DDX_Control(pDX, IDC_BUTTON_STOP, m_IDC_BUTTON_STOP); DDX_Control(pDX, IDC_BUTTON_START, m_IDC_BUTTON_START); /AFX_DATA_MAP BEGIN_MESSAGE_MAP(CChatRoomServerDlg, CDialog) /AFX_MSG_MAP(CChatRoomServerDlg)ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON_START, O
8、nButtonStart) ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop) /AFX_MSG_MAP END_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_ABOUTBO
9、X 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 dialog SetIcon(m_hIcon, TRUE);/ Set big icon SetIcon(m_hIcon, FALSE);/ Set sm
10、all icon/ TODO: Add extra initialization here m_IDC_BUTTON_STOP.EnableWindow(FALSE); return TRUE; / return TRUE unless you set the focus to a control void CChatRoomServerDlg:OnSysCommand(UINT nID, LPARAM lParam) if (nID dlgAbout.DoModal(); else CDialog:OnSysCommand(nID, lParam); / If you add a minim
11、ize 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_ICONER
12、ASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);/ Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect( int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2;/ Draw the icon dc.DrawIcon(x, y,
13、m_hIcon); else CDialog: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 her
14、e m_IDC_BUTTON_START.EnableWindow(FALSE);/使启动按钮无效 ListenSocket.Create(6767);/创建监听套接字端口为 6767 ListenSocket.Listen();/开始监听 m_IDC_BUTTON_STOP.EnableWindow(TRUE);/将停止按钮激活 void CChatRoomServerDlg:OnButtonStop() / TODO: Add your control notification handler code here m_IDC_BUTTON_STOP.EnableWindow(FALSE);
15、/使停止按钮无效 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_I NCLUDED_) #define AFX_CHATROOMSERVER_H_680EC642_E19B_4D55_88DF_2C9E9B1B30FD_INCLU