Sign in to follow this  
CodeMachine

[java] Gaming - Network issue

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this