TCP > Link > Three standard message formats @ spring > length-header / stx-etx / crlf


TCP通信などにおけるデータフォーマットに関して、<CR><LF><STX><ETX>などの使用について疑問に思った。

以下を見つけた。

Three standard message formats are provided for this purpose; you can also provide code for your own custom format.

  • > The first of the three standard formats is length-header, in which case a 4 byte length header precedes the data;
  • > The second is stx-etx in which the message data is preceded by an STX (0x02) character and terminated with an ETX (0x03) character.
  • > The third is crlf in which the message is terminated with a carriage return and line feed (\r\n).

...

The length-header format can also handle binary data. The other two formats can only handle text data (specifcally, data that does not contain characters 0x02 and 0x03 for stx-etx and 0x0d and 0x0a for crlf). This limitation can be avoided by appropriate character escaping techniques in the application layer.