因为TCP保证数据包的传递,因此可以被认为是“可靠的”,而UDP不保证任何东西,数据包可能会丢失。在应用程序中使用UDP而不是TCP流传输数据的优势是什么?在什么情况下UDP是更好的选择,为什么?
我假设UDP更快,因为它没有创建和维护流的开销,但如果一些数据从未到达目的地,这不是无关紧要的吗?
因为TCP保证数据包的传递,因此可以被认为是“可靠的”,而UDP不保证任何东西,数据包可能会丢失。在应用程序中使用UDP而不是TCP流传输数据的优势是什么?在什么情况下UDP是更好的选择,为什么?
我假设UDP更快,因为它没有创建和维护流的开销,但如果一些数据从未到达目的地,这不是无关紧要的吗?
当前回答
关于这个问题,我所知道的最好的答案之一来自Hacker News的用户zAy0LfpBZLC8mAC。这个答案太好了,我就原原本本地引用它吧。
TCP has head-of-queue blocking, as it guarantees complete and in-order delivery, so when a packet gets lost in transit, it has to wait for a retransmit of the missing packet, whereas UDP delivers packets to the application as they arrive, including duplicates and without any guarantee that a packet arrives at all or which order they arrive (it really is essentially IP with port numbers and an (optional) payload checksum added), but that is fine for telephony, for example, where it usually simply doesn't matter when a few milliseconds of audio are missing, but delay is very annoying, so you don't bother with retransmits, you just drop any duplicates, sort reordered packets into the right order for a few hundred milliseconds of jitter buffer, and if packets don't show up in time or at all, they are simply skipped, possible interpolated where supported by the codec. Also, a major part of TCP is flow control, to make sure you get as much througput as possible, but without overloading the network (which is kinda redundant, as an overloaded network will drop your packets, which means you'd have to do retransmits, which hurts throughput), UDP doesn't have any of that - which makes sense for applications like telephony, as telephony with a given codec needs a certain amount of bandwidth, you can not "slow it down", and additional bandwidth also doesn't make the call go faster. In addition to realtime/low latency applications, UDP makes sense for really small transactions, such as DNS lookups, simply because it doesn't have the TCP connection establishment and teardown overhead, both in terms of latency and in terms of bandwidth use. If your request is smaller than a typical MTU and the repsonse probably is, too, you can be done in one roundtrip, with no need to keep any state at the server, and flow control als ordering and all that probably isn't particularly useful for such uses either. And then, you can use UDP to build your own TCP replacements, of course, but it's probably not a good idea without some deep understanding of network dynamics, modern TCP algorithms are pretty sophisticated. Also, I guess it should be mentioned that there is more than UDP and TCP, such as SCTP and DCCP. The only problem currently is that the (IPv4) internet is full of NAT gateways which make it impossible to use protocols other than UDP and TCP in end-user applications.
其他回答
UDP是完美的VoIP地址,其中数据包必须发送不考虑其可靠性… 视频聊天是UDP的一个例子(你可以在任何视频聊天期间通过wireshark网络捕获来检查它)。 而且TCP不能与DNS和SNMP协议一起使用。 UDP没有任何开销,而TCP有很多开销
如果TCP数据包丢失,将重新发送。这对于依赖于以特定顺序实时处理数据的应用程序来说并不方便。
例如视频流,特别是VoIP(例如Skype)。然而,在这些情况下,一个掉落的包并不是什么大问题:我们的感官并不完美,所以我们甚至可能不会注意到。这就是为什么这些类型的应用程序使用UDP而不是TCP。
UDP的“不可靠性”是一种形式主义。传播并不能绝对保证。实际上,他们几乎总是能通过。它们只是在暂停后不被确认和重试。
协商TCP套接字和握手TCP数据包的开销是巨大的。真的很大。没有明显的UDP开销。
最重要的是,您可以轻松地用一些可靠的传输握手来补充UDP,开销比TCP要少。读这个:http://en.wikipedia.org/wiki/Reliable_User_Datagram_Protocol
UDP对于在发布-订阅类型的应用程序中广播信息非常有用。IIRC, TIBCO大量使用UDP来通知状态变化。
任何其他类型的单向“重要事件”或“日志记录”活动都可以用UDP包很好地处理。您希望在不构造整个套接字的情况下发送通知。你不期望从不同的听众那里得到任何回应。
系统“心跳”或“我还活着”消息也是一个不错的选择。错过一个不是危机。(连续)少了半打就是。
UDP确实有更少的开销,适合做一些事情,比如流式实时数据,如音频或视频,或者在任何情况下,如果数据丢失是ok的。
关键的问题是“在什么情况下UDP是更好的选择[而不是tcp]”
上面有很多很好的答案,但是缺少的是对传输不确定性对TCP性能影响的正式、客观的评估。
随着移动应用程序的大量增长,以及“偶尔连接”或“偶尔断开”的范式,在很难获得连接的情况下,TCP试图维持连接的开销肯定会导致UDP及其“面向消息”的性质的强烈情况。
现在我没有数学/研究/数字,但我制作的应用程序使用ACK/NAK和UDP上的消息编号比使用TCP更可靠,当时连接通常很差,可怜的旧TCP只是花费了时间和客户的金钱来尝试连接。在许多西方国家的地区和农村地区都有这种情况....