Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了cocos2dx-3.3 网络编程(CURL+PHP) NO.2 登陆功能大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

(提前声明一下,以下内容中passWARD是错误的,应该是password)


首先应该在头文件加以下内容:

#include "curl/include/win32/curl/curl.h"//网络连接-1
#pragma comment ( lib,"libcurl_imp.lib" )
#pragma comment ( lib,"ws2_32.lib" )
#pragma comment ( lib,"wldap32.lib" )
static size_t returnData(char *ptr,size_t size,size_t nmemb,std::string *stream);//获取数据时的回调函数


在按钮的回调函数里加入:
//-----网络连接-1
CURL* curl = curl_easy_init(); //1 curl初始化
char url[1000] = {0};  
//我们根据用户输入的用户名和密码拼出请求url      
sprintf(url,"http://127.0.0.1/check.php?name=%s&passWARD=%s",m_name.c_str(),m_password.c_str()); 
int res; 
  //2 网络连接初始化 
  res = curl_easy_setopt(curl,CURLOPT_URL,url); //设置要连接的网址, res返回0表示成功 
  //3 设定数据接收方法 
  curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,returnData); 
  //4 设定数据接收变量 
  std::string recvbuf; 
  curl_easy_setopt(curl,CURLOPT_WRITEDATA,&recvbuf); 
  //5 发起联网请求 
  res = curl_easy_perform(curl); 
  //6 处理结果,根据网络连接返回的结果实现跳转和提示 
CCLOG(url);
  if (CURLE_OK == res) //CURLE_OK == 0 
  { 
char log_msg[1000];
sprintf(log_msg,recvbuf.c_str());
CCLOG("-----return Data start");
CCLOG(log_msg);
CCLOG("-----return Data end");
    if (recvbuf.compare("1")==1) //如果返回结果为1,即用户名和密码匹配上 
    { 
      CCLOG("login success");
    } 
    else //否则登录失败 
    { 
      CCLOG("login failed");
    } 

  } 
 以下是回调函数: 
 
 
size_t  returnData(char *ptr,size_t  size,size_t  nmemb,std::string *stream)  
{  
    //char* ptr就是返回的服务器数据,服务器echo 1,这里就返回"1"  
    CCLOG("is wriTing");  
    if (stream == NULL)  
    {  
        return 0;  
    }  
    size_t  sizes = size * nmemb;  
    //String* ss = (String *)stream;  
    stream->append(ptr,sizes);  
    return sizes;  
}  


对于curl_easy_setopt(curl,returnData)中的CURLOPT_WRITEFUNCTION,官方有如下解释:

Pass a pointer to your callBACk function,which should match the prototype shown above.

This callBACk function gets called by libcurl as soon as there is data received that needs to be saved.ptrpoints to the delivered data,and the size of that data issize@H_892_25@multiplied withnmemb.

The callBACk function will be passed as much data as possible in all invokes,but you must not make any assumptions. It may be one byte,it may be thousands. The maximum amount of body data that will be passed to the write callBACk is defined in the curl.h header file:CURL_MAX_WRITE_SIZE(the usual default is 16K). IfCURLOPT_HEADERis enabled,which makes header data get passed to the write callBACk,you can get up toCURL_MAX_http_HEADERbytes of header data passed into it. This usually means 100K.

This function may be called with zero bytes data if the transferred file is empty.

The data passed to this function will not be zero terminated!

Set theuserdataargument with theCURLOPT_WRITEDATAoption.

Your callBACk should return the number of bytes actually taken care of. If that amount differs from the amount passed to your callBACk function,it'll signal an error condition to the library. This will cause the transfer to get aborted and the libcurl function used will returnCURLE_WRITE_ERROR.

If your callBACk function returns CURL_WRITEFUNC_PAUSE it will cause this transfer to become paused. Seecurl_easy_pausefor further details.

Set this option to NULL to get thE internal default function used instead of your callBACk. ThE internal default function will write the data to the FILE * given withCURLOPT_WRITEDATA.

第一次硬着头皮啃e文,发现想要理解还是相当有难度的,遇到不懂的单词还要翻译一下


好了,我们现在测试一下:

输入正确


输出错误

成功咯

大佬总结

以上是大佬教程为你收集整理的cocos2dx-3.3 网络编程(CURL+PHP) NO.2 登陆功能全部内容,希望文章能够帮你解决cocos2dx-3.3 网络编程(CURL+PHP) NO.2 登陆功能所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。