vs Engineering part----Network programmingThis section will put the final implementation code and resources in the article most not provide you download learning. In this section we start to play the development of networking features, I use cocos2d-x bound Curl Library, this curl is said to be very fire, although I know not much only know the basic usage, let everyone laughed at. Well, want to use this library or to be set in the project, such as the beginning to include the header file #include "curl/curl.h" (the official wording) that requires the project properties---"Add a curl path in the additional library directory, if you are as confused as I am, so simply write all: # Include "Curl/include/win32/curl/curl.h" This is going to be okay. Also include the Lib under Curl, additional dependencies, input, project properties, linker, add "Libcurl_imp.lib "," Ws2_32.lib "," Wldap32.lib "the first of the most important must add, after two Lib online tutorial said to add do not know is really need. began to work formally, in the last section onbtnloginclicked is the login button response code, put txtuser->setstring ("Click Login Btn"), this line out, write the following code:
0 Get user name and password string struser = Txtuser->getstring (); String strpass = Txtpass->getstring ();//1 Curl initialization curl* curl = Curl_easy_init (); if (curl)//If initialization succeeds {char url[1000] = {0 }; We spell request urlsprintf (URL, "http://127.0.0.1/testPhp/checkLogin02.php?uname=%s&upass=%s") based on user name and password entered by the user. Struser.c_str (), Strpass.c_str ()); int RES;//2 network connection Initialize res = curl_easy_setopt (curl, Curlopt_url, URL); Set the URL to be connected, RES returns 0 for successful//3 set data Receive method Curl_easy_setopt (curl, curlopt_writefunction, processdata);//4 set data receive variable std:: String recvbuf;curl_easy_setopt (Curl, Curlopt_writedata, &recvbuf);//5 initiating networking Request res = Curl_easy_perform (curl);//6 Processing results, based on the results returned by the network connection to implement the Jump and prompt if (CURLE_OK = = Res)//CURLE_OK = = 0{if (Recvbuf.compare ("1") ==0)//If the return result is 1, that is, the user name and password match on {// Save the user's Name//helloworld::setuname (struser);//Login success, jump to the game scene, here Gamescene is your official game scene, I did not realize, please forgive//director::getinstance ()->replacescene (Gamescene::scene ()); Lblresult->setstring ("Login success!"); else//Otherwise login failed {lblresult->setstring ("login failed!");}}}
Comments are very detailed, you can see the basic usage of curl, need curl_easy_setopt set Curlopt_url: Request URL, set curlopt_writefunction: Data reception method, and Curlopt_ WriteData: Used to hold a string that accepts the returned data. The curlopt_writefunction is very important, which determines what is the success or not of receiving data. Then when everything is set up, execute Curl_easy_perform to execute the network request and get the response data from the server, which is the success of the login we want. Note that this curl_easy_perform function is a big pit, curl library here error handling writing is very unfriendly, regardless of what went wrong execution to Curl_easy_perform are instantaneous runtime crashes, no assert error hint, let the user inexplicably, unable to debug. In fact, most of the error is the above curl_easy_setopt Setup error, especially curlopt_writefunction, the data receive method may be wrong parameter here will crash, I put that processdata function to paste out:
size_t ProcessData (char *ptr, std::size_t size, std::size_t nmemb, std::string *stream) {//char* ptr is the returned server data, server Echo 1, This 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;}
function parameter format to be strictly according to this writing, note that this function can not be written as a class member function, can only be written into the class intrinsic function, or execute curl_easy_perform, this function parameter char* ptr is the server returns the first address of the data, size is probably a packet how many bytes, Nmemb is probably how much packet meaning, stream is we set the Curlopt_writedata when set the receive data string recvbuf, so the function meaning is very clear, is simply based on the number of bytes returned by the server char* PTR memory data is copied to stream, which is recvbuf, so we get the received data returned by the server.
Well done, you can run, you must remember to open the server before running, is to run Phpstudy, start Apache and MySQL, like this
In case it is advisable to manually enter the URL in the browser:http://127.0.0.1/testPhp/checkLogin02.php?uname=wang&upass=123See if the server is normal, do not make the server is not open debugging, the result is login failure and crazy-_-OK, let's test it out, this should show the login success in the UI interface, and I won't cut it. Source Code + resource file
Cocos2d-x Network Programming Connect PHP Server Note 4