因为TCP保证数据包的传递,因此可以被认为是“可靠的”,而UDP不保证任何东西,数据包可能会丢失。在应用程序中使用UDP而不是TCP流传输数据的优势是什么?在什么情况下UDP是更好的选择,为什么?
我假设UDP更快,因为它没有创建和维护流的开销,但如果一些数据从未到达目的地,这不是无关紧要的吗?
因为TCP保证数据包的传递,因此可以被认为是“可靠的”,而UDP不保证任何东西,数据包可能会丢失。在应用程序中使用UDP而不是TCP流传输数据的优势是什么?在什么情况下UDP是更好的选择,为什么?
我假设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.
其他回答
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和DNS等协议,在这些协议中,无序到达的数据包是可以接受的,数据包的即时传输很重要。
在SNMP中使用它是因为网络管理通常必须在网络处于压力状态时进行,即当可靠的、拥塞控制的数据传输难以实现时。
它用于DNS,因为它不涉及连接建立,从而避免了连接建立延迟。
干杯
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传输没有保证,你只是被告知套接字是否断开,或者数据是否没有到达。否则它会在到达目的地的时候到达目的地。
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“数据包”是发送的完整数据包,你会更好吗?(如果你费心检查长度/内容,你可能会扔掉两个包)
我们知道UDP是一种无连接协议,的确如此
适用于需要简单请求-响应通信的流程。 适用于有内部流动、误差控制的工艺 适用于广泛铸造和多播
具体的例子:
用于SNMP 用于RIP等路由更新协议