I am creating a multiplayer game and not sure how should I separate packet data.
My packet structure looks like this (it is one string): id (separator) content
And the content sometimes also has multiple separators (to send more data in one packet)
But if my game had a chat (or any other user input), user could type the separator string and break the server. How can I prevent that?
Is there any other more safe way of sending network data than PrintWriter to the socket output stream?
There are 3 solutions:
0
(a byte of value 0) - this isn't a character so you can tell when the string ends because it reads for example Alec0
where the 0
is a literal 0
not the letter. Because it doesn't map to a letter you KNOW that it ends the string. You could employ the same thing0
instead (this works for streams) then you can processes 0msg0other
as g
must be the last of message
because 0
starts a new incoming bit of data. Then the stream ends so the last bit must be other
0x1
0x4
Alec
denotes
Alec
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments