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

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


当前回答

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可以非常快,有更少的延迟,在连接的基础上不受拥塞的影响,传输固定大小的数据报,并可用于组播。

其他回答

我们知道UDP是一种无连接协议,的确如此

适用于需要简单请求-响应通信的流程。 适用于有内部流动、误差控制的工艺 适用于广泛铸造和多播

具体的例子:

用于SNMP 用于RIP等路由更新协议

这并不总是明确的。然而,如果您需要保证数据包以正确的顺序无丢失地传递,那么TCP可能是您想要的。

另一方面,UDP适用于传输信息的短数据包,其中信息的顺序不太重要,或者数据可以放入单个数据包中 包。

当您希望向许多用户广播相同的信息时,这种方法也很合适。

其他时候,当您正在发送序列数据时,它是合适的,但如果有些数据丢失了 错过你不太关心的(例如VOIP应用程序)。

有些协议更复杂,因为需要的是TCP的一些(但不是全部)功能,但比UDP提供的功能更多。这就是应用层必须做到的 实现附加功能。在这些情况下,UDP也是合适的(例如,互联网广播,顺序很重要,但不是每个数据包都需要通过)。

它在哪里/可以被使用的例子 1)时间服务器向局域网上的一堆机器广播正确的时间。 2) VOIP协议 3) DNS查找 4)请求局域网服务,例如:where are you? 5)网络电台 还有许多其他的……

在unix上,您可以输入grep udp /etc/services以获得实现的udp协议列表 今天……有几百个。

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可以非常快,有更少的延迟,在连接的基础上不受拥塞的影响,传输固定大小的数据报,并可用于组播。

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

UDP的“不可靠性”是一种形式主义。传播并不能绝对保证。实际上,他们几乎总是能通过。它们只是在暂停后不被确认和重试。

协商TCP套接字和握手TCP数据包的开销是巨大的。真的很大。没有明显的UDP开销。

最重要的是,您可以轻松地用一些可靠的传输握手来补充UDP,开销比TCP要少。读这个:http://en.wikipedia.org/wiki/Reliable_User_Datagram_Protocol

UDP对于在发布-订阅类型的应用程序中广播信息非常有用。IIRC, TIBCO大量使用UDP来通知状态变化。

任何其他类型的单向“重要事件”或“日志记录”活动都可以用UDP包很好地处理。您希望在不构造整个套接字的情况下发送通知。你不期望从不同的听众那里得到任何回应。

系统“心跳”或“我还活着”消息也是一个不错的选择。错过一个不是危机。(连续)少了半打就是。