Are you using 8 booleans for player ID?
Anyway, here's a quick look at what your packets should look like.
Let's say you're sending an ascii string (8-bit). You can either send length before the string or place a zero at the end.
The binary for sending "TEST" should be either:
- 04 54 45 53 54 (Length before string)
- 54 45 53 54 00 (Zero after string)
You may also want different operation codes. Since most games hardly ever use more than 256 we can just use a single byte for the enum.
Let's say player "Player1" joins the game.
(01) (00) (07) (50 6C 61 79 65 72 31)
I am using parenthesis for each variable we're sending.
First we have an operation code, I chose 0x01 to be for player joining.
Second we have player ID, in this case player 0.
third we have our string length.
fourth we have our string "Player1".
Now that our player joined. It's much easier to deal with player ID than string because:
- You don't have to search for player name each time.
- Much less data is sent each time.
- Player name may change during gameplay. (Optional)
- Player name is unknown. Server sent other packets concerning that player before sending the name.
- 2 or more players have the same name.
Everything I listed above does not affect performance much but can lead to some game breaking bugs.
I hope it helps you understand how strings are sent between clients and server.
I was reading that in C# a string takes 20 bytes as an object and 2 bytes for each character.
The 20 bytes are used to define where the string is stored in memory and some other information not required by data streams.