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

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


当前回答

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

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

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

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

其他回答

这是我最喜欢的问题之一。UDP被误解了。

当你真的想快速地向另一个服务器得到一个简单的答案时,UDP是最好的选择。通常,您希望答案在一个响应包中,并准备实现自己的协议以提高可靠性或重新发送。DNS是这个用例的完美描述。连接设置的成本太高了(然而,DNS 不支持TCP模式以及)。

另一种情况是,当您交付的数据可能会丢失,因为新的数据将取代之前的数据/状态。天气数据、视频流、股票报价服务(不用于实际交易)或游戏数据浮现在脑海中。

另一种情况是,当您正在管理大量的状态时,您希望避免使用TCP,因为操作系统无法处理那么多会话。这在今天是一个罕见的案例。事实上,现在可以使用用户专用的TCP堆栈,以便应用程序编写人员可以对该TCP状态所需的资源进行更细粒度的控制。在2003年之前,UDP是唯一的游戏。

另一种情况是多播流量。UDP可以多播到多个主机,而TCP根本不能这样做。

I work on a product that supports both UDP (IP) and TCP/IP communication between client and server. It started out with IPX over 15 years ago with IP support added 13 years ago. We added TCP/IP support 3 or 4 years ago. Wild guess coming up: The UDP to TCP code ratio is probably about 80/20. The product is a database server, so reliability is critical. We have to handle all of the issues imposed by UDP (packet loss, packet doubling, packet order, etc.) already mentioned in other answers. There are rarely any problems, but they do sometimes occur and so must be handled. The benefit to supporting UDP is that we are able to customize it a bit to our own usage and tweak a bit more performance out of it.

Every network is going to be different, but the UDP communication protocol is generally a little bit faster for us. The skeptical reader will rightly question whether we implemented everything correctly. Plus, what can you expect from a guy with a 2 digit rep? Nonetheless, I just now ran a test out of curiosity. The test read 1 million records (select * from sometable). I set the number of records to return with each individual client request to be 1, 10, and then 100 (three test runs with each protocol). The server was only two hops away over a 100Mbit LAN. The numbers seemed to agree with what others have found in the past (UDP is about 5% faster in most situations). The total times in milliseconds were as follows for this particular test:

1记录 IP: 390760 ms TCP: 416,903毫秒 10个记录 IP: 91,707 ms TCP: 95,662毫秒 100条记录 IP: 29,664 ms TCP: 30,968毫秒

IP和TCP传输的数据总量大致相同。我们在UDP通信方面有额外的开销,因为我们拥有一些与TCP/IP“免费”相同的东西(校验和,序列号等)。例如,Wireshark显示对下一组记录的请求在UDP中是80字节,在TCP中是84字节。

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.

视频流是使用UDP的一个很好的例子。

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