communicating between C++ server and Java client app

Started by
8 comments, last by wood_brian 11 years, 5 months ago
Hi fellow developers, I hope you're all fine and well.

Well I got a few question here. Basically I managed to start a simple chat application with movable avatars (like the sims multiplayer). I could code the server in C++, but the client app is meant to be installed on android mobile devices, thus I'd better code it in java.
(I'm quite fluent in C++, but java is quite new to me, although my friend said the transition wouldn't be too painful. I've read several pages of a java book and the syntax is quite similar to C++)

Now comes the complex part, which is communicating between the two languages. Here's several questions I got in my head:

1. What kind of issues that I should anticipate?
2. Aren't the sizes of datatypes in both language the same? what about the byteorder?
3. I use winsock in C++, I wonder if there's such equivalence in java.
4. I'm planning to buy a book that teaches networking in java apps. Do you have some good suggestions? (my current book is for beginner. It doesn't have networking sections in it mellow.png )

well, that's all I have in mind. I hope you guys could shed some light here. I'm so eager to learn java biggrin.png .
the hardest part is the beginning...
Advertisement
Have you considered writing the server in Java? This might simplify some things.

As for your questions:

  • 1. What experience do you have writing portable code?

  • 2. C++ does only gives minimum data type sizes, and does not guarantee byte order. You'd have the same issue writing a portable C++ client talking to a portable C++ server. The solution here is to define what the data should look like on the wire, and then ensure that each peer will correctly pack and unpack the data from this format. You can base the wire format on one that is convenient for one of the implementations (e.g. it might map directly to a big endian C++ unpadded structure written in terms of explicitly sized types).

  • 3. Java has various Socket classes. They are essentially an object oriented version of a berkley-esque API, so will not be dissimilar to WinSock. Probably easier, if anything.

  • 4. I have no specific suggestions, but I would say that if you're experienced in network programming then you probably don't need a book. There are lots of good examples on the internet that will get you up and running quickly.

Have you considered writing the server in Java? This might simplify some things.

As for your questions:

  • 1. What experience do you have writing portable code?
  • 2. C++ does only gives minimum data type sizes, and does not guarantee byte order. You'd have the same issue writing a portable C++ client talking to a portable C++ server. The solution here is to define what the data should look like on the wire, and then ensure that each peer will correctly pack and unpack the data from this format. You can base the wire format on one that is convenient for one of the implementations (e.g. it might map directly to a big endian C++ unpadded structure written in terms of explicitly sized types).
  • 3. Java has various Socket classes. They are essentially an object oriented version of a berkley-esque API, so will not be dissimilar to WinSock. Probably easier, if anything.
  • 4. I have no specific suggestions, but I would say that if you're experienced in network programming then you probably don't need a book. There are lots of good examples on the internet that will get you up and running quickly.



Firstly, I never code anything in Java and compile it in linux. And I'm mostly comfortable with C++. Plus I heard java is quite unsuitable for resource hogging, low-level task that most server do. Correct me if I'm wrong sad.png

1. I've written a simple platformer game using SDL and C++ and also a simple chat program, and port them to Linux (do they count as portable?)
2. The data would consist of header (id and size), and a stream of bytes following it. I'm mostly worried of endianness between platform/language
3. Hmm now that you've mentioned it, I've yet to see if it's supported in Android. I hope it is.
4. 56306d1348599090-memes-okay-okay_meme_tf2_version_by_jaymewes-d2y3ud5-2-.jpg
the hardest part is the beginning...
Java doesn't have to be so bad. Consider that Minecraft is written in Java. As is many large enterprise web applications, such as online banking etc. And if it makes you happy, Java is less resource hogging than Ruby (used by Twitter) or PHP (used by Facebook) or Python (used by Google) :-)

Still, if you want to keep languages separate, you'll need to come up with a way to make sure the languages agree on packet formats and semantics. You have to be very meticulous in how you define each kind of packet you use, and make sure you update both languages at the same time. (Once you ship, you have to do something about older clients connecting, too.) Typically, you'll end up with an IDL (interface description language) of some sort, which takes an abstract description of your packets, and generates code for each language you're using to generate and interpret the bytes on the wire. Example IDLs include Google Protocol Buffers (used by Google,) Apache Thrift (used by Facebook,) Sun XDR (old-school, used by NFS) and many others.
enum Bool { True, False, FileNotFound };
I don't think you'll run into perfomance problems by writing your server in Java (unless your server logic is filled with a lot of number-crushing stuff). Actually the newer JVM are high-optimized with server side in mind. Take a look at apache MINA, it's a framework to write scalable server software.

But if choose to write your server in C++ (which is a logical decision, as you're familiar with it), just write a low-level protocol (not using boost::serializable or any other fancy seralization library, using primitve datatypes always minding types size mismacht and using network endian order (big endian)) and you'll be fine.

Java doesn't have to be so bad. Consider that Minecraft is written in Java. As is many large enterprise web applications, such as online banking etc. And if it makes you happy, Java is less resource hogging than Ruby (used by Twitter) or PHP (used by Facebook) or Python (used by Google) :-)

Still, if you want to keep languages separate, you'll need to come up with a way to make sure the languages agree on packet formats and semantics. You have to be very meticulous in how you define each kind of packet you use, and make sure you update both languages at the same time. (Once you ship, you have to do something about older clients connecting, too.) Typically, you'll end up with an IDL (interface description language) of some sort, which takes an abstract description of your packets, and generates code for each language you're using to generate and interpret the bytes on the wire. Example IDLs include Google Protocol Buffers (used by Google,) Apache Thrift (used by Facebook,) Sun XDR (old-school, used by NFS) and many others.


Yes, Google Protocols are an even better solution than writing low-level network protocol.

Firstly, I never code anything in Java and compile it in linux. And I'm mostly comfortable with C++.
[/quote]
Presumably you'll be getting fluent in Java as part of or before undertaking this project, in order to be able to write the client.


Plus I heard java is quite unsuitable for resource hogging, low-level task that most server do.
[/quote]
That depends on the nature of your game. I expect that for most games that are going to be completed by a single developer or a small team, Java will suffice.

Will all the clients in your game be connecting to one server instance / cluster, or do players create their own servers?
I could code the server in C++, but the client app is meant to be installed on android mobile devices, thus I'd better code it in java.


What just happened right there? The client environment has nothing to do with the language the server is written in. IP derived protocols are platform agnostic. As long as you take control of your byte ordering and serialization you should be fine.

My one word of advice though, is that you use some tool like wireshark or etc to ensure that your socket IO is as expected at both ends. I ran into a problem once where an IO method I was using was tacking a "\n" on the end of outbound data and it took me a week to figure out what was happening. I could have avoided all of that by just glancing at the data on the wire.

Java is less resource hogging than Ruby


Well, Ruby's gotta have somewhere to put in all that extra deliciousness. :wink:
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.


Firstly, I never code anything in Java and compile it in linux. And I'm mostly comfortable with C++.

Presumably you'll be getting fluent in Java as part of or before undertaking this project, in order to be able to write the client.


Plus I heard java is quite unsuitable for resource hogging, low-level task that most server do.
[/quote]
That depends on the nature of your game. I expect that for most games that are going to be completed by a single developer or a small team, Java will suffice.

Will all the clients in your game be connecting to one server instance / cluster, or do players create their own servers?
[/quote]

Yes, I'm learning Java right now, which is quite weird for some friends since the learning process seems to be "reversed" (they learn java first)

On my app, all clients connect to single authoritative server.



I don't think you'll run into perfomance problems by writing your server in Java (unless your server logic is filled with a lot of number-crushing stuff). Actually the newer JVM are high-optimized with server side in mind. Take a look at apache MINA, it's a framework to write scalable server software.

But if choose to write your server in C++ (which is a logical decision, as you're familiar with it), just write a low-level protocol (not using boost::serializable or any other fancy seralization library, using primitve datatypes always minding types size mismacht and using network endian order (big endian)) and you'll be fine.


this is what I'm planning too. I'll be serializing data and not just send the struct/class as "it is". Plus I'll be using htonl() and htons() etc to ensure byte order consistency.


What just happened right there? The client environment has nothing to do with the language the server is written in. IP derived protocols are platform agnostic. As long as you take control of your byte ordering and serialization you should be fine.


well thanks mate. You've just cleared some doubt here.


Btw, it turns out that java is a lil bit different in that it has no "globals". Gotta wrap the main function within a class, but I guess that's fine. I'm considering writing the server in java too. The curve might get a little steeper, but I'm willing to learn. Besides, this project is not too time critical. I do it in my free time only. I've seen several pros and cons of it. And I decided to stick with it. Anyway, thanks guys. I'll be reading a lot of stuffs tonight, and I'll be back here when things are getting too hard to grasp
laugh.png
the hardest part is the beginning...

Java doesn't have to be so bad. Consider that Minecraft is written in Java. As is many large enterprise web applications, such as online banking etc. And if it makes you happy, Java is less resource hogging than Ruby (used by Twitter) or PHP (used by Facebook) or Python (used by Google) :-)


Facebook was heavy on PHP, but then they decided to start using C++ more.

I don't have Android experience, but I've heard C++ is an option there.

This topic is closed to new replies.

Advertisement