版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、蘇州大學(xué)實驗報告院、系文正學(xué)院年級專業(yè)12物理網(wǎng)姓名高成波學(xué)號1217443036課程名稱網(wǎng)絡(luò)程序設(shè)計成績指導(dǎo)教師陸建德同組實驗者無實驗日期第17頁,共17頁教務(wù)處制實 驗 名 稱實驗四 基于mfc casyncsocket類的網(wǎng)絡(luò)通信一. 實驗內(nèi)容(要求先完成題目,然后上機驗證)本實驗利用visual c+ mfc casyncsocket類進行網(wǎng)絡(luò)程序設(shè)計。參照實驗4講義示例,完成基于casyncsocket類的點到點網(wǎng)絡(luò)聊天通信程序,并上機調(diào)試。要求可以實現(xiàn)本機間以及在同一網(wǎng)絡(luò)內(nèi)不同主機之間的通信。實驗報告內(nèi)容包括主要的實驗代碼、必要的注釋或另外的說明文檔以及實驗結(jié)果與分析。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(連接已經(jīng)斷開!);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); /將對應(yīng)變量中字符串送控件中顯示void cchatseverdlg:send()updatedata(true); /將控件中顯示的字符串送到對應(yīng)的變量中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); /將對應(yīng)變量中
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年單位集體用餐協(xié)議模板解析
- 2024年機票代理購買協(xié)議范本
- 2024防火安全門供應(yīng)安裝協(xié)議
- 2024年建筑項目保險協(xié)議范例全書
- DB11∕T 1725-2020 蔬菜病蟲害全程綠色防控技術(shù)規(guī)程
- 2024年上海勞務(wù)派遣協(xié)議格式
- 2024年度牛肉購銷協(xié)議范本
- 2024年汽車托管租賃模板協(xié)議
- 2024年道路施工合作協(xié)議范本
- 文書模板-《住房換瓦協(xié)議書》
- 2020-2021學(xué)年河南省洛陽市高一上學(xué)期期中考試化學(xué)試題
- 肥胖癥診療指南(2024年版)
- 《高血壓科普知識》課件
- 行政復(fù)議法-形考作業(yè)3-國開(ZJ)-參考資料
- 江蘇省蘇州市2023-2024學(xué)年七年級上學(xué)期期中陽光測評英語試題
- 【招標(biāo)控制價編制研究文獻綜述(論文)4800字】
- GB/T 5237.1-2017鋁合金建筑型材第1部分:基材
- EXCEL 支票打印模板
- ISO9000質(zhì)量管理體系培訓(xùn)資料課件
- 城鎮(zhèn)污水處理廠污泥日轉(zhuǎn)運聯(lián)單
- 中藥知識文庫:藥材資料匯編(上集)
評論
0/150
提交評論