如何标记TCP数据包的结尾? [英] How should I mark the end of a TCP packet?

查看:502
本文介绍了如何标记TCP数据包的结尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在客户端/服务器应用程序中,将在客户端和服务器之间来回发送不同长度的文本数据,我该如何标记正在发送的数据包的结尾?例如,当服务器从客户端接收数据包数据时,服务器如何知道客户端数据包已被完全接收?

In a client/server application were text data of varying length will be sent back and forth between the client and server, how should I mark the end of a packet that is being sent? For example, when the server is receiving packet data from a client how does the server know that the client packet has fully been received?

更常见的是在数据之前告诉服务器它将要接收的数据包的完整长度,或者在数据包的末尾标记一些内容吗?

Is it more common to tell the server the full length of the packet that it is going to receive before the data or to have something marking the end of the packet?

发送的某些数据长度只有几个字符,而有些则可能是数千个字符.

Some of the data sent will only be a few characters long and some could be thousands of characters.

推荐答案

TCP提供连续的数据流. TCP是使用数据包实现的,但是TCP的全部目的是隐藏它们.

TCP provides a continuous stream of data. TCP is implemented using packets but the whole point of TCP is to hide them.

请考虑一下,就好像它是您要在其上绘制的墙一样.墙是砖头的.用砂浆将砖粘在一起,并在墙壁上涂上石膏使表面光滑.砖是IP数据包,TCP是灰泥.

Think of it as if it was a wall on which you want to draw. The wall is made of bricks. Bricks are glued together with mortar, and plaster is applied to that the wall surface become smooth. Bricks are the IP packets, TCP is the plaster.

因此,现在您有了平滑的抹灰TCP隧道,并且想要在其中添加一些结构.您想要绘制框,以便您的绘图彼此分开.这就是您想要做的:在数据中添加一些管理"结构(图形周围的方框).

So now you have your smooth plastered TCP tunnel, and you want to add some structure in it. You want to draw boxes, so that your drawings are kept separate from each other. This is what you want to do: to add a bit of "administrative" structure (boxes around the drawings) to your data.

许多协议使用packet的概念,它是一堆以固定格式的管理头开始的数据.标头包含足够的信息来决定数据包在何处结束;例如,它包括分组长度. HTTP使用Content-Length标头,或(使用HTTP/1.1)使用分块传输编码",将数据分为一个或几个微型数据包,每个微型数据包都由一个完全由一个微型数据包组成的简单标头组成,长度指示.

Many protocols use the concept of a packet, which is a bunch of data beginning with a fixed-format administrative header. The header contains enough information to decide where the packet ends; e.g., it includes the packet length. HTTP does that, with a Content-Length header, or (with HTTP/1.1) with the "chunked transfer encoding" where data is split into one or several mini-packets, each with a simple header consisting of exactly a mini-packet-length indication.

另一种方法是具有一个特殊的终止符序列,该序列不能出现在正常数据"中.如果您的数据是文本,则可以使用零值字节作为终止符.

Another way is to have a special terminator sequence which cannot appear in "normal data". If your data is text, then you could use a byte of value zero as terminator.

另一种方法是使用自终止数据.这种数据的结构方式使您可以随时知道是否已到达元素的末尾.例如,XML数据被组织为诸如<foo>...</foo>之类的嵌套标记对.当到达结束标记(</foo>)时,您就知道该元素已完成.

Yet another way is to use self-terminated data. This is data structured in such a way that you can know at any point whether the end of the element has been reached. For instance, XML data is organized as nested pairs of markers such as <foo>...</foo>. When the end marker (</foo>) is reached, you know that the element is finished.

这篇关于如何标记TCP数据包的结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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