强制 TCP 流发送缓冲区内容

发布于 2024-11-18 08:10:22 字数 320 浏览 6 评论 0原文

我正在使用 Csharp tcp 套接字在客户端和服务器之间发送数据。 现在,我认为或我认为的问题是 tcp 是一种流协议,除非数据量足够,否则不会推送(发送)数据。

例如,假设我想发送一些数据,无论它是什么,都没关系,我们只说它的 8 字节长。我看到的行为是,无论我等待多长时间,它都不会发送该数据,除非我在它后面推更多的数据,大概直到我到达 tcp 缓冲区。

所以我的问题是。如果我想通过 tcp 发送少量数据,我是否需要在末尾附加垃圾以强制套接字发送。 (我对此感觉不太好)或者他们是我可以强制发送流的前段的另一种方式。

提前致谢。我还在学习 tcp 所以请原谅我的无知。

I am using Csharp tcp sockets to send data between a client and server.
Now the problem as i see it or as i perceive it is that tcp is a stream protocol and will not push (send) data unless their is a sufficient amount of it.

For instance say i wanted to send some data whatever it is doesnt matter lets just say its 8 bytes long. The behaviour i am seeing is no matter how long i wait it wont send that data unless i push more behind it presumably until i reach the tcp buffer.

So my question is. If i want to send a small amount of data via tcp do i need to append garbage to the end to force the socket to send. ( I wouldnt feel good about this ) or is their an alternative way i can force the front segment of the stream to send.

thanks in advance. i am still learning tcp so excuse the ignorance.

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

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

发布评论

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

评论(2

半城柳色半声笛 2024-11-25 08:10:22

除非您是 TCP/IP 专家并了解其全部影响,否则不要设置 NoDelay。如果你还没读过史蒂文斯的书,就别想了。

这是一个示例问题:如果建立套接字连接并在其上发送 8 个字节,那么这 8 个字节是立即发送还是 Nagle 算法等待更多数据发送?答案是“立即发送 8 个字节” - 但在您确切理解为什么为什么这就是答案之前,不要考虑搞乱 Nagle。

这里还有一个问题:在标准命令/响应协议中,每个数据包应用多少 Nagle 延迟?答案是:没有。同样,您应该研究 Nagle 在这种常见情况下为什么导致没有延迟。

如果您没有看到 250 毫秒(Nagle 在最坏的情况下造成的最大延迟)发送的数据,那么还有其他问题。

Don't set NoDelay unless you are an expert at TCP/IP and understand its full ramifications. If you haven't read Stevens, don't even think about it.

Here's an example question: if you establish a socket connection and send 8 bytes on it, are the 8 bytes immediately sent or does the Nagle algorithm wait for more data to send? The answer is "the 8 bytes are sent immediately" - but don't consider messing with Nagle until you understand exactly why that is the answer.

Here's another question: in a standard command/response protocol, how much Nagle delay is applied to each packet? The answer: none. Again, you should research why Nagle causes no delays in this common scenario.

If you're not seeing the data sent by 250 milliseconds (the maximum delay caused by Nagle in the worst possible scenario), then there is something else wrong.

顾挽 2024-11-25 08:10:22

您可以设置 NoDelay 属性(我认为这就是禁用 Nagle)。

You could set the NoDelay property (I think it's what disables Nagle).

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