如何高效定义基于 XML 的文本协议的 End-of-Transmission? [英] How to efficiently define End-of-Transmission for XML-based text protocol?

查看:33
本文介绍了如何高效定义基于 XML 的文本协议的 End-of-Transmission?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个基于 XML 并通过 TCP/IP 套接字传输的文本协议.假设我有一个简单的请求/响应机制要通过持久性发送客户端和服务器之间的 TCP/IP 连接如下:

I want to develop a text protocol based on XML and transmitted via TCP/IP sockets. Let's say I have a simple request/response mechanism to be send over a persistent TCP/IP connection between client and server like this:

<?xml version="1.0" encoding="UTF-8"?>
<request id="1" command="get.answer">
    <value type="string">Answer to the Ultimate Question of Life, the Universe, and Everything</value>
</request>

<?xml version="1.0" encoding="UTF-8"?>
<response id="1" command="get.answer">
    <value type="int32">42</value>
</response>

双方什么时候开始处理传入的数据,换句话说服务器何时会知道传入的客户端数据已完全传输并且可以处理以创建响应?

When should each side start to process the incoming data or in other words when would the server know that the incoming client data is fully transfered and possible to process to create a response?

当然我对这个话题做了一些研究:我发现 this 答案根据 HTTP 示例指出了正确的方向:因此,在 XML 消息之上使用一种传输协议"肯定会有所帮助.

Of course I made some research about that topic: I found this answer which points in the right direction based on an HTTP example: So using a kind of 'Transfer Protocol' on top of the XML messages would certainly help.

但我也看了纯粹基于 XML 的 XMPP 协议,它不使用任何至少就我所见,传输协议"类似于 HTTP.

But I also looked at the purely XML-based XMPP protocol which doesn't use any 'Transfer Protocol' like HTTP at least as far as I have seen.

从 RFC 6120 中的2.4. 结构化数据"它读到:

From RFC 6120 at "2.4. Structured Data" it reads:

XMPP 中的基本协议数据单元不是 XML 流(它只是为点对点通信提供传输)但是XML节",本质上是发送的 XML 片段在一条小溪上.节的根元素包括路由属性(例如from"和to"地址),以及子节的元素包含一个有效载荷,用于交付给预期的收件人.

The basic protocol data unit in XMPP is not an XML stream (which simply provides the transport for point-to-point communication) but an XML "stanza", which is essentially a fragment of XML that is sent over a stream. The root element of a stanza includes routing attributes (such as "from" and "to" addresses), and the child elements of the stanza contain a payload for delivery to the intended recipient.

所以他们通过 TCP/IP 发送基本上小的 XML 块,没有传输协议",并且来自我的wireshark痕迹我可以看到也没有特殊的传输结束字符在每个 XML 节的末尾,比如两次 \r\n 或类似的东西.那么他们如何知道消息的结尾(节)?

So they send basically small XML chunks over TCP/IP w/o 'Transfer Protocol' and from my wireshark traces I can see that there is also no special End-Of-Transmission character at the end of each XML stanza like two times \r\n or something like that. So how do they know about the end of a message (stanza)?

推荐答案

实际上,XMPP 使用 XML流来传输数据.您所指的数据单元是单个消息的实际交换,但它们都包含在定义 XMPP 会话通信的起点和终点的 XML 流中.

Actually, XMPP uses an XML stream to transfer data. The data unit you are referring to is the actual exchange of individual messages, but they are all contained within an XML stream that define the start and endpoint of the communication for an XMPP session.

这将是传输结束的地方,就像所有传输的结束一样.在该流中,有 3 种定义的数据包类型(IQ、Message 和 Presence),它们将指示单个消息的开始和结束(用于客户端到服务器的通信).

This would be where the End Of Transmission occurs, as in end of all transmission. Within that stream, there are 3 defined packet types (IQ, Message and Presence) which would indicate the start and end of individual messages (for client to server comms).

虽然基本情况是通过 TCP 连接完成的,但也有支持不同有线协议的扩展,例如 HTTP 这对于允许 XMPP 通过防火墙很有用.

Although the basic case is done over a TCP connection, there are extensions to support different wireline protocols as well, such as HTTP which is useful for allowing XMPP through a firewall.

如果您想做类似的事情,那么您可以遵循相同的方法,即在建立和断开连接时开始和结束 XML 流.然后,您只需定义各个消息类型,这样您的端点就会知道什么是完整的消息.

If you want to do something similar, then you can follow the same approach, which is to start and end you XML stream when your connection is established and dropped. Then you simply need to define the individual message types, so your endpoints will know what constitutes a complete message.

或者您可以使用 XMPP,它似乎非常适合您的用例.

Or you could just use XMPP which seems to fit your use case perfectly.

这篇关于如何高效定义基于 XML 的文本协议的 End-of-Transmission?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆