Cocos2d-x network programming connection php server Notes 4

Source: Internet
Author: User
Tags response code
Cocos2d-x network programming connection php server Notes 4 VS Engineering part ---- network programming

This section will describe the final implementation code and resources in the document, which is not available for download.

In this section, we started the development of online functions, I use the cocos2d-x bound curl Library, this curl is said to be very popular, although I do not know much about the basic usage, let everyone laugh. Okay. You still need to set this library in the project. for example, you need to include the header file # include "curl/curl. h" at the beginning (officially written)

In this way, you need to add the curl path to the Directory of the additional library "project properties"> "linker". if you are not aware of it like me, simply write it all: # include "curl/include/win32/curl. h "it's okay.

In addition, the lib of curl must be included. add "libcurl_imp.lib" and "ws2_32.lib" to the project Properties> linker> Input> additional dependencies ", "wldap32.lib" must be added as the first and most important feature. the last two lib online tutorials indicate that you need to add them.

In the previous section, onBtnLoginClicked is the response code of the logon button. in the response code, the line txtUser-> setString ("click login btn") is injected, and the following code is written:

// 0 get the username and password string strUser = txtUser-> getString (); string strPass = txtPass-> getString (); // 1 curl initialize CURL * curl = curl_easy_init (); if (curl) // if the initialization is successful {char url [1000] = {0}; // based on the user name and password entered by the user, we spell out the request urlsprintf (url, "http: // 127.0.0.1/testPhp/checkLogin02.php? Uname = % s & upass = % s ", strUser. c_str (), strPass. c_str (); int res; // 2 network connection initialization res = curl_easy_setopt (curl, CURLOPT_URL, url); // Set the url to be connected, res returns 0 to indicate success // 3 sets the data receiving method curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, processData); // 4 sets the data receiving variable std: string recvbuf; curl_easy_setopt, & recvbuf); // 5 initiate a networking request res = curl_easy_perform (curl); // 6 handle the result, implement a jump based on the result returned by the network connection and prompt if (CURLE_ OK = res) // CURLE_ OK = 0 { If (recvbuf. compare ("1") = 0) // if the returned result is 1, the user name and password match {// save the user's name // HelloWorld :: setUname (strUser); // login successful, jump to the game scenario, here GameScene is your official game scenario, I did not implement, please forgive me // Director: getInstance () -> replaceScene (GameScene: scene (); lblResult-> setString ("Login Success! ");} Else // otherwise the logon fails {lblResult-> setString (" Login Failed! ");}}}
The comment is very detailed. you can also see the basic usage of curl. you need to set CURLOPT_URL: Request URL, set CURLOPT_WRITEFUNCTION: Method for receiving data, and CURLOPT_WRITEDATA: string used to store the returned data. Among them, CURLOPT_WRITEFUNCTION is very important and determines whether the data is successfully received. After everything is set, execute curl_easy_perform to execute the network request and obtain the response data from the server, that is, whether or not we want to log on successfully.

Note that the curl_easy_perform function is a big pitfall. the error processing and writing in the curl library are unfriendly. no matter what error occurs, the execution of curl_easy_perform crashes instantly without any assert error prompts, it makes the user confused and cannot be debugged. In fact, most of the errors are the above curl_easy_setopt settings errors, especially CURLOPT_WRITEFUNCTION. The data receiving method may crash if the parameters are incorrect. I posted the processData function:

Size_t processData (char * ptr, std: size_t size, std: size_t nmemb, std: string * stream) {// char * ptr is the server data returned, the server returns "1" log ("writing data"); if (stream = NULL) {return 0;} size_t sizes = size * nmemb; // string * ss = (string *) stream; stream-> append (ptr, sizes); return sizes ;}
The format of function parameters must be strictly as follows. Note that you cannot write this function as a member function of the class. you can only write it as an internal function of the class. Otherwise, an error occurs when you execute curl_easy_perform, this function parameter char * ptr is the first address of the data returned by the server. size is about the number of bytes of a data packet, and nmemb is about the number of data packets, stream is the receiving data string recvbuf we set when setting CURLOPT_WRITEDATA. in this way, the function is quite clear, simply copy the data in the char * ptr memory to the stream based on the number of bytes returned by the server, that is, recvbuf. in this way, we get the received data returned by the server.


Okay, you can run it. before running, remember to open the server, that is, run phpStudy to start apache and mysql, like this


Just in case, we suggest you manually enter the URL http: // 127.0.0.1/testPhp/checkLogin02.php in your browser? Uname = wang & upass = 123

Check whether the server is normal. do not debug the server if it is not enabled. The result is that the login fails and the server is mad -_-

Now, let's try it out. it should be displayed on the UI that the logon is successful and I will not cut it down.

Source code + resource file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.