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

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


当前回答

UDP确实有更少的开销,适合做一些事情,比如流式实时数据,如音频或视频,或者在任何情况下,如果数据丢失是ok的。

其他回答

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.

比较TCP和UDP,像UDP这样的无连接协议可以保证速度,但不能保证数据包传输的可靠性。 例如,电子游戏通常不需要可靠的网络,但速度是最重要的,在游戏中使用UDP具有减少网络延迟的优势。

UDP是完美的VoIP地址,其中数据包必须发送不考虑其可靠性… 视频聊天是UDP的一个例子(你可以在任何视频聊天期间通过wireshark网络捕获来检查它)。 而且TCP不能与DNS和SNMP协议一起使用。 UDP没有任何开销,而TCP有很多开销

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

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

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

干杯

We have web service that has thousands of winforms client in as many PCs. The PCs have no connection with DB backend, all access is via the web service. So we decided to develop a central logging server that listens on a UDP port and all the clients sends an xml error log packet (using log4net UDP appender) that gets dumped to a DB table upon received. Since we don't really care if a few error logs are missed and with thousands of client it is fast with a dedicated logging service not loading the main web service.