




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 = 新的连接: + str
39、ipaddr;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, 1024);buflen =
40、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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 斧凿混凝土外墙施工方案
- 台山屋顶清洗施工方案
- 水库高温施工方案
- 危险性专项施工方案
- 漯河管井降水施工方案
- TSHJMRH 0063-2024 在用润滑油颗粒污染度的测定 光阻法
- 家长会安全发言稿
- 2025年度股份转让过程中税务筹划及优惠政策合同
- 二零二五年度关于协议作废纠纷的调解与赔偿协议
- 二零二五年度夫妻共同维护家庭和谐与子女幸福感协议书
- 《信息技术基础》高职全套教学课件
- GB/T 19077-2024粒度分析激光衍射法
- 露天矿山开采施工组织方案
- 北京市西城区2022-2023学年高三上学期1月期末考试历史试题 附答案
- 2024关于进一步提升基层应急管理能力的意见学习解读课件
- 幼儿园小班健康公开课《笑一笑》课件
- 单词连连看答题闯关游戏课堂互动课件1
- 16S524塑料排水检查井-井筒直径Φ700~Φ1000
- 加强文物古籍保护利用(2022年广东广州中考语文试卷非连续性文本阅读试题及答案)
- 2024小学数学义务教育新课程标准(2022版)必考题库附含答案
- 2024年上半年教师资格证《高中物理》真题及答案
评论
0/150
提交评论