对于客户端来说,知道服务器发送的包有多大的优点

发布于 2024-08-04 20:27:55 字数 690 浏览 6 评论 0原文

我对网络编程非常陌生,所以我希望这不是一个完整的新手问题。
我在 Qt-Homepage 上阅读了如何构建小型服务器的教程,我发现 this

QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out << (quint16)0;
out << "..."; // just some text
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));

在 QByteArray 的开头,我们为 16 位整数保留空间,该整数将包含我们要发送的数据块的总大小。 [我们继续以随机方式进行流式传输。] 然后我们返回到 QByteArray 的开头,并用数组的总大小覆盖保留的 16 位整数值。 通过这样做,我们为客户端提供了一种方法,可以在读取整个数据包之前验证他们可以预期多少数据。

所以我想知道,这个过程有什么优点?如果你不这样做会发生什么?也许你还可以添加一个小例子。

I'm really new at network-programming, so I hope this isn't a complete Newbie-question.
I read a tutorial at the Qt-Homepage how to build a little server, and I found this:

QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out << (quint16)0;
out << "..."; // just some text
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));

At the start of our QByteArray, we reserve space for a 16 bit integer that will contain the total size of the data block we are sending. [We continue by streaming in a random fortune.] Then we seek back to the beginning of the QByteArray, and overwrite the reserved 16 bit integer value with the total size of the array. By doing this, we provide a way for clients to verify how much data they can expect before reading the whole packet.

So I want to know, what are the advantages of this procedure? What can happen if you don't do that? Maybe you also could add a little example.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

御守 2024-08-11 20:27:55

这是标准的东西。

对于接收程序来说,通过网络传输的所有内容都只是字节流。除了应用程序强加给它的内容之外,流没有任何意义,就像文件除了应用程序如何定义其记录、行等之外没有任何意义一样。
客户端和服务器理解流的唯一方法是建立他们同意的约定或协议。

因此,实现此目的的一些常见方法是:

  • 使用一个指定消息结尾的分隔符(例如回车符)
  • 传递一个长度字段,如您的示例所示,它告诉接收者下一条消息包含多少数据。
  • 只需建立一个固定约定(例如每条消息将为20字节或类型“A”记录将是一种定义的格式,类型“B”记录将是另一种......)
  • 只需将其视为流即可完全没有约定(例如,将网络上传来的任何内容放入文件中,而不关心它是什么)

长度字节方法的一个优点是接收者确切知道如何大量数据值得期待。通过一些额外的健全性检查,这可以帮助消除应用程序中的缓冲区溢出等问题。

It is standard stuff.

To the receiving program everything coming over the network is just a stream of bytes. The stream has no meaning beyond what the application imposes upon it, exactly the same way a file has no meaning beyond how its records, lines, etc., are defined by the application(s).
The only way the client and server can make sense of the stream is to establish a convention, or protocol, that they agree upon.

So some common ways to accomplish this are by:

  • have a delimiter that designates the end of a message (e.g. a carriage return)
  • pass a length field, as in your example, which tells the receiver how much data comprises the next message.
  • just establish a fixed convention (e.g. every message will be 20 bytes or type 'A' records will be one defined format, type 'B' records another...)
  • just treat it like a stream by having no convention at all (e.g. take whatever comes over the network and put it in a file w/o paying any attention to what it is)

One advantage of the length byte method is that the receiver knows exactly how much data to expect. With some added sanity checks this can help eliminate things like buffer overflows and such in your application.

她如夕阳 2024-08-11 20:27:55

在接收数据包之前了解数据包大小具有性能优势。
然后,您可以从堆或您使用的任何缓冲区管理中准确分配所需的字节数,并通过对“网络接收函数”的几次(最好是一次)调用来接收所有字节。
如果您不知道优势的大小,则必须为消息的一小部分调用“网络接收函数”。

由于“网络接收函数”(可能是 recv() 或 Qt 提供给您的任何函数)是一个系统调用,它还执行 TCP 缓冲区处理等操作,因此应该假设它很慢,每次调用开销很大。所以你应该尽可能少地调用它。

Knowing the packet size before receiving it has a performance advantage.
You can then allocated exactly the needed number of bytes from the heap or whatever buffer management you use and receive all by few (ideally one) calls to the 'network receive function'.
If you don't know the size in advantage, you have to call the 'network receive function' for very small portion of the message.

Since the 'network receive function' (which may be recv() or whatever Qt offers to you) is a system call which also does TCP buffer handling and so on it should be assumed as being slow with a large per-call overhead. So you should call it as few as possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文