Jump to content

  • Log In with Google      Sign In   
  • Create Account

communicating between C++ server and Java client app


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Triad_prague   Members   -  Reputation: 140

Like
0Likes
Like

Posted 10 November 2012 - 04:31 AM

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 Posted Image )

well, that's all I have in mind. I hope you guys could shed some light here. I'm so eager to learn java Posted Image .
the hardest part is the beginning...

Sponsor:

#2 rip-off   Moderators   -  Reputation: 8726

Like
1Likes
Like

Posted 10 November 2012 - 06:38 AM

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.

Edited by rip-off, 10 November 2012 - 06:39 AM.


#3 Triad_prague   Members   -  Reputation: 140

Like
0Likes
Like

Posted 10 November 2012 - 08:21 AM

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 Posted Image

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. Posted Image
the hardest part is the beginning...

#4 hplus0603   Moderators   -  Reputation: 5707

Like
1Likes
Like

Posted 10 November 2012 - 11:03 AM

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 };

#5 Bubsy   Members   -  Reputation: 407

Like
1Likes
Like

Posted 10 November 2012 - 11:12 AM

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.

#6 Bubsy   Members   -  Reputation: 407

Like
0Likes
Like

Posted 10 November 2012 - 11:14 AM

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.

#7 rip-off   Moderators   -  Reputation: 8726

Like
1Likes
Like

Posted 10 November 2012 - 11:55 AM

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.

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?

#8 Khatharr   Crossbones+   -  Reputation: 3038

Like
1Likes
Like

Posted 10 November 2012 - 12:58 PM

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:

Edited by Khatharr, 10 November 2012 - 01:18 PM.

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.

#9 Triad_prague   Members   -  Reputation: 140

Like
0Likes
Like

Posted 10 November 2012 - 10:03 PM


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.

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?


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
Posted Image
the hardest part is the beginning...

#10 wood_brian   Banned   -  Reputation: 197

Like
0Likes
Like

Posted 11 November 2012 - 05:25 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS