[java] Gaming - Network issue

Started by
3 comments, last by CodeMachine 17 years, 5 months ago
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]
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.
I think you will also be interested in the non-blocking i/o stuff. Take a look at the java.nio.channels package.
DataInputStream is very slow, use java.nio.ByteBuffer with the classes provided in java.nio.channels.
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]

This topic is closed to new replies.

Advertisement