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

上传人:文库蛋蛋多 文档编号:2385791 上传时间:2023-02-17 格式:DOC 页数:16 大小:59KB
返回 下载 相关 举报
《网络程序设计》实验4基于MFCCAsyncSocket类的网络通信.doc_第1页
第1页 / 共16页
《网络程序设计》实验4基于MFCCAsyncSocket类的网络通信.doc_第2页
第2页 / 共16页
《网络程序设计》实验4基于MFCCAsyncSocket类的网络通信.doc_第3页
第3页 / 共16页
《网络程序设计》实验4基于MFCCAsyncSocket类的网络通信.doc_第4页
第4页 / 共16页
《网络程序设计》实验4基于MFCCAsyncSocket类的网络通信.doc_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《《网络程序设计》实验4基于MFCCAsyncSocket类的网络通信.doc》由会员分享,可在线阅读,更多相关《《网络程序设计》实验4基于MFCCAsyncSocket类的网络通信.doc(16页珍藏版)》请在三一办公上搜索。

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

2、nclude 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_DA

3、TA/ 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(C

4、AboutDlg)/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(

5、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(CDa

6、taExchange* 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

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

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

9、ly/ 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)-Enable

10、Window(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 m

11、inimize 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_ICONERASE

12、BKGND, (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_h

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

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

15、 (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:On

16、Send(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) / T

17、ODO: 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(b

18、uf, 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

19、);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);voi

20、d 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 notifi

21、cation 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() / TO

22、DO: 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

23、 = _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

24、); / 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_DA

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

26、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);/AF

27、X_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_SE

28、ND, 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 AppendMenu(

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

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

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

32、cations 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 = GetSystemMe

33、trics(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

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

35、END_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 CMySo

36、cket: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 h

37、ere 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_

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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号