《网络程序设计》实验4基于MFCCAsyncSocket类的网络通信.doc
苏州大学实验报告院、系文正学院年级专业12物理网姓名高成波学号1217443036课程名称网络程序设计成绩指导教师陆建德同组实验者无实验日期实 验 名 称实验四 基于MFC CAsyncSocket类的网络通信一. 实验内容(要求先完成题目,然后上机验证)本实验利用Visual C+ MFC CAsyncSocket类进行网络程序设计。参照实验4讲义示例,完成基于CAsyncSocket类的点到点网络聊天通信程序,并上机调试。要求可以实现本机间以及在同一网络内不同主机之间的通信。实验报告内容包括主要的实验代码、必要的注释或另外的说明文档以及实验结果与分析。ChatClientDlg.cpp#include "stdafx.h"#include "ChatClient.h"#include "ChatClientDlg.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()/ CChatClientDlg dialogCChatClientDlg:CChatClientDlg(CWnd* pParent /*=NULL*/): CDialog(CChatClientDlg:IDD, pParent)/AFX_DATA_INIT(CChatClientDlg)m_edit_server = _T("");m_edit_msg = _T("");/AFX_DATA_INIT/ Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);void CChatClientDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CChatClientDlg)DDX_Control(pDX, IDC_LIST_MSG, m_list_msg);DDX_Text(pDX, IDC_EDIT_SEVER, m_edit_server);DDX_Text(pDX, IDC_EDIT_MSG, m_edit_msg);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CChatClientDlg, CDialog)/AFX_MSG_MAP(CChatClientDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BTN_CONNECT, OnBtnConnect)ON_BN_CLICKED(IDC_BTN_DISCONNECT, OnBtnDisconnect)ON_BN_CLICKED(IDC_BTN_MSG_SEND, OnBtnMsgSend)/AFX_MSG_MAPEND_MESSAGE_MAP()/ CChatClientDlg message handlersBOOL CChatClientDlg: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 < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL)CString strAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if (!strAboutMenu.IsEmpty()pSysMenu->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 hereGetDlgItem(IDC_BTN_CONNECT)->EnableWindow(TRUE);GetDlgItem(IDC_BTN_DISCONNECT)->EnableWindow(FALSE);GetDlgItem(IDC_BTN_MSG_SEND)->EnableWindow(FALSE);GetDlgItem(IDCANCEL)->EnableWindow(TRUE);return TRUE; / return TRUE unless you set the focus to a controlvoid CChatClientDlg: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 CChatClientDlg: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 CChatClientDlg:OnQueryDragIcon()return (HCURSOR) m_hIcon;/ CMySocketCMySocket:CMySocket()CMySocket:CMySocket()/ Do not edit the following lines, which are needed by ClassWizard.#if 0BEGIN_MESSAGE_MAP(CMySocket, CAsyncSocket)/AFX_MSG_MAP(CMySocket)/AFX_MSG_MAPEND_MESSAGE_MAP()#endif/ 0/ CMySocket member functionsvoid CMySocket:OnClose(int nErrorCode) / TODO: Add your specialized code here and/or call the base classif(nErrorCode = 0)CChatClientDlg * p_Dlg;p_Dlg = (CChatClientDlg *) :AfxGetMainWnd();p_Dlg->close();CAsyncSocket:OnClose(nErrorCode);void CMySocket:OnSend(int nErrorCode) / TODO: Add your specialized code here and/or call the base classif(nErrorCode = 0)CChatClientDlg * p_Dlg;p_Dlg = (CChatClientDlg *) :AfxGetMainWnd();p_Dlg->send();CAsyncSocket:OnSend(nErrorCode);void CMySocket:OnReceive(int nErrorCode) / TODO: Add your specialized code here and/or call the base classif(nErrorCode = 0)CChatClientDlg * p_Dlg;p_Dlg = (CChatClientDlg *) :AfxGetMainWnd();p_Dlg->recv();CAsyncSocket:OnReceive(nErrorCode);void CMySocket:OnConnect(int nErrorCode) / TODO: Add your specialized code here and/or call the base classif(nErrorCode = 0)CChatClientDlg * p_Dlg;p_Dlg = (CChatClientDlg *) :AfxGetMainWnd();p_Dlg->connect();CAsyncSocket:OnConnect(nErrorCode);void CChatClientDlg:recv()char buf1024;CString msg;CString from = "对方:"int len = m_worksocket.Receive(buf, 1024);buflen = 0;msg = buf;msg = from + msg;m_list_msg.InsertString(0, msg);void CChatClientDlg:send()UpdateData(true);if(!m_edit_msg.IsEmpty()CString msg;CString from = "自己:"msg = from + m_edit_msg;int len = m_edit_msg.GetLength();m_worksocket.Send(m_edit_msg, len);m_list_msg.InsertString(0, msg);m_edit_msg.Empty();UpdateData(FALSE);void CChatClientDlg:close()m_worksocket.Close();AfxMessageBox("连接已经断开!");GetDlgItem(IDC_BTN_CONNECT)->EnableWindow(TRUE);GetDlgItem(IDC_BTN_DISCONNECT)->EnableWindow(FALSE);GetDlgItem(IDC_BTN_MSG_SEND)->EnableWindow(FALSE);GetDlgItem(IDCANCEL)->EnableWindow(TRUE);void CChatClientDlg:connect()AfxMessageBox("连接成功!");GetDlgItem(IDC_BTN_MSG_SEND)->EnableWindow(TRUE);GetDlgItem(IDC_BTN_DISCONNECT)->EnableWindow(TRUE);GetDlgItem(IDC_BTN_CONNECT)->EnableWindow(FALSE);GetDlgItem(IDCANCEL)->EnableWindow(FALSE);void CChatClientDlg:OnBtnConnect() / TODO: Add your control notification handler code here/ TODO: Add your control notification handler code hereUpdateData(TRUE);if(!m_edit_server.IsEmpty()m_worksocket.Create();m_worksocket.Connect(m_edit_server, iPort);GetDlgItem(IDC_BTN_CONNECT)->EnableWindow(FALSE);m_edit_server.Empty();void CChatClientDlg:OnBtnDisconnect() / TODO: Add your control notification handler code hereclose();void CChatClientDlg:OnBtnMsgSend() / TODO: Add your control notification handler code heresend();ChatSever.cpp#include "stdafx.h"#include "ChatSever.h"#include "ChatSeverDlg.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()/ CChatSeverDlg dialogCChatSeverDlg:CChatSeverDlg(CWnd* pParent /*=NULL*/): CDialog(CChatSeverDlg:IDD, pParent)/AFX_DATA_INIT(CChatSeverDlg)m_edit_msg = _T("");/AFX_DATA_INIT/ Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);void CChatSeverDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CChatSeverDlg)DDX_Control(pDX, IDC_LIST_MSG, m_list_msg);DDX_Text(pDX, IDC_EDIT_MSG, m_edit_msg);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CChatSeverDlg, CDialog)/AFX_MSG_MAP(CChatSeverDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BTN_SER_OPEN, OnBtnSerOpen)ON_BN_CLICKED(IDC_BTN_SER_CLOSE, OnBtnSerClose)ON_BN_CLICKED(IDC_BTN_DISCONNECT, OnBtnDisconnect)ON_BN_CLICKED(IDC_BTN_MSG_SEND, OnBtnMsgSend)/AFX_MSG_MAPEND_MESSAGE_MAP()/ CChatSeverDlg message handlersBOOL CChatSeverDlg: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 < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL)CString strAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if (!strAboutMenu.IsEmpty()pSysMenu->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 hereGetDlgItem(IDC_BTN_SER_OPEN)->EnableWindow(TRUE);GetDlgItem(IDC_BTN_SER_CLOSE)->EnableWindow(FALSE);GetDlgItem(IDC_BTN_DISCONNECT)->EnableWindow(FALSE);GetDlgItem(IDC_BTN_MSG_SEND)->EnableWindow(FALSE);GetDlgItem(IDCANCEL)->EnableWindow(TRUE);UpdateData(FALSE);return TRUE; / return TRUE unless you set the focus to a controlvoid CChatSeverDlg: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 CChatSeverDlg: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 CChatSeverDlg:OnQueryDragIcon()return (HCURSOR) m_hIcon;/ CMySocketCMySocket:CMySocket()CMySocket:CMySocket()/ Do not edit the following lines, which are needed by ClassWizard.#if 0BEGIN_MESSAGE_MAP(CMySocket, CAsyncSocket)/AFX_MSG_MAP(CMySocket)/AFX_MSG_MAPEND_MESSAGE_MAP()#endif/ 0/ CMySocket member functionsvoid CMySocket:OnAccept(int nErrorCode) / TODO: Add your specialized code here and/or call the base classif(nErrorCode = 0)CChatSeverDlg * p_Dlg;p_Dlg = (CChatSeverDlg*) :AfxGetMainWnd();p_Dlg->accept();CAsyncSocket:OnAccept(nErrorCode);void CMySocket:OnClose(int nErrorCode) / TODO: Add your specialized code here and/or call the base classif(nErrorCode = 0)CChatSeverDlg * p_Dlg;p_Dlg = (CChatSeverDlg *) :AfxGetMainWnd();p_Dlg->close();CAsyncSocket:OnClose(nErrorCode);void CMySocket:OnReceive(int nErrorCode) / TODO: Add your specialized code here and/or call the base classif(nErrorCode = 0)CChatSeverDlg * p_Dlg;p_Dlg = (CChatSeverDlg *) :AfxGetMainWnd();p_Dlg->recv();CAsyncSocket:OnReceive(nErrorCode);void CMySocket:OnSend(int nErrorCode) / TODO: Add your specialized code here and/or call the base classif(nErrorCode = 0)CChatSeverDlg * p_Dlg;p_Dlg = (CChatSeverDlg *) :AfxGetMainWnd();p_Dlg->send();CAsyncSocket:OnSend(nErrorCode);void CChatSeverDlg:accept()struct sockaddr_in cli;int len = sizeof(cli);CString strIpAddr;m_listensocket.Accept(m_worksocket, (SOCKADDR*)&cli, &len);strIpAddr = inet_ntoa(cli.sin_addr);strIpAddr = "新的连接:" + strIpAddr;AfxMessageBox(strIpAddr);GetDlgItem(IDC_BTN_SER_CLOSE)->EnableWindow(FALSE);GetDlgItem(IDC_BTN_MSG_SEND)->EnableWindow(TRUE);GetDlgItem(IDC_BTN_DISCONNECT)->EnableWindow(TRUE);void CChatSeverDlg:recv()char buf1024;CString msg;CString from = "对方:"int len