MFC初學(xué)實例(客戶端服務(wù)器消息接收)_第1頁
MFC初學(xué)實例(客戶端服務(wù)器消息接收)_第2頁
MFC初學(xué)實例(客戶端服務(wù)器消息接收)_第3頁
MFC初學(xué)實例(客戶端服務(wù)器消息接收)_第4頁
MFC初學(xué)實例(客戶端服務(wù)器消息接收)_第5頁
已閱讀5頁,還剩39頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、新建WClient工程ApplicationWhattypeofapplicationwouldyouliketocreate?SingledocumentMultipledocumentsDialogbased!疋Document/Viewarchitecturesupport?Whatlanguagewouldyoulikeyourresourcesin?|中文中國HAPPWZCHS.DLL3OK,直接Finish界面制作我們需要三個按鈕otodo:在這里設(shè)置對話控制.二口白接下來添加六個靜態(tài)文本三個編輯框兩個列表控件,最后我們有:接下來設(shè)置控件文本,控件ID,設(shè)置成員變量,排布界面XXX

2、X消息:服務(wù)器名稱:發(fā)送的消息服務(wù)器名稱:消息:本居ITablen設(shè)置gTClientECenterverticallyNoprefixNowrapNotifySunktBordeSimpleRight服務(wù)稱:Aligntext:橫排變豎排:TextPropertiesGeneralStylesExtendedStyles|服務(wù)器名i消Aligntext:|LeftCenterverticallyNoprefixNowrapSimple拉長兩個靜態(tài)文本控件接收的消息Centervertically反選中。接收的消息發(fā)送的消息調(diào)整兩個ListBox同樣大小MakeSameSize發(fā)送的消息接收的

3、消息空間ID設(shè)置:斷開發(fā)送RVisibleDisabledExtendedStylesTCaption:|斷開和tiil詼器端口:|Edit連接1|EditEd.itProperties_一岡占昭GeneralIStyles|ExtendedStyles|ID:IDC_EDIT_W_ADDR3|7VisibleGroup廠HelpIDDisabled|7TabstopListBozProperties兇Ed.11Properties蟲霍GeneralStylesExtendedStyles|ID:IDCEDITWjORTJpVisibleGroup旦亡IpIDDisabledRTabstop連

4、接發(fā)送各器名稱:Edit服務(wù)器喘口:&阿ListBozProperties兇血頰GeneralIStyles|ExtendedStyles|ID:fC_LIST_W_MSGREVIEVEDjJ破VisibleGroupHelpIDDisabled眉Tabstop成員變量設(shè)置:你可以使用Ctrl+W快捷鍵跳出該窗口cwc區(qū)IDCBUTTONWDISCONNIDCBUTTONWSENDAddIeberVariableProject:Description:Membervariablename:Description:maptoCButtonmemberControlIDs:Description:

5、點OKProject:CButton|mctlWDisConnCategory:Variabletype:IDC_EDIT_W_ADDRIDC_EDIT_W_MSGTOSENDIDC_EDIT_W_PORTidClistAvJmsgrevievedIDCLISTWMSGSENDEDMembervariablename:|lDCBUTTOMWCONhiIDC_BUTTON_W_SENDIDC_EDIT_W_ADDRidCediddrIDC_EDIT_W_ADDRIDCEDITWMSGTOSENDIDCEDITWPORT|muWPortCategory:Variabletype:UINTIDC_L

6、IST_W_MSGREVIEVEDIDCLISTWMSGSENDEDDescription:UINTwithrangevalidationDescription:maptoCEditmember點OK點OKProject:Membervariabename:ControDs:DCLSTWMSGSENDFDDescription:Description:DCBUTTONWCONNDCBUTTONWDSCONNDCBUTTONWSENDDCEDTWADDRDCEDTWADDRDCEDTWMSGTOSENDDCEDTWPORTDCEDTWPORTm_ctlWMsgRecieved|ControlJV

7、ariabletype:|CListBoxJCategory:maptoCListBoxmember點OKTJTJ在WClient中:Ctrl+W-AddClass-NewClass在WClient中:Ctrl+W-AddClass-NewClassWServer斷開I發(fā)送的消息接收的消息服務(wù)器TJTJ在WClient中:Ctrl+W-AddClass-NewClass在WClient中:Ctrl+W-AddClass-NewClassTJTJ在WClient中:Ctrl+W-AddClass-NewClass在WClient中:Ctrl+W-AddClass-NewClass現(xiàn)在所有界面準備

8、完畢,接下來為兩個程序添加對應(yīng)的繼承CSocket類的類在WClient中:Ctrl+W-AddClass-NewClass在WClient中:Ctrl+W-AddClass-NewClassWCIientSocket.cpp蜩worKspacewciieni;iproj-SpWClientfiles-_jSourceFiles再StdAfx.cpp再WCIient.cpp再WCIient.rc再WCIientDIg.cppl-_JHeaderFiles罵Resource.h罵StdAfx.h罵WCIient.h3WCIientDIg.h+_|ResourceFiles弐ReadMe.txt5

9、Clas.勰1Res.File.f_jSourceFiles再StdAfx.cpp再WServer.cpp再WServer.rc再WServerDIg.cpp_jHeaderFiles司Resource.h罵StdAfx.h司W(wǎng)Server.h“JWServerDIg.hIResourceFiles豈ReadMe.txt接下來為兩個類添加虛函數(shù)CWClientSocket調(diào)出右鍵菜單CWClientSocket二AllclassmEmbers|CWCIiEntEGoToFunctionDefinitionFilesGoTuFutlctionHecl:=Lt_:=LtionAddWindowsM

10、essageHandler.WorkspaceWCIiei5PWCIientfilesStdAfx.cppWCIient.rcWCIientDIWCIientSFiles_gResource.罵StdAfx.hWCIient.hAddVirtualFineti口:n.AddMemberFunction.IleleteGoTciClassDeinitionNewClass.NewFm.卅迂:遼迪:遼HelpII-IOnAcceeVirtualOverrideOnCloseOnConnectOnMessagePendingOnOutOfBandDataOnReceiveOnSendReceiveS

11、endOnAcceptQ:CalledtonotifyasocketthatitmaycallAcceptAddHandler,我們將所有的都添加進去。雖然不是所有的都用的上,但有總比沒有好CWServerSocketCWServerSocket二|roje(Globals)CAboutDIgCWServerAppCWServErDIgCWServerSocketStdAfx.cpp-宙WServer.cppAlluIbesmEnib亡rs|CWSrv亡GoToFunctionDefinitionGoTuFijilc+ionIiecl:=Lt_a+ionAddWiruloweMeeeageH:

12、=lTlidler.AddVirtualFijilctiom.AddMemberFunction.DhIhrkspaceWServer:1pServerfiles_jSourceFiles再StdAfx.cppGoToClassDefinition.NewClass.NewFo.rm.Wiz.:=Lt-liE:=q-Help:再WServer.cpp再WServer.rc酉WServerDIg.cppWServerSocket.ci_jHeaderFiles弐Resource.h罵StdAfx.hW1WServer.h接下來我們可以看看CPPH文件中的變化頭文件(.h)/Ouerridespu

13、blic:/ClassUizardgenerateduirtualFunctionouerrides/1000ttpragmaoncettendiF/_MSC_UER1000ttincludeWSeruerDlg.h/CWSeruerDlgdialogclassCWSeruerDlg:publicCDialog/Constructionpublic:在CServerDlg.h中包含我們定義的CWServerSocket對象的變量用于監(jiān)聽端口,以及與客戶端(一個)進行通信添加CWServerSocket.h頭文件#iF_MSC_UER1000ttpragmaoncettendiF/_MSC_UE

14、R1000ttincludeWSeruerDlg.httincludeWSeruerSocket.h/./CWSeruerDlgdialogclassCWSeruerDlg:publicCDialog/Construction定義對象protected:HICONm_hlcon;CWSeruerSocketSocket,uConnSocket;CSocketFile*wSocketFile;Cfirchiue*uArdn;CArchiue*uArcOut;/GeneratedmessagemapFunctions/str;iF(?str.IsEmptO)m_ctlWMsgRecieued.fi

15、ddString(str);UpdateData(FALSE);while(?wArcIn-IsBuFFerEmpty();接受到信息時uoidCUSeruerDlg:0nClose()iF(wArcIn?=NULL)wArcIn-Close();wArcOut-Close();wSocketFile-Close()deleteuiArcIn;deleteuiArcOut;deletewSocketFile;SetButtons(TRUE);套接字被關(guān)閉時雙擊界面按鈕,添加按鈕響應(yīng)事件在WServerDlg.h中增加SetButtons函數(shù)定義classCWSeruerDlg:publicCD

16、ialog/Constructionpublic:CWSeruerDlg(CWnd*pParent=NULL);uoidOnAccept();uoidOnRecieue();uoidOnClose();uoidSetButtons(BOOL)uoidCWSeruerDlg:OnButtonUConn()/TODO:Addyourcontrolnotification發(fā)送的消息uoidCWSeruerDlg:OnButtonUDisconn()/TODO:AddyourcontrolnotificationhandlercodehereuoidCWSeruerDlg:OnButtonUSend(

17、)/TODO:Addyourcontrolnotificationhandlercodehere添加,代碼uoidCWSeruerDlg:OnButtonUConn()/TODO:Addi/ourcontrolnotificationhandlercodehereUpdateData(TRUE);wSocket.Create(m_uWPort);wSocket.SetParentDig(this);if(wSocket.Listen()m_ctlWPort.EnableWindow(FALSE);mctlWConn.EnableWindow(FALSE);mctlWDisConn.Enable

18、Window(FALSE);mlctlWSend.EnableWindow(FALSE);elseAFxMessageBox(監(jiān)聽失敗?”);uoidCWSeruerDlg:OnButtonUDisconn()/TODO:AddpourcontrolnotificationhandlercodehereuSocket.Close();SetButtons(TRUE);uoidCWSeruerDlg:OnButtonUSend()/TODO:AddiourcontrolnotificationhandlercodehereUpdateData(TRUE);(*wfircOut)m_strWMsg

19、ToSend;m_ctlWMsgSended.Addstring(m_strWMsgToSend);(*wArcOut).Flush();UpdateData(FALSE);uoidCUSeruerDlg:SetButtons(B00Lvalue)(fualue);m_ctlUSend.EnableUindou(fualue);UpdateData(FALSE);SetButtons函數(shù)為控制界面按鈕聯(lián)動。設(shè)置個控件顯隱狀態(tài)在WServerSocket.cpp中添加ttincludestdaFx.httincludeWSeruer.httincludeWSeruerSocket.httincl

20、udeWSeruerDlg.httiFdeF_DEBUG#deFine_newDEBUG_NEWttundeFTHISFILE-staticchar_THIS_FILE=_FILE_;ttendiF/.uoidCWSeruerSocket:OnAccept(intnErrorCode)/TODO:Addyourspecializedcodehereand/orcallthebaseclassCSocket:Onficcept(nErrorCode);(CWSeruerDlg*)pDlg)-Onficcept();uoidCWSeruerSocket:OnClose(intnErrorCode)

21、/TODO:Addyourspecializedcodehereand/orcallCSocket:0nClose(nErrorCode);(CWSeruerDlg*)pDlg)-OnClose();uoidCWSeruerSocket:OnReceiue(intnErrorCode)/TODO:Addpourspecializedcodehereand/orcallthebaseclassCSocket:OnReceiue(nErrorCode);(CWSeruerDlg*)pDlg)-OnRecieue();uoidCWSeruerSocket:SetParentDlg(CDialog*p

22、)CWSeruerSocket:pDlg=p;在CClientDlg.h中添加頭文件引用ttdeFineflFX_WCLIENTDLG_H_7BB49491_1Uf_MSC_UER1000ttpragmaoncettendiF/MSCUER1000ttincludeWClientSocket.h1/CUClientDlgdialogclassCUClientDlg:publicCDialog定義變量/Implementationprotected:HICONm_hlcon;CWClientSocketuSocket;CSocketFile*wSocketFile;CArchiue*uArdn;

23、CArchiue*uiArcOut;/GeneratedmessagemapFunctions/AFK_MSG(CWClientDlg)uirtualBOOLOnInitDialog();aFxmsquoidOnSusCommandfUINTnID,定義函數(shù)classCUClientDlg:publicCDialog/Constructionpublic:CWCliEntDlgeWnd#pPmfent:=NULL);/standardconsuoidOnRecieue(uoid0nClose();uoidSetButtons(BOOL)/DialogData/str;iF(?str.IsEmp

24、tO)m_ctlWMsgRecieued.AddString(str);UpdateData(FALSE);while(?wArcIn-IsBuFFerEmpty();uoidCUClientDlg:0nClose()iF(wSocketFile?=NULL)wArcIn-Close();wArcOut-Close();wSocketFile-Close()deleteuArdn;deleteuArcOut;deletewSocketFile;SetButtons(TRUE);uoidCUClientDlg:OnButtonUConn()/TODO:Addiourcontrolnotifica

25、tionhandlercodehereUpdateData(TRUE);wSocket.Create();wSocket.SetParentDlg(this);iF(wSocket.Connect(m_strWAddr,m_uWPort)wSocketFile=newCSocketFile(&wSocket);wfircln=newCArchiue(wSocketFile,CArchiue:load);uArcOut=neuCArchiue(wSocketFile,CArchiue:store);SetButtons(FALSE);mctlWMsgRecieued.AddStringC客戶機接

26、收到了OnConnect消息)UpdateData(FALSE);elseDUORDd=wSocket.GetLastError();uiSocket.Close();AFxMessageBoxC接失?。俊?;uoidCUClientDlg:OnButtonUDisconn()/TODO:AddyourcontrolnotificationhandlercodehereuSocket.Close();SetButtons(TRUE);uoidCUClientDlg:OnButtonUSend()/TODO:AddpourcontrolnotificationhandlercodehereUpd

27、ateData(TRUE);(*wfircOut)m_strWMsgToSend;m_ctlWMsgSended.flddString(m_strWMsgToSend);(*wArcOut).Flush();UpdateData(FALSE);uoidCUClientDlg:SetButtons(B00Lvalue)1000/WClientSocketl.h:headerFilettinclude/CWClientSocketcommandtargetclassCWClientSocket:publicCSocket引入頭文件定義函數(shù)public:/Operationspublic:CWClientSocket();uirtualCWClientSocket();voidSEtP日ntDlgeDi日log*);/Ouerridespublic:/ClassUizardgenerateduirtualFunctio/AFXUIRTUAL(CWClientSocket)定義變量/Implementationprotected:CDialog*pDlg;/在CCLientSocket.cpp中引入頭文件ttincludestdaFx.httincludeWClient.httincludeWClientSocket.h1

溫馨提示

  • 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論