欢迎来到三一文库! | 帮助中心 三一文库31doc.com 一个上传文档投稿赚钱的网站
三一文库
全部分类
  • 研究报告>
  • 工作总结>
  • 合同范本>
  • 心得体会>
  • 工作报告>
  • 党团相关>
  • 幼儿/小学教育>
  • 高等教育>
  • 经济/贸易/财会>
  • 建筑/环境>
  • 金融/证券>
  • 医学/心理学>
  • ImageVerifierCode 换一换
    首页 三一文库 > 资源分类 > DOC文档下载  

    《网络程序设计》实验4基于MFCCAsyncSocket类的网络通信.doc

    • 资源ID:10312662       资源大小:59KB        全文页数:16页
    • 资源格式: DOC        下载积分:4
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录 QQ登录   微博登录  
    二维码
    微信扫一扫登录
    下载资源需要4
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    《网络程序设计》实验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

    注意事项

    本文(《网络程序设计》实验4基于MFCCAsyncSocket类的网络通信.doc)为本站会员(土8路)主动上传,三一文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一文库(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    经营许可证编号:宁ICP备18001539号-1

    三一文库
    收起
    展开