因为TCP保证数据包的传递,因此可以被认为是“可靠的”,而UDP不保证任何东西,数据包可能会丢失。在应用程序中使用UDP而不是TCP流传输数据的优势是什么?在什么情况下UDP是更好的选择,为什么?
我假设UDP更快,因为它没有创建和维护流的开销,但如果一些数据从未到达目的地,这不是无关紧要的吗?
因为TCP保证数据包的传递,因此可以被认为是“可靠的”,而UDP不保证任何东西,数据包可能会丢失。在应用程序中使用UDP而不是TCP流传输数据的优势是什么?在什么情况下UDP是更好的选择,为什么?
我假设UDP更快,因为它没有创建和维护流的开销,但如果一些数据从未到达目的地,这不是无关紧要的吗?
当前回答
请参阅Steven的Unix网络编程的22.4节,“何时使用UDP而不是TCP”。
另外,请参阅关于UDP总是比TCP更快的误解的其他SO回答。
史蒂文的话可以总结如下:
使用UDP广播和组播,因为这是你唯一的选择(任何新的应用程序使用组播) 你可以在简单的请求/回复应用中使用UDP,但你需要构建自己的ack、超时和重传 不要使用UDP进行批量数据传输。
其他回答
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是更好的选择[而不是tcp]”
上面有很多很好的答案,但是缺少的是对传输不确定性对TCP性能影响的正式、客观的评估。
随着移动应用程序的大量增长,以及“偶尔连接”或“偶尔断开”的范式,在很难获得连接的情况下,TCP试图维持连接的开销肯定会导致UDP及其“面向消息”的性质的强烈情况。
现在我没有数学/研究/数字,但我制作的应用程序使用ACK/NAK和UDP上的消息编号比使用TCP更可靠,当时连接通常很差,可怜的旧TCP只是花费了时间和客户的金钱来尝试连接。在许多西方国家的地区和农村地区都有这种情况....
在某些情况下,保证数据包的到达并不重要,因此使用UDP是可以的。在其他情况下,UDP比TCP更可取。
你想要使用UDP而不是TCP的一个独特情况是你在另一个协议(例如隧道,虚拟网络等)上建立TCP隧道。如果您在TCP上建立隧道,则每个TCP的拥塞控制将相互干扰。因此,人们通常更喜欢在UDP(或其他无状态协议)上传输TCP。参见TechRepublic文章:理解TCP Over TCP: TCP隧道对端到端吞吐量和延迟的影响。
UDP在需要速度时使用,如果数据包不需要,则使用精度,而TCP在需要精度时使用。
UDP通常比较难,因为你必须以一种不依赖于数据包准确性的方式来编写程序。
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字节。