N*(连接+发送+关闭)与(Nagle禁用+连接+N*发送+关闭),N> 1
我是套接字编程的新手(正如您已经通过我的愚蠢问题弄清楚的那样),但抛开我的羞耻不谈,我正在使用 TCP posix 编写一个程序。我的限制如下:从客户端发送到服务器的消息应以字节流形式读取,虽然我的应用程序性能不高,但消息应尽快传递。我编写了一个 TCP 客户端类,目的是执行以下操作:1 个连接 - 多次发送 - 以及 1 个在流传输结束时关闭。问题是消息不能近乎实时地传递(我假设它等待更大的包裹以获得更好的结果) 吞吐量)在网上做了一些研究后,我发现虽然你可以禁用 Nagle 算法(NA),但这样做是一个非常糟糕的主意。由于我是套接字编程的新手,我不想禁用我不完全理解的功能。所以我只剩下两个(坏?)选项:
- 连接 - 发送 - 关闭每条消息
- 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:
- connect - send- close per message
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就您而言,禁用 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.
@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.