因为TCP保证数据包的传递,因此可以被认为是“可靠的”,而UDP不保证任何东西,数据包可能会丢失。在应用程序中使用UDP而不是TCP流传输数据的优势是什么?在什么情况下UDP是更好的选择,为什么?

我假设UDP更快,因为它没有创建和维护流的开销,但如果一些数据从未到达目的地,这不是无关紧要的吗?


当前回答

UDP can be used when an app cares more about "real-time" data instead of exact data replication. For example, VOIP can use UDP and the app will worry about re-ordering packets, but in the end VOIP doesn't need every single packet, but more importantly needs a continuous flow of many of them. Maybe you here a "glitch" in the voice quality, but the main purpose is that you get the message and not that it is recreated perfectly on the other side. UDP is also used in situations where the expense of creating a connection and syncing with TCP outweighs the payload. DNS queries are a perfect example. One packet out, one packet back, per query. If using TCP this would be much more intensive. If you dont' get the DNS response back, you just retry.

其他回答

我们知道UDP是一种无连接协议,的确如此

适用于需要简单请求-响应通信的流程。 适用于有内部流动、误差控制的工艺 适用于广泛铸造和多播

具体的例子:

用于SNMP 用于RIP等路由更新协议

关键的问题是“在什么情况下UDP是更好的选择[而不是tcp]”

上面有很多很好的答案,但是缺少的是对传输不确定性对TCP性能影响的正式、客观的评估。

随着移动应用程序的大量增长,以及“偶尔连接”或“偶尔断开”的范式,在很难获得连接的情况下,TCP试图维持连接的开销肯定会导致UDP及其“面向消息”的性质的强烈情况。

现在我没有数学/研究/数字,但我制作的应用程序使用ACK/NAK和UDP上的消息编号比使用TCP更可靠,当时连接通常很差,可怜的旧TCP只是花费了时间和客户的金钱来尝试连接。在许多西方国家的地区和农村地区都有这种情况....

UDP是一种无连接协议,用于SNMP和DNS等协议,在这些协议中,无序到达的数据包是可以接受的,数据包的即时传输很重要。

在SNMP中使用它是因为网络管理通常必须在网络处于压力状态时进行,即当可靠的、拥塞控制的数据传输难以实现时。

它用于DNS,因为它不涉及连接建立,从而避免了连接建立延迟。

干杯

UDP can be used when an app cares more about "real-time" data instead of exact data replication. For example, VOIP can use UDP and the app will worry about re-ordering packets, but in the end VOIP doesn't need every single packet, but more importantly needs a continuous flow of many of them. Maybe you here a "glitch" in the voice quality, but the main purpose is that you get the message and not that it is recreated perfectly on the other side. UDP is also used in situations where the expense of creating a connection and syncing with TCP outweighs the payload. DNS queries are a perfect example. One packet out, one packet back, per query. If using TCP this would be much more intensive. If you dont' get the DNS response back, you just retry.

只使用UDP,如果你真的知道你在做什么。UDP在今天是在极其罕见的情况下,但数量(甚至非常有经验)专家谁会试图坚持到处似乎是不成比例。也许他们喜欢自己实现错误处理和连接维护代码。

由于所谓的校验和印记,使用现代网络接口卡,TCP应该会快得多。令人惊讶的是,在快速连接速度下(比如1Gbps),计算校验和对CPU来说是一个很大的负载,所以它被卸载到识别TCP数据包的NIC硬件上,它不会为你提供相同的服务。