通过 TCP 传输数据的格式是什么?
我想要一些关于使用哪种格式通过 TCP 传输数据的建议。目前,我设计了一个带有分隔字符串的简单文本协议。我想我应该使用一些已经存在的东西,比如 XML、JSON 或 XMPP?
人们使用什么数据格式通过 TCP 进行传输?
我想优化速度和吞吐量,但宁愿采用现有标准,也不愿使用我自己的标准。
I'd like some advice on which format to use for transmitting data over TCP. Currently, I have devised a simple text-protocol with delimited strings. I'm thinking I should use something out there that already exists such as XML, JSON, or XMPP?
What data formats do people use for transmitting over TCP?
I would like to optimize for speed and throughput but would rather adopt an existing standard than use my own.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可能需要查看 Google Protocol Buffers 或 Apache Thrift。
You might want to look at Google Protocol Buffers or Apache Thrift.
在大多数情况下,人们只需声明一个具有两端可复制布局的记录并使用它。只有当你有更复杂的需求时,你才必须做比这更花哨的事情。
对于不同长度的字符串,我可能只是将它们实现为一系列长度,然后是长度字节的数据。在 Cish 语言中,您可能可以通过利用空终止符而无需长度。没有什么比这更复杂的了。
In most cases folks just declare a record with a layout that is reproducable on both ends and use that. It is only when you have more complicated needs that you have to do anything more fancy than that.
For varying length strings, I'd probably just implement them as a series of length and then length bytes of data. In Cish languages you could probably get by without the length by leveraging the null terminator. Nothing more complex than that is really needed.
取决于数据类型以及谁在使用您的数据。
如果您正在编写自己的客户端/服务器对,那么可以说最好的格式是某种二进制序列化。它结构紧凑,可以轻松地通过线路传输,并且可以快速重建。
如果您正在使用多种语言为许多消费者编写内容...那么我会更担心 XML 或 JSON(取决于数据的大小和复杂性)。
XML 更适合大型、复杂的数据。
JSON 更适合更小、更紧凑的数据。
Depends on the type of data and who is consuming your data.
If you're writing your own client/server pair, then arguably the best format is some sort of binary serialization. It's compact, transmits easily over the wire, and can be quickly reconstructed.
If you're writing something for many consumers using a variety of languages...then I'd worry more about XML or JSON (depending on the size and complexity of your data).
XML is more well suited for large, complex pieces of data.
JSON is better for smaller, more compact pieces of data.
您的数据的键/值外观表明 JSON 可能更容易使用。
优化速度和吞吐量可能更好地在应用程序之外、OSI 模型中的第 3 层和第 4 层进行处理我会冒险。在这些层上投入一单位的优化工作可能会比在数据结构和编码中投入一单位的工作带来更多的优化。
Your data's key/value appearance suggests JSON might just be easier to work with.
Optimising speed and throughput is probably better handled outside your application, layers 3 and 4 in the OSI model I'd hazard. One unit of optimising effort invested in those layers will likely give you more optimality than one unit of effort sunk into the structure and encoding of your data.
XML 听起来对于您的数据类型来说是一个不错的选择 - 已经有大量的 XML 库(或者您的语言甚至可能内置了 XML 解析)。
基于文本还使手动调试变得更容易,因此这是远离在线二进制编码的原因之一。
XML sounds like a good choice for your data type - there's plenty of XML libraries out there already (or your language might even have XML parsing built in).
Being text-based also makes it easier to debug things by hand, so that's one reason to stay away from binary encodings on the wire.