N*(连接+发送+关闭)与(Nagle禁用+连接+N*发送+关闭),N> 1

发布于 2024-11-12 06:37:44 字数 484 浏览 0 评论 0原文

我是套接字编程的新手(正如您已经通过我的愚蠢问题弄清楚的那样),但抛开我的羞耻不谈,我正在使用 TCP posix 编写一个程序。我的限制如下:从客户端发送到服务器的消息应以字节流形式读取,虽然我的应用程序性能不高,但消息应尽快传递。我编写了一个 TCP 客户端类,目的是执行以下操作:1 个连接 - 多次发送 - 以及 1 个在流传输结束时关闭。问题是消息不能近乎实时地传递(我假设它等待更大的包裹以获得更好的结果) 吞吐量)在网上做了一些研究后,我发现虽然你可以禁用 Nagle 算法(NA),但这样做是一个非常糟糕的主意。由于我是套接字编程的新手,我不想禁用我不完全理解的功能。所以我只剩下两个(坏?)选项:

  1. 连接 - 发送 - 关闭每条消息
  2. 1 连接 - 发送多次并在禁用 NA 的情况下在最后执行 1 关闭。 当我读到禁用 NA 的后果时,在我看来,每次只是为了发送消息而打开和关闭套接字也是一个昂贵的代价。

还有其他不留套接字的解决方案吗?

谢谢。

I'm new to socket programming (as you already figure out by my silly question), but keeping my shame aside, I'm writing a program using TCP posix. My constrain is the following: The message to be sent from the client to the server,should be read as byte stream and while my application is not high performance, the message should be deliver as soon as possible. I wrote a TCP client class with the intention of doing the following: 1 connect - many send - and 1 close at the end of the streaming. The problem is that the messages does not get deliver in near-real-time (I'm assuming its waiting to have a larger package for better
throughput) After doing some research online, I found that while you can disable the Nagle algorithm (NA), it is s a very bad idea to do so. Since I'm new on socket programming, I don't want to disable features that I don't fully understand. So I'm left with two (bad?) options:

  1. connect - send- close per message
  2. 1 connect - send multiple times and do 1 close at the end with the NA disabled.
    While I read the consequences of disabling the NA, It seems to me that opening and closing a socket every time just to send a message is an expensive price to pay as well.

Are there other solutions without leaving sockets?

Thanks.

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

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

发布评论

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

评论(2

Saygoodbye 2024-11-19 06:37:44

就您而言,禁用 Nagle 正是您想要做的。

请记住,每次调用 write() 都会立即传输您的数据。因此,请确保将整个消息打包在一起,然后在准备发送时调用一次 write() (或 writev());不要使用较小的有效负载重复调用 write(),因为那样会很慢。

像你这样的情况正是他们让你禁用 Nagle 的原因。

In your case, disabling Nagle is exactly what you want to do.

Just remember that every call to write() is going to transmit your data immediately. So be sure you are packing your entire message together and then calling write() (or writev()) once when you are ready to send; do not call write() repeatedly with small payloads because that will be slow.

Situations like yours are exactly why they let you disable Nagle.

听风念你 2024-11-19 06:37:44

@Nemo 为 TCP 提供了很好的建议。

但我建议你看看 UDP。 TCP 可以在数据包丢失期间引入任意延迟,而“TCP 公平性”的工作原理是强制发生数据包丢失。它对于低延迟传输来说并不理想。想要禁用 Nagle 是您使用错误协议的强烈信号。

@Nemo has given great advice for TCP.

But I suggest you look at UDP instead. TCP can introduce arbitrary delay during packet loss, and "TCP fairness" works based on forcing packet loss to occur. It's not ideal for low-latency transfers. Wanting to disable Nagle is a strong sign you're using the wrong protocol.

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