"HTTP text of c/s communication interaction" COCOS2DX (Client) uses Curl and jetty (Server) to implement the HTTP communication framework for mobile online games (contains the solution curl.h header file can not find the problem)

Source: Internet
Author: User

Have previously shared a framework based on COCOS2DX and the server using the socket to communicate, but also unfamiliar to the following blog post:

"C/S communication interaction Socket" COCOS2DX (Client) use BSD socket and Mina (Server) mobile games Communication Framework!

So today Himi to share how to use HTTP in cocos2dx to access the server side and get the data;

Here for the server side, Himi selection, Jetty, Jetty is not familiar with the first self-baidu~google~ is a servlet container. Similar to JSP. What is a servlet? Jsp? = =. Don't dwell on it. Everybody manual good;

Below we simply write a server side (how to create a Jetty server see Himi Jetty Development Series article)

——— – First server-side ———— –

This is where IBU writes the jar package build path that created the project and configuration item. Directly on the main code snippet:

The first is the jetty server main class: (Here the Himi IDE is Eclipse)

1234567891011121314151617181920212223242526 ServletServer.java importorg.eclipse.jetty.server.Server;importorg.eclipse.jetty.servlet.ServletContextHandler;importorg.eclipse.jetty.servlet.ServletHolder;importservlet.HServlet;/** * @author Himi */publicclassServletServer {    publicstaticvoidmain(String[] args) throwsException {        Server server = newServer(8080);         ServletContextHandler context = newServletContextHandler(ServletContextHandler.SESSIONS);        context.setContextPath("/");         server.setHandler(context);          context.addServlet(newServletHolder(newHServlet()), "/himi");         server.start();        server.join();    }}

Then there is one of our servlet classes:

12345678910111213141516171819202122232425262728293031323334353637383940414243 HServlet.javapackageservlet;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;/** * @author Himi */publicclassHServlet extendsHttpServlet {    privatestaticfinallongserialVersionUID = 1L;     publicHServlet() {        protectedvoid doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {        System.out.println("~~~~有一个Clinet访问!~~~~");        //获取http Client端对应的两个字段的数据        String name = request.getParameter("name");        String password = request.getParameter("password");        //设置字符编码        response.setCharacterEncoding("UTF-8");        response.setContentType("text/html");        response.setStatus(HttpServletResponse.SC_OK);        response.getWriter().println("Server say: 测试中文:session="+ request.getSession(true).getId());         if(name!=null) {            response.getWriter().println("Server say:名字:"+name);            System.out.println("Client say: name="+name);        }        if(password!=null) {            response.getWriter().println("Server say:密码:"+password);            System.out.println("Client say: password="+password);        }    }}

In our servlet, we get back the data from the HTTP clinet end. Middle Simple to the client some simple string ~

OK, start our jetty server, right-Servletserver.java run, watch the console:

12 2012-05-25 16:43:04.767:INFO:oejs.Server:jetty-8.1.3.v201204162012-05-25 16:43:05.110:INFO:oejs.AbstractConnector:Started [email protected]:8080

Appears as above, indicating that your jetty server started successfully; OK. Then design the client code;

——— – then Cocos2dx clinet End ———— –

Start by creating a new COCOS2DX project, which doesn't say much. Then replace the following code in the default HelloWorldScene.cpp initialization function:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 bool HelloWorld::init(){/* *@author By Himi */    //////////////////////////////    // 1. super init first    if( !CCLayer::init() )    {        returnfalse;    }    CCLabelTTF* pLabel = CCLabelTTF::labelWithString("Hello World""Thonburi", 34);    CCSize size = CCDirector::sharedDirector()->getWinSize();    pLabel->setPosition( ccp(size.width / 2, size.height - 20) );    this->addChild(pLabel, 1);    CURL *curl;    CURLcode res;    charbuffer[10];    curl = curl_easy_init();    if(curl)    {//        curl_easy_setopt(curl, CURLOPT_URL, "");        curl_easy_setopt(curl, CURLOPT_URL, "李华明");        res = curl_easy_perform(curl);        /* always cleanup */        curl_easy_cleanup(curl);        if(res == 0)        {            pLabel->setString("0 response");        }        else        {            sprintf(buffer,"code: %i",res);            pLabel->setString(buffer);        }    }    else    {        pLabel->setString("no curl");    }    returntrue;}

OK, then we import the curl.h header file:

1 #include "curl/curl.h"

Not yet, this time prompted us to find this header file, OK, continue to operate two steps as follows:

1. Add the Libcurl.a file: (This file defaults to the Cocos2dx/platform/third_party/ios/libraries folder under the COCOS2DX engine package)

Don't worry at this time will also prompt the header file can not find;

Click your COCOS2DX project in Xcode, then select the targets of your project and find the Search Paths in Build settings:

Double-click your Library Search Paths to observe:

The following "$...../third_party/ios/libraries" path is added by default when you add Lib Curl.a in the first step. We don't want to change this, but please double-click the path and copy it.

What we need to modify is the previous property of this property, Header Search Paths;

Double-click the header Search paths the connection after the property, and then click on the "+" sign to add a path, this path is just your copy of the path, but after pasting also set this path to the path of the previous folder, so a bit around, in fact, is as follows:

Assuming that your previous copy path is "$...../third_party/ios/libraries"

So when you paste here, the path should be: "$...../third_party/ios"

Ok,himi The path here also gives you a convenient comparison:

OK, if the above steps are normal then compile will have no problem;

After the compilation succeeds, Command+r runs the project, observes the Xcode console print, and the server-side printing: Normally it should be as follows:

OK, everything is fine.

Note: The new project with the script, the default is no Libcurl, you compile to the other platform when you want to modify the makefile file to add it, (specifically, you can refer to tests inside the makefile)

Reminder: Here the client and the server is just a simple HTTP interaction, no more details of processing, such as client side access should be another thread, the interaction of data to have a certain protocol specification and so on when the introduction of the socket has been said, here is not much to say;


"HTTP text of c/s communication interaction" COCOS2DX (Client) uses Curl and jetty (Server) to implement the HTTP communication framework for mobile online games (contains the solution curl.h header file can not find the problem)

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.