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

下载本文档

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

文档简介

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

2、ientdlg.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_abou

3、tbox ;/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)/af

4、x_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:cc

5、hatclientdlg(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:dodat

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

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

8、alog();/ 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(mf_separator);psysmenu-appendmenu(mf_string, idm_aboutbox, straboutmenu);/ set the icon for this dialog. the framework does thi

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

10、_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);/ i

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

12、(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.drawi

13、con(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 a

14、re 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 *

15、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();ca

16、syncsocket: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 ne

17、rrorcode) / 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_worksoc

18、ket.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.insert

19、string(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)-enablewin

20、dow(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 c

21、ontrol 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:onbtndisc

22、onnect() / 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 c

23、har 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(cdata

24、exchange* 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(cabo

25、utdlg)/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_ini

26、t/ 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_

27、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(i

28、dc_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_aboutbo

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

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

31、ue 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.

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

33、= 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 disp

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

35、)/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(nerrorcod

36、e);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 speci

37、alized 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)cchat

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

39、= 新的连接: + 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 = m_worksocket.receive(buf, 10

40、24);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();updatedata(false); /将对应变量中

温馨提示

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

评论

0/150

提交评论