計(jì)算機(jī)網(wǎng)絡(luò)程序設(shè)計(jì)實(shí)驗(yàn)6_第1頁
計(jì)算機(jī)網(wǎng)絡(luò)程序設(shè)計(jì)實(shí)驗(yàn)6_第2頁
計(jì)算機(jī)網(wǎng)絡(luò)程序設(shè)計(jì)實(shí)驗(yàn)6_第3頁
計(jì)算機(jī)網(wǎng)絡(luò)程序設(shè)計(jì)實(shí)驗(yàn)6_第4頁
計(jì)算機(jī)網(wǎng)絡(luò)程序設(shè)計(jì)實(shí)驗(yàn)6_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、北京聯(lián)合大學(xué)信息學(xué)院網(wǎng)絡(luò)程序設(shè)計(jì)實(shí)驗(yàn)報(bào)告實(shí)驗(yàn)序號(hào):姓名:學(xué)號(hào):班級:專業(yè):實(shí)驗(yàn)六、基于mfc的udp協(xié)議的網(wǎng)絡(luò)程序調(diào)試服務(wù)器代碼void cudpsdlg:0nsockct(wparam wparam, lparam 1param)char cs 100=; /定義數(shù)據(jù)緩沖區(qū)if(l par am二二fd_read) /如果是套接字讀取時(shí)間cstring num二”;/定義字符串變量int n=sizeof(addrl);:recvfrom(s, cs, 100, 0, (sockaddr*)&addrl, &n) ; /接收客戶端信息 getdlgltem(idc_edit1

2、)->getwindowtext (num) ; /獲取消息框中的內(nèi)容 num+=z,rn客月端說:;添加冋車換行符 num+= (lptstr) cs; /將接收到的數(shù)據(jù)轉(zhuǎn)換為字符串getdlgltem(1dc_ed1t1)->setwindowtext(num) ; /設(shè)置消息框內(nèi)容 "bool cudpsdlg:ontnitdialog()cdialog:0nlnitdialog ();/ set the ico n for this dialog. the framework does this automatically/ when the applicatio

3、n's nieiin window is not a dialog seticon(m_hlcon, true);/ set big iconseticon(mhlcon, false);/ set small icon/ todo: add extra initialization hereword version = makeword(2, 0);wsadata wsadata;i f(wsastartup(version, &wsadata)messageboxc加載 winsock dll 失敗”);return true;addr.sinfamily=af_tnet;

4、addr sin_port二htons(8088);addr.sin addr.s un. s addr=lnaddr any;s=:socket (af_inet, sock_dgram, 0);:bind(s, (sockaddr*)&addr, sizeof(addr);:wsaasyncselect(s, thi s->m_hwnd, w_s0cket, fd_read) ;/將套接字設(shè)置為異步模式return true; / return true unless you set the focus to a control void cudpsdlg:0npaint()

5、if (isleonic()cpaintdc de (this); / device context for paintingsendmessage(wm_iconerasebkgnd, (wparam) de.getsafehde (), 0);/ center icon in client rectangleint cxlcon = getsystemmetrics(sm_cxic0n);int cylcon = getsystemmetrics(sm_cy1con);crect rect;getclientrect( srect);int x 二(rect. width() - cxtc

6、on + 1) / 2;int y = (rect iieight () - cylcon + 1) / 2;/ draw the iconde. drawlcon(x, y, m hlcon); "elsecdialog:0npaint();void cudpsdlg:0nbuttonl()/ todo: add your control notification handler code here cstring str二"“;int n=sizcof(addrl);getdlgltem(ldc_edlt2)->getwindowtext(str);if(:se

7、ndto(s, str. get buffer (1), str. getl en gth (), 0, (sockaddr* )& addrl, n)/ /發(fā)數(shù)據(jù)到客戶端getdlgitem(idc_edit2)->setwindowtext (); void cudpsdlg:0ndestroy() cdialog:0ndestroy ();/ todo: add your message hemdlcr code here closesocket (s);if (wsacleanupo) messageboxc卸載 winsock dll 失敗);rcturn;客戶端代碼v

8、oid cudpcdlg:0nsocket(wparam wparam, lparam 1param)char cs100二;/定義數(shù)據(jù)緩沖區(qū)if(lparam=fd_read) 如果是套接字讀取吋間cstring num二";/定義字符串變量int n=sizeof(addrl);:recvfrom(s, cs, 100, 0, (sockaddr*)&addrl, &n) ; /接收客戶端信息 getdlgitem(idc_editl)->getwindowtext(num) ; /獲取消息框中的內(nèi)容 num+=,rn服務(wù)器說:;添加回車換行符num+= (

9、lptstr) cs; /將接收到的數(shù)據(jù)轉(zhuǎn)換為字符串getdlgltem(idc editl) ->setwindowtext (num) ; /設(shè)置消息框內(nèi)容 "00l cudpcdlg:0ninitdialog()cdialog:0ninitdialog ();this/ set the ico n for this dialog. the framework does automatically/ when the application's main window is not a dialog seticon(m hlcon, true) ;/ set big

10、 iconsetlcon(m hlcon, false); / set small icon/ todo: add extra initializeition hereword version = makeword(2,0);wsadata wsadata;i f(wsastartup(version, &wsadata)messagebox c加載 winsock dll 失敗);return true;s=:socket (af_1net, sock_dgram, 0);:wsaasyncselect(s, this->m_hwnd, wm_s0cket, fd_read)

11、;/將套接字設(shè)置為 異步模式return true; / return true unless you set the focus to a control void cudpcdlg:0npaint()if (tstconico)cpaintdc de (this); / device context for paintingsendmessage(wm_iconerasebkgnd, (wparam) de.getsafehdc (), 0);/ center icon in client rectangleint cxlcon = getsystemmetrics(sm_cxic0n);

12、int cylcon = getsystemmetrics(sm_cy1con);crect rect;getclientrect( srect);int x 二(rect. width() - cxtcon + 1) / 2;int y = (rect iieight () - cylcon + 1) / 2;/ draw the iconde. drawlcon(x, y, m hlcon); "elsecdialog:0npaint();void cudpcdlg:0nbuttonl()/ todo: add your control notification handler

13、code herecstring str二“;cstring strl; /定義字符串 int port; /定義端口變量getdlgltem(ldc_edlt3) ->getwindowtext (str) ; /獲取服務(wù)器地址 getdlgltem(idc_edit4) ->getwindowtext (strl) ; /獲取端口號(hào) port=atoi (strl. getbuffer (1) ; /將端口字符串轉(zhuǎn)換為數(shù)字 addr.sinfamily=af_tnet;addr. sin addr. s un. s_addr=inet_addr(str.getbuffer(1);轉(zhuǎn)換服另器ip地址addr. sin_port=htons(port);int n=sizcof(addr);getdlgltem(ldc_edlt2)->getwindowtext(str);if(:se ndto(s, str. get buffer (1), str. getl en gth (), 0, (sockaddr* )& addr,

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論