How to safely separate packet data?

Ladas125

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?

Alec Teal

There are 3 solutions:

  1. Termination character - strings often end with a literal 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 thing
  2. A start character (not a good solution) suppose we start data with 0 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
  3. The most common for protocols where the messages are defined (as for games) is so common it even has a name, https://en.wikipedia.org/wiki/Type-length-value - type length value (encoding).
    Basically you say "the first 4 bytes are an integer denoted the message type" then the next 4 bytes is the length of the payload (the value) for example: 0x1 0x4 Alec denotes
    • message type: 1
    • length: 4
    • data: Alec

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to extract data from Bluetooth packet data?

From Dev

TCP FIN in separate packet

From Dev

How can you safely tell that a TCP/IP packet was received at the other end?

From Dev

How to safely store HTML data in MySQL?

From Dev

How to safely rely on Ajax data being present

From Dev

How to safely use std::move() on template data

From Dev

Scapy - How to sniff packet and send it as encapsulated data

From Dev

Python How to split a packet to extract data

From Dev

How to randomly separate data in MATLAB

From Dev

Breeze - how to separate user data?

From Dev

How to separate String array data?

From Dev

How to randomly separate data in MATLAB

From Dev

How to separate String array data?

From Dev

How do I safely parse data from a Ruby Hash?

From Dev

How can I safely handle invalid requests for repository data?

From Dev

How to know if wstring can be safely (no data loss) converted to string?

From Dev

How can I transfer data from unsigned char * to char * safely?

From Dev

How safely does reinstalling Windows wipe old data?

From Dev

How to load data in global memory into shared memory SAFELY in CUDA?

From Dev

How to safely display user's server side data in a form?

From Dev

How to safely retrieve sql data using a stored function

From Dev

How do I safely parse data from a Ruby Hash?

From Dev

How to safely deserialize keys/data format JSON response

From Dev

How to get packet.tcp.payload and packet.http.data as string?

From Dev

How do I specify packet data in Net::RawIP?

From Dev

How to send big chunk of data in one UDP packet?

From Dev

How to intercept packet on TCP layer in kernel to analyze data?

From Dev

How do you properly modify packet data in Scapy?

From Dev

How to copy hex data of captured packet form wireshark

Related Related

  1. 1

    How to extract data from Bluetooth packet data?

  2. 2

    TCP FIN in separate packet

  3. 3

    How can you safely tell that a TCP/IP packet was received at the other end?

  4. 4

    How to safely store HTML data in MySQL?

  5. 5

    How to safely rely on Ajax data being present

  6. 6

    How to safely use std::move() on template data

  7. 7

    Scapy - How to sniff packet and send it as encapsulated data

  8. 8

    Python How to split a packet to extract data

  9. 9

    How to randomly separate data in MATLAB

  10. 10

    Breeze - how to separate user data?

  11. 11

    How to separate String array data?

  12. 12

    How to randomly separate data in MATLAB

  13. 13

    How to separate String array data?

  14. 14

    How do I safely parse data from a Ruby Hash?

  15. 15

    How can I safely handle invalid requests for repository data?

  16. 16

    How to know if wstring can be safely (no data loss) converted to string?

  17. 17

    How can I transfer data from unsigned char * to char * safely?

  18. 18

    How safely does reinstalling Windows wipe old data?

  19. 19

    How to load data in global memory into shared memory SAFELY in CUDA?

  20. 20

    How to safely display user's server side data in a form?

  21. 21

    How to safely retrieve sql data using a stored function

  22. 22

    How do I safely parse data from a Ruby Hash?

  23. 23

    How to safely deserialize keys/data format JSON response

  24. 24

    How to get packet.tcp.payload and packet.http.data as string?

  25. 25

    How do I specify packet data in Net::RawIP?

  26. 26

    How to send big chunk of data in one UDP packet?

  27. 27

    How to intercept packet on TCP layer in kernel to analyze data?

  28. 28

    How do you properly modify packet data in Scapy?

  29. 29

    How to copy hex data of captured packet form wireshark

HotTag

Archive