网络程序设计实验4基于MFCCAsyncSocket类网络通信_第1页
网络程序设计实验4基于MFCCAsyncSocket类网络通信_第2页
网络程序设计实验4基于MFCCAsyncSocket类网络通信_第3页
网络程序设计实验4基于MFCCAsyncSocket类网络通信_第4页
网络程序设计实验4基于MFCCAsyncSocket类网络通信_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、苏州大学实验报告院、系文正学院年级专业12物理网姓名高成波学号36课程名称网络程序设计成绩指导教师陆建德同组实验者无实验日期16 / 16文档可自由编辑打印实 验 名 称实验四 基于MFC CAsyncSocket类的网络通信一. 实验内容(要求先完成题目,然后上机验证)本实验利用Visual C+ MFC CAsyncSocket类进行网络程序设计。参照实验4讲义示例,完成基于CAsyncSocket类的点到点网络聊天通信程序,并上机调试。要求可以实现本机间以及在同一网络内不同主机之间的通信。实验报告内容包括主要的实验代码、必要的注释或另外的说明文档以及实验结果与分析。ChatClientD

2、lg.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_DAT

3、A(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:CAboutDl

4、g() : 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()/ CChatCl

5、ientDlg 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 = AfxGet

6、App()->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_

7、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

8、 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 (pSysMen

9、u != 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 application's main

10、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);GetDl

11、gItem(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 but

12、ton 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, (WPA

13、RAM) 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);e

14、lseCDialog: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 ClassWi

15、zard.#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 = (CChat

16、ClientDlg *) :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:O

17、nSend(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

18、) / 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_

19、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_

20、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)->

21、;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(IDCA

22、NCEL)->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_B

23、TN_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"#i

24、nclude "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 ;/A

25、FX_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_I

26、NIT(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:CChatSeverDl

27、g(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(CDataExch

28、ange* 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_

29、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 "Ab

30、out." 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 (!strAbo

31、utMenu.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 application's main window is not a dialogSetIcon(m_hIcon, TRUE);/ Set big iconSetIcon(m_hIcon, FALS

32、E);/ 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)->Enabl

33、eWindow(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 y

34、our 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.G

35、etSafeHdc(), 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);elseCDialo

36、g: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 0

37、BEGIN_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*)

38、 :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(nEr

39、rorCode);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

40、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_w

41、orksocket, (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(T

42、RUE);void CChatSeverDlg: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); UpdateData(FALSE); /将对应变量中字符串送控件中显示void CChatSeverDlg:send()UpdateData(TRUE); /将控件中显示的字符串送到对应的变量中if(!m_edit_msg.IsEmpty()CString msg;CString from = "自己:"int len = m_edit_msg.GetLength();m_worksocket.Send(m_edit_msg, len);msg = from + m_edit_msg;m_list_msg.InsertString(0, msg);m_edit_msg.Empty();Upd

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论