How to create network connections using cocos2d-x?

Hello!
I’m a french student and with my group we try to create a game using cocos2d-x wich will be playing on iPhone and Android.
We have a problem with the network communications.
We don’t know in fact how to create sockets and connections to a server using the library of cocos2d-x.
We have seen that the libcurl library could help us but we don’t know how to use it.
I’m sorry for my bad english.:slight_smile:

Thanks for your answers.

Hi,
What we’ve used so far is libcurl making http requests to a web server (ASP.NET MVC but anything should work as long as you keep the responses a short as possible). This means you won’t actually have an opened connection all the time, but rather per request basis.

Depending on the type of game you are creating you might run into performance issues with this approach but I can tell you we’ve built a RTS game making sync requests for about 15 times per second and so far it looks good.

This approach will also enable your users to play together from both Android and IOS.

If you need more info let me know.

Several months ago, I wrote CCHttpClient, a easier wrapper for libcurl.
You can find the source code in cocos2d-x/extensions/network.
And you can find its sample code and usage from cocos2d-x/samples/TestCpp/Classes/ExtensionsTest/NetworkTest

Thanks for your answers Christian and Zhe, it’s very kind of yours.
In fact,for our game we use a galaxy tab as our server and the players will play with theirs phones by communicate with the tablet with wifi.
So, we won’t work with a web server and Http sockets.
We want to use sockets for wifi.
I’m sorry again for my english and the fact that I have not be very clear in my problem’s description.

In this case your best bet would be to make a wrapper for network operations through JNI for Android (basically write the code in Java and access it in C++ through JNI) and Obj-C for IOS.
Thus you will be able to use the regular socket connections.

Thanks Christian
So if I have understand you correctly, you mean that we should create our networks classes and our sockets in a native way kind of sort.
We should create our network in Java on Eclipse and Objective-C on X-Code?
In fact, we don’t know objective-C so we try to find a way to make one developpment for every network connections.
Is there a way to create sockets wifi without using objective-C? Is there any way to use ifdef declarations in our code on visual studio in C++?
I’m sorry but Objective-C is a great problem for us.

If you take the Android case for example you would do the following:

Approach 1
In Java: make static methods for basic networking operations (discovery, connect, send/receive, etc), make this available in C*+ through JNI
In C**: write a wrapper class for the above operations making it platform agnostic either through interface/subclassing or using #ifdefs
use this class to write the actual networking logic code without caring for the underlying platform
Approach 2
In Java: write basic networking operations and handle the pairing and connecting in Java, start the network listening thread there and call through JNI just a callback with data received
In C**: write the callback listener function and handle the different operations.
The second approach though will slightly reduce the flexibility in C*+ but this again depends on the specifications of your project

The same goes for win32 and ios but with slightly easier integration since both C++ and Obj-C runs native

Hope this helps

1 Like

Thank you for your answer Christian but Objective-C is a real problem for us.:slight_smile:
We dont’t know this language and we try to find a way to create the sockets wifi without using Objective-C but it seems that there isn’t any
way to make.
It seems that there isn’t any library in cocos2d-x that can help us create our wifi network without doing a native developpment.
In fact, we chose Cocos2d-X since it give us the possibility to create a multiplatform game for Android and IOS without the objective-C problem.
I thank you again for your answer.

I understand your concern but there are severel socket libraries out there besides the standard that might ease the pain like

As far as I see you could get away with this approach in about 30 lines of Obj-C code. So it shouldn’t be that bad. Just write the connection handling and have some callback to C++ functions.

I’m not a fan of Obj-C either but it shouldn’t scare you that much;)

I think you have used the right words for Objective-C. It scares us a lot.:slight_smile:
The fact is that nobody in our group have an experience in Objective-C nor the use of X-code.
And we should finish our project for the next week so we don’t have much time.
That’s why we tried to find a way to use only visual studio to implement our network.
Is there another way to proceed?
Can the sockets in bluetooth help us? I think that we will have the same problem in Bluetooth than wifi with Objective-C but we never know.:slight_smile:

Well bluetooth case is exactly the same.

If you are on such a tight schedule and don’t want to dive in platform specific code then the approach I suggested using libcurl and a webserver will get you up and running in several hours.
Drop me a line on mail if you are considering the approach and I can send you some code to speed things up.

Cheers

I think that the webserver approach can help us a lot but honnestly I don’t know how to do it so if it don’t worry you, i will send you an email.:slight_smile: