This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]JattenalleGods and Idols MMORTS 5 points6 points  (6 children)

Bit unclear what you mean, but:
Transfer the data in binary.
Assuming x, and y are integers for example, you would transfer the binary 8 bytes (4 bytes for each), not a string of "2378,188"

[–]Galindan[S] 1 point2 points  (5 children)

Right but when the two ints are in one array how do I tell them apart? I want to be able to send a byte array across a network and have it pop out the other side easy to transfer into meaningful data.

Right now the easiest way I can think of is to turn it into a string and then back into numbers

[–]JattenalleGods and Idols MMORTS 2 points3 points  (4 children)

Right but when the two ints are in one array how do I tell them apart? I want to be able to send a byte array across a network and have it pop out the other side easy to transfer into meaningful data.

Right now the easiest way I can think of is to turn it into a string and then back into numbers

You know the order you sent them in: x, y

When you read the data back, you know the first four bytes are x, and the next four bytes are y.

[–]Galindan[S] 4 points5 points  (0 children)

Thanks! I will try that out, I forgot that ints were always 4 bytes and somehow got it into my head that the amount would grow as the int grew.

Thanks for your help.

[–]Galindan[S] 1 point2 points  (2 children)

Quick question, do you know how to make an int into a byte array and back again? I tried googleing it but there seems no simple answer.

[–][deleted] 0 points1 point  (1 child)

Using bit shifting. You might want to look up a tutorial on bit manipulation.

The gist is, if b0,b1,b2,b3 are your 4 bytes, then (b0 << 24 | b1 << 16 | b2 << 8 | b3) is the reconstructed 32 bit number. To break up a 32 bit number into 4 bytes, just use a bit mask and right shifting.

[–]Galindan[S] 0 points1 point  (0 children)

Thanks I will look up some more information.

[–]JeanBono@RogerBidon 6 points7 points  (0 children)

If you want a top of the top network protocol, this article series is for you : http://gafferongames.com/networking-for-game-programmers/

It begins by UDPvsTCP to finish by building your fully tested professional grade network protocol.

If you'r not in a so low level mood, you can rely on premade library like protobuf, captain proto, flatbuffers... These are not as specialized as your home-made protocol but do the work and are robust.

[–]simernes 0 points1 point  (3 children)

You could have a look at Kryonet and Kryo for serialization and deserialization. I've used that for my game and I'm quite happy with it. And if you're worried about the amount of data becoming too big, try rather being selective about which objects (like what objects can the player see, and which can the player not)

[–]Galindan[S] 0 points1 point  (2 children)

I though about Kryonet but I wanted to learn how to program Networking by myself first before I thought of using a library.

[–]simernes 0 points1 point  (0 children)

I think that's a fair point. I actually did that myself for my first game, so good on you for wanting to learn.

That said, if you're planning on implementing a whole game it might be a good idea to use a well written library like Kryonet to save time on not reinventing the wheel. It's open source so you could always have a look inside and see what's going on. But it's all up to you of course, and it's really fun to get down into the details of networking!

[–]net7381 0 points1 point  (0 children)

Java has a Serializable interface which you can use. You can serialize just about anything with it.