cocos2dx-3.3 网络编程(CURL+PHP) NO.2 登陆功能

前端之家收集整理的这篇文章主要介绍了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 issizemultiplied 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文,发现想要理解还是相当有难度的,遇到不懂的单词还要翻译一下


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

输入正确


输出错误

成功咯

猜你在找的Cocos2d-x相关文章