UDP packages

So I’m trying to write a card game, I’m using udp, but I don’t really know how the packages should look like. I started with sending Strings with some prefixes like: “/c/” + client.getId() + “/e/” or "/d/ + client.getId() + “/e/”. Is it a good way to do this or is there a better way?

Help would be really appreciated. ;D

Are you talking about serializing data? If so, you should try .json format. There are a billion json libraries for java.

The most effective way would be to send and receive raw byte arrays.

I’d recommend starting each byte array with an identifying byte, which tells the recipient what the packet is for and then follow with the data.
I’m guessing that those “/d/”-like strings are separators, but it would be more efficient for the recipient to know how long everything in the array is, so bytes aren’t wasted on separators.

If you send strings, the recipient has to do the following:

  • Convert the byte array into a String
  • Split the String
  • Parse any Integers or Shorts etc

If you use raw byte arrays, the recipient has to do the following:

  • Extract each piece of data from the array
  • Convert some of it into things like Integers or Shorts etc

Below is an example of my favourite approach.
Say you want to tell the server that you are placing a card into your deck. Assuming that there are under 257 cards in your hand and less than 257 slots in your deck, I’d structure the packet to only use 3 bytes like this:
1x Identifying byte | 1x (short) Card Position in Hand | 1x (short) Position in Deck

Ok, thank you. That was helpful. :slight_smile: