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

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


当前回答

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

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

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

现在我没有数学/研究/数字,但我制作的应用程序使用ACK/NAK和UDP上的消息编号比使用TCP更可靠,当时连接通常很差,可怜的旧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字节。

UDP具有较低的开销,如前所述,它对视频和音频等流媒体很好,最好只是丢失一个数据包,然后尝试重新发送和追赶。

TCP传输没有保证,你只是被告知套接字是否断开,或者数据是否没有到达。否则它会在到达目的地的时候到达目的地。

A big thing that people forget is that udp is packet based, and tcp is bytestream based, there is no guarantee that the "tcp packet" you sent is the packet that shows up on the other end, it can be dissected into as many packets as the routers and stacks desire. So your software has the additional overhead of parsing bytes back into usable chunks of data, that can take a fair amount of overhead. UDP can be out of order so you have to number your packets or use some other mechanism to re-order them if you care to do so. But if you get that udp packet it arrives with all the same bytes in the same order as it left, no changes. So the term udp packet makes sense but tcp packet doesnt necessarily. TCP has its own re-try and ordering mechanism that is hidden from your application, you can re-invent that with UDP to tailor it to your needs.

UDP更容易在两端编写代码,基本上是因为你不需要建立和维护点到点连接。我的问题是在什么情况下你需要TCP开销?如果你走捷径,比如假设接收到的tcp“数据包”是发送的完整数据包,你会更好吗?(如果你费心检查长度/内容,你可能会扔掉两个包)

There are already many good answers here, but I would like to add one very important factor as well as a summary. UDP can achieve a much higher throughput with the correct tuning because it does not employ congestion control. Congestion control in TCP is very very important. It controls the rate and throughput of the connection in order to minimize network congestion by trying to estimate the current capacity of the connection. Even when packets are sent over very reliable links, such as in the core network, routers have limited size buffers. These buffers fill up to their capacity and packets are then dropped, and TCP notices this drop through the lack of a received acknowledgement, thereby throttling the speed of the connection to the estimation of the capacity. TCP also employs something called slow start, but the throughput (actually the congestion window) is slowly increased until packets are dropped, and is then lowered and slowly increased again until packets are dropped etc. This causes the TCP throughput to fluctuate. You can see this clearly when you download a large file.

因为UDP没有使用拥塞控制,它可以更快,并且经历更少的延迟,因为它不会寻求最大化缓冲区直到丢弃点,也就是说,UDP数据包在缓冲区中花费的时间更少,到达那里的速度更快,延迟更少。由于UDP不采用拥塞控制,但TCP采用拥塞控制,因此它可以从TCP中占用生成UDP流的容量。

UDP仍然容易受到拥塞和数据包丢失的影响,所以你的应用程序必须准备好以某种方式处理这些问题,可能使用重传或错误纠正代码。

结果是UDP可以:

Achieve higher throughput than TCP as long as the network drop rate is within limits that the application can handle. Deliver packets faster than TCP with less delay. Setup connections faster as there are no initial handshake to setup the connection Transmit multicast packets, whereas TCP have to use multiple connections. Transmit fixed size packets, whereas TCP transmit data in segments. If you transfer a UDP packet of 300 Bytes, you will receive 300 Bytes at the other end. With TCP, you may feed the sending socket 300 Bytes, but the receiver only reads 100 Bytes, and you have to figure out somehow that there are 200 more Bytes on the way. This is important if your application transmit fixed size messages, rather than a stream of bytes.

总之,UDP可以用于TCP可以使用的任何类型的应用程序,只要您还实现了适当的重传输机制。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,如果你真的知道你在做什么。UDP在今天是在极其罕见的情况下,但数量(甚至非常有经验)专家谁会试图坚持到处似乎是不成比例。也许他们喜欢自己实现错误处理和连接维护代码。

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