Jump to content
  • Advertisement
Sign in to follow this  
CodeMachine

[java] Gaming - Network issue

This topic is 4275 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello fellows! I have some questions about how to "best" implement network communication in a game. Im making a racing-game, the game is dual player only. One acts as server (choose race map, game mode etc), and the other one acts client. I want to send "data-bundles" like: - Settings (race map, gamemode (time on/off etc), (only for server). - Coordinates when racing. - Status messages when racing (as fuel left, gamewon, gameover, etc). - etc. I think I should use "DataInputStream" and "DataOutputStream". Im I right? :) Should I send the "bundles" above like: "1" (Settings bundle), total_bytes_to_read, race map bytes, game-mode bytes, etc ? Or is it recommended to send it all as objects (using "ObjectInputStream" and "ObjectOutputStream"? Should I then have one object for each "bundle"? I need some help! :) Kind Regards [Edited by - CodeMachine on November 7, 2006 10:37:07 AM]

Share this post


Link to post
Share on other sites
Advertisement
As far as I know it's mostly a matter of personal preference, but if you are using UDP(where some packets may be lost or arrive out of order) it could be difficult to identify what is what if you are sending primitive data types(bytes, ints etc) whereas custom objects specially designed for sending will be much easier to identify when they get there. Though with TCP/IP it shouldn't matter too much since everything gets there in the same order it was sent. The advantage of not using large objects is that you can begin working with some of the data before the rest of it gets there. I personally prefer ObjectInputStream and ObjectOutputStream because they are easier to use in my opinion.

Share this post


Link to post
Share on other sites
I think you will also be interested in the non-blocking i/o stuff. Take a look at the java.nio.channels package.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
DataInputStream is very slow, use java.nio.ByteBuffer with the classes provided in java.nio.channels.

Share this post


Link to post
Share on other sites
Hi
Thanks for your answers!

I made one class for every "bundle" and used "ObjectInputStream" and "ObjectOutputStream".
It worked perfectly.

(NOTE, the objects are very small (contains only 1-4 members (int/boolean/string)), so it shouldn't be inefficient I think :))

Kind Regards

[Edited by - CodeMachine on November 8, 2006 4:29:30 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!