乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      【玩轉(zhuǎn)cocos2d-x之三十一】服務(wù)器的網(wǎng)絡(luò)通信編程

       Kitsdk 2014-02-24

      原創(chuàng)作品,轉(zhuǎn)載請(qǐng)標(biāo)明http://blog.csdn.net/jackystudio/article/details/17347069


      這里采用Apache+php搭建了一個(gè)簡(jiǎn)易服務(wù)器,服務(wù)端用php語(yǔ)言,客戶端采用cocos2d-x的CCHttpClient類(lèi)通過(guò)http方式訪問(wèn)服務(wù)端資源。模擬了cocos2d-x提交賬戶和密碼到服務(wù)端,服務(wù)端校驗(yàn)賬號(hào)密碼,如果正確返回客戶端成功登錄,如果錯(cuò)誤則返回錯(cuò)誤信息,同時(shí)在服務(wù)端后臺(tái)保存登錄log。第一次接觸php,語(yǔ)法上和C/C++還是蠻像的,主要是給出一個(gè)cocos2d-x網(wǎng)絡(luò)實(shí)例,代碼中并沒(méi)有做一些防呆糾錯(cuò)措施。


      1.搭建Apache+php網(wǎng)頁(yè)服務(wù)器

      Apche2.2 x86版下載地址:http://pan.baidu.com/s/1vNuLF

      php5.2.17版下載地址:http://pan.baidu.com/s/17sFoN

      搭建過(guò)程參見(jiàn)http://tech.163.com/06/0206/11/299AMBLT0009159K.html,這里就不安裝MySQL了。

      搭建成功后,打開(kāi)http://127.0.0.1,就可以看到"It' works!"字樣。同時(shí)打開(kāi)Apache monitor監(jiān)控Apache處于運(yùn)行狀態(tài)。我這里使用的80端口。


      2.php收集表單的方式

      Http定義了與服務(wù)器交互的不同方法,最基本的方法有4種,分別是GET,POST,PUT,DELETE,對(duì)應(yīng)著查改增刪,這里介紹GET和POST。

      用$_GET獲取表單數(shù)據(jù),表單數(shù)據(jù)對(duì)任何人都是可見(jiàn)的,比如

      http://www.w3school.com.cn/welcome.php?username=jackystudio&password=123

      用$_POST獲取表單數(shù)據(jù),表單數(shù)據(jù)則是不可見(jiàn)的,比如
      http://www.w3school.com.cn/welcome.php

      詳細(xì)可見(jiàn)http://www.w3school.com.cn/php

      3.服務(wù)器php處理代碼


      這里我直接修改了主頁(yè)index.html。會(huì)C++應(yīng)該都能看懂,先是打開(kāi)一個(gè)log.txt,接收到username和password,如果是username是jackystudio,password是123的話,把username和password寫(xiě)入log.txt,并返回登錄成功,如果username或password錯(cuò)誤時(shí)返回登錄失敗。如果未接收到則返回沒(méi)有用戶名密碼。

      3.1.采用get方式代碼

      1. <html>  
      2. <body>  
      3. <?php  
      4. $open=fopen("log.txt","a" ); //Save password  
      5. if(isset($_GET["username"]) && isset($_GET["password"]))  
      6. {  
      7. if($_GET["username"]=="jackystudio" && $_GET["password"]=="123")  
      8. {  
      9. fwrite($open,"Username:".$_GET["username"]);  
      10. fwrite($open,"\r\n");  
      11. fwrite($open,"Password:".$_GET["password"]);  
      12. echo "Login Success"//return to client  
      13. }  
      14. else  
      15. {  
      16. fwrite($open,"Wrong Username or password!");  
      17. echo "Login Failed"//return to client  
      18. }  
      19. }  
      20. else  
      21. {  
      22. fwrite($open,"No password");  
      23. echo "No Username or Password"//return to client  
      24. }  
      25. fclose($open);  
      26. ?>  
      27. </body>  
      28. </html>  

      3.2.采用post方式代碼

      1. <html>  
      2. <body>  
      3. <?php  
      4. $open=fopen("log.txt","a" ); //Save password  
      5. if(isset($_POST["username"]) && isset($_POST["password"]))  
      6. {  
      7. if($_POST["username"]=="jackystudio" && $_POST["password"]=="123")  
      8. {  
      9. fwrite($open,"Username:".$_POST["username"]);  
      10. fwrite($open,"\r\n");  
      11. fwrite($open,"Password:".$_POST["password"]);  
      12. echo "Login Success"//return to client  
      13. }  
      14. else  
      15. {  
      16. fwrite($open,"Wrong Username or password!");  
      17. echo "Login Failed"//return to client  
      18. }  
      19. }  
      20. else  
      21. {  
      22. fwrite($open,"No password");  
      23. echo "No Username or Password"//return to client  
      24. }  
      25. fclose($open);  
      26. ?>  
      27. </body>  
      28. </html>  

      4.cocos2d-x使用CCHttpClient類(lèi)進(jìn)行網(wǎng)絡(luò)請(qǐng)求

      CCHttpClient的使用這里也不贅述了,請(qǐng)移步官方文檔How_to_use_CCHttpClient。這里在上文編輯框和點(diǎn)九圖的基礎(chǔ)上進(jìn)行了修改。2個(gè)編輯框,分別是username和password。一個(gè)按鈕點(diǎn)擊發(fā)送請(qǐng)求。一個(gè)文本顯示從服務(wù)器返回的結(jié)果。


      4.1.按鈕請(qǐng)求處理

      1. void TestLayer::btncallback( CCObject* pSender )  
      2. {  
      3.     bool requestType_is_get=true;//采用get方式或者post方式  
      4.     if (requestType_is_get)  
      5.     {  
      6.         CCHttpRequest* request = new CCHttpRequest();//創(chuàng)建請(qǐng)求對(duì)象  
      7.         string str1 = "127.0.0.1:80/index.html?";  
      8.         string str2 = p_User_EditBox->getText();//獲取username編輯框內(nèi)容  
      9.         string str3 = p_Psw_EditBox->getText();//獲取password編輯框內(nèi)容  
      10.         string struser="username=";  
      11.         string strpsw="&password=";  
      12.         str1=str1+struser+str2+strpsw+str3;  
      13.         request->setUrl(str1.c_str());//設(shè)置請(qǐng)求的url,username和password已經(jīng)包含在url中  
      14.         request->setRequestType(CCHttpRequest::kHttpGet);//設(shè)置為Get模式  
      15.         request->setResponseCallback(this, httpresponse_selector(TestLayer::onHttpRequestCompleted));//設(shè)置響應(yīng)的回調(diào)  
      16.         request->setTag("GET test");  
      17.         CCHttpClient::getInstance()->send(request);//發(fā)送請(qǐng)求  
      18.         request->release();//釋放請(qǐng)求  
      19.     }  
      20.     else  
      21.     {  
      22.         CCHttpRequest* request = new CCHttpRequest();//創(chuàng)建請(qǐng)求對(duì)象  
      23.         string str1 = "127.0.0.1:80/index.html";  
      24.         string str2 = p_User_EditBox->getText();  
      25.         string str3 = p_Psw_EditBox->getText();  
      26.         string struser="username=";  
      27.         string strpsw="&password=";  
      28.         str2=struser+str2+strpsw+str3;  
      29.   
      30.         request->setUrl(str1.c_str());//設(shè)置請(qǐng)求的url,只是請(qǐng)求頁(yè)面的url,并不包含username和password  
      31.         request->setRequestType(CCHttpRequest::kHttpPost);//設(shè)置為Post模式  
      32.         request->setResponseCallback(this, httpresponse_selector(TestLayer::onHttpRequestCompleted));//設(shè)置響應(yīng)的回調(diào)  
      33.   
      34.         const char* postData = str2.c_str();  
      35.         request->setRequestData(postData, strlen(postData));//設(shè)置請(qǐng)求數(shù)據(jù),也就是username和password  
      36.           
      37.         request->setTag("POST test");  
      38.         CCHttpClient::getInstance()->send(request);//發(fā)送請(qǐng)求  
      39.         request->release();//釋放請(qǐng)求  
      40.     }  
      41. }  

      4.2.響應(yīng)回調(diào)處理

      1. void TestLayer::onHttpRequestCompleted( CCHttpClient* client, CCHttpResponse* response )  
      2. {  
      3.     if (!response->isSucceed())//如果響應(yīng)失敗,輸出錯(cuò)誤信息  
      4.     {    
      5.         CCString strError;  
      6.         strError.initWithFormat("Receive Error! \n%s\n",response->getErrorBuffer());  
      7.         m_labelStatusCode->setString(strError.getCString());  
      8.         return ;     
      9.     }    
      10.   
      11.     std::vector<char> *buffer = response->getResponseData();//接收響應(yīng)信息  
      12.     string recieveData;  
      13.     for (unsigned int i = 0; i < buffer->size(); i++)  
      14.     {    
      15.         recieveData += (*buffer)[i];    
      16.     }  
      17.     size_t begin= recieveData.find("<body>")+6;//這里簡(jiǎn)單處理,獲取<body>標(biāo)簽內(nèi)數(shù)據(jù),即是響應(yīng)內(nèi)容  
      18.     size_t end= recieveData.find("</body>");  
      19.     string result(recieveData,begin,end-begin);  
      20.     m_labelStatusCode->setString(result.c_str());  
      21. }  

      5.效果圖

      5.1.Apache運(yùn)行(Get和Post兩種效果都是一樣的)

      (1)賬號(hào)密碼正確時(shí)


      (2)賬號(hào)密碼錯(cuò)誤時(shí)


      5.2.關(guān)閉Apache



      6.源碼下載

      下載地址:http://download.csdn.net/detail/jackyvincefu/6713471


        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類(lèi)似文章 更多