[java] How sending something over a network?

Started by
3 comments, last by helldoggie666 16 years, 11 months ago
Hi, I'm trying to make a little network game. Till now i'm sending my commands just as strings over the network. But for sending the player object,I don't what is the best thing to do. I can send all values of the player object as string and rebuild it again on the other side. I guess it's better to use something like a binarystream? What is the fastest way to do this? Thx Tim
Advertisement
I suppose that the fastest would be a binary transmission. But that fixes you to a known format on both sides. Which if you are good with then it's all good.

Best thing to do in that case is to include a versioning indicator, in the event you ever want to expand on what you are sending. You know, different data, or changing the size.

My devils advocate is saying that I've been nailed enough times going after speed increases instead of making something that works well. So I'd choose the path of whatever is easier to complete and finish... and if that's a bag of strings, I'm okay with it. When required, then revisit it for speed... then you got two implementations that you can compare and contrast.
You can send Objects through a socket, but I wouldn't recommend that due to the serialization cost. Instead, why not work with datagram packets, or even NIO and ByteBuffers?

You could use something like this:

public interface Transmittable {    ByteBuffer toByteBuffer();  void fromByteBuffer(ByteBuffer buf);}public void GameNetworkCommand implements Transmittable {    public long timestamp;  // 8 bytes  public int senderId;    // 4 bytes  public int targetId;    // 4 bytes  public ByteBuffer toByteBuffer() {        // Allocate the Buffer    ByteBuffer buf = ...;        buf.putXXX( xxx ); // Some header that you may need    buf.putLong( timestamp );    buf.putInt( senderId );    buf.putInt( targetId );  }  public void fromByteBuffer(ByteBuffer buf) {        int header = getHeaderFrom( buf );    long timestamp = buf.getLong();    // ...  }}


Remember to use the position of the ByteBuffer wisely, or you may read/write corrupt data and have headaches. Overall, it is very simple, and with a bit of tweak can be reusable and quite fast, given a game architecture that knows how to benefit of it.
a.k.a javabeats at yahoo.ca
I'm using serialized objects now.
Ok, I will remove that out of the code and try it on
some other way. Make some tests with those things.
Thx for the responses :-).

Tim
So if you want to send a bigger object to the player, instead
of just a command. Let say a map area or something.
Would you still use the bytearray? So probably splitting the
object into multiple parts then.

Something like
int counter;long timestamp;int senderId;int targetId;byte[] bytes = new byte[100];//putting a part of the object in it


And afterwards putting the object back together in the right order, or
am i wrong with this? Cause it seems to me, if you have a player object
with 100 variables in it, it's pretty hard to put every variable one by
one into the object you send over the network.

This topic is closed to new replies.

Advertisement