因为TCP保证数据包的传递,因此可以被认为是“可靠的”,而UDP不保证任何东西,数据包可能会丢失。在应用程序中使用UDP而不是TCP流传输数据的优势是什么?在什么情况下UDP是更好的选择,为什么?
我假设UDP更快,因为它没有创建和维护流的开销,但如果一些数据从未到达目的地,这不是无关紧要的吗?
因为TCP保证数据包的传递,因此可以被认为是“可靠的”,而UDP不保证任何东西,数据包可能会丢失。在应用程序中使用UDP而不是TCP流传输数据的优势是什么?在什么情况下UDP是更好的选择,为什么?
我假设UDP更快,因为它没有创建和维护流的开销,但如果一些数据从未到达目的地,这不是无关紧要的吗?
如果TCP数据包丢失,将重新发送。这对于依赖于以特定顺序实时处理数据的应用程序来说并不方便。
例如视频流,特别是VoIP(例如Skype)。然而,在这些情况下,一个掉落的包并不是什么大问题:我们的感官并不完美,所以我们甚至可能不会注意到。这就是为什么这些类型的应用程序使用UDP而不是TCP。
UDP的“不可靠性”是一种形式主义。传播并不能绝对保证。实际上,他们几乎总是能通过。它们只是在暂停后不被确认和重试。
协商TCP套接字和握手TCP数据包的开销是巨大的。真的很大。没有明显的UDP开销。
最重要的是,您可以轻松地用一些可靠的传输握手来补充UDP,开销比TCP要少。读这个:http://en.wikipedia.org/wiki/Reliable_User_Datagram_Protocol
UDP对于在发布-订阅类型的应用程序中广播信息非常有用。IIRC, TIBCO大量使用UDP来通知状态变化。
任何其他类型的单向“重要事件”或“日志记录”活动都可以用UDP包很好地处理。您希望在不构造整个套接字的情况下发送通知。你不期望从不同的听众那里得到任何回应。
系统“心跳”或“我还活着”消息也是一个不错的选择。错过一个不是危机。(连续)少了半打就是。
在某些情况下,如果丢失一些数据不会完全破坏正在传输的数据,则应该使用UDP而不是TCP。它的很多应用都是在实时应用中,比如游戏(例如FPS,你不需要知道每个玩家在特定时间的位置,如果你丢失了一些数据包,新的数据会正确地告诉你玩家在哪里),以及实时视频流(一个损坏的帧不会破坏观看体验)。
UDP是一种无连接协议,用于SNMP和DNS等协议,在这些协议中,无序到达的数据包是可以接受的,数据包的即时传输很重要。
在SNMP中使用它是因为网络管理通常必须在网络处于压力状态时进行,即当可靠的、拥塞控制的数据传输难以实现时。
它用于DNS,因为它不涉及连接建立,从而避免了连接建立延迟。
干杯
UDP can be used when an app cares more about "real-time" data instead of exact data replication. For example, VOIP can use UDP and the app will worry about re-ordering packets, but in the end VOIP doesn't need every single packet, but more importantly needs a continuous flow of many of them. Maybe you here a "glitch" in the voice quality, but the main purpose is that you get the message and not that it is recreated perfectly on the other side. UDP is also used in situations where the expense of creating a connection and syncing with TCP outweighs the payload. DNS queries are a perfect example. One packet out, one packet back, per query. If using TCP this would be much more intensive. If you dont' get the DNS response back, you just retry.
这是我最喜欢的问题之一。UDP被误解了。
当你真的想快速地向另一个服务器得到一个简单的答案时,UDP是最好的选择。通常,您希望答案在一个响应包中,并准备实现自己的协议以提高可靠性或重新发送。DNS是这个用例的完美描述。连接设置的成本太高了(然而,DNS 不支持TCP模式以及)。
另一种情况是,当您交付的数据可能会丢失,因为新的数据将取代之前的数据/状态。天气数据、视频流、股票报价服务(不用于实际交易)或游戏数据浮现在脑海中。
另一种情况是,当您正在管理大量的状态时,您希望避免使用TCP,因为操作系统无法处理那么多会话。这在今天是一个罕见的案例。事实上,现在可以使用用户专用的TCP堆栈,以便应用程序编写人员可以对该TCP状态所需的资源进行更细粒度的控制。在2003年之前,UDP是唯一的游戏。
另一种情况是多播流量。UDP可以多播到多个主机,而TCP根本不能这样做。
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是可以的。在其他情况下,UDP比TCP更可取。
你想要使用UDP而不是TCP的一个独特情况是你在另一个协议(例如隧道,虚拟网络等)上建立TCP隧道。如果您在TCP上建立隧道,则每个TCP的拥塞控制将相互干扰。因此,人们通常更喜欢在UDP(或其他无状态协议)上传输TCP。参见TechRepublic文章:理解TCP Over TCP: TCP隧道对端到端吞吐量和延迟的影响。
当TCP可以工作时,我有点不情愿建议使用UDP。问题是,如果TCP由于某种原因不能工作,因为连接太延迟或拥塞,将应用程序更改为使用UDP不太可能有帮助。一个坏的连接对UDP也不好。TCP在减少拥塞方面已经做得很好了。
我能想到的唯一需要UDP的情况是广播协议。在应用程序涉及两个已知主机的情况下,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字节。
这并不总是明确的。然而,如果您需要保证数据包以正确的顺序无丢失地传递,那么TCP可能是您想要的。
另一方面,UDP适用于传输信息的短数据包,其中信息的顺序不太重要,或者数据可以放入单个数据包中 包。
当您希望向许多用户广播相同的信息时,这种方法也很合适。
其他时候,当您正在发送序列数据时,它是合适的,但如果有些数据丢失了 错过你不太关心的(例如VOIP应用程序)。
有些协议更复杂,因为需要的是TCP的一些(但不是全部)功能,但比UDP提供的功能更多。这就是应用层必须做到的 实现附加功能。在这些情况下,UDP也是合适的(例如,互联网广播,顺序很重要,但不是每个数据包都需要通过)。
它在哪里/可以被使用的例子 1)时间服务器向局域网上的一堆机器广播正确的时间。 2) VOIP协议 3) DNS查找 4)请求局域网服务,例如:where are you? 5)网络电台 还有许多其他的……
在unix上,您可以输入grep udp /etc/services以获得实现的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硬件上,它不会为你提供相同的服务。
请参阅Steven的Unix网络编程的22.4节,“何时使用UDP而不是TCP”。
另外,请参阅关于UDP总是比TCP更快的误解的其他SO回答。
史蒂文的话可以总结如下:
使用UDP广播和组播,因为这是你唯一的选择(任何新的应用程序使用组播) 你可以在简单的请求/回复应用中使用UDP,但你需要构建自己的ack、超时和重传 不要使用UDP进行批量数据传输。
关键的问题是“在什么情况下UDP是更好的选择[而不是tcp]”
上面有很多很好的答案,但是缺少的是对传输不确定性对TCP性能影响的正式、客观的评估。
随着移动应用程序的大量增长,以及“偶尔连接”或“偶尔断开”的范式,在很难获得连接的情况下,TCP试图维持连接的开销肯定会导致UDP及其“面向消息”的性质的强烈情况。
现在我没有数学/研究/数字,但我制作的应用程序使用ACK/NAK和UDP上的消息编号比使用TCP更可靠,当时连接通常很差,可怜的旧TCP只是花费了时间和客户的金钱来尝试连接。在许多西方国家的地区和农村地区都有这种情况....
我们知道UDP是一种无连接协议,的确如此
适用于需要简单请求-响应通信的流程。 适用于有内部流动、误差控制的工艺 适用于广泛铸造和多播
具体的例子:
用于SNMP 用于RIP等路由更新协议
关于这个问题,我所知道的最好的答案之一来自Hacker News的用户zAy0LfpBZLC8mAC。这个答案太好了,我就原原本本地引用它吧。
TCP has head-of-queue blocking, as it guarantees complete and in-order delivery, so when a packet gets lost in transit, it has to wait for a retransmit of the missing packet, whereas UDP delivers packets to the application as they arrive, including duplicates and without any guarantee that a packet arrives at all or which order they arrive (it really is essentially IP with port numbers and an (optional) payload checksum added), but that is fine for telephony, for example, where it usually simply doesn't matter when a few milliseconds of audio are missing, but delay is very annoying, so you don't bother with retransmits, you just drop any duplicates, sort reordered packets into the right order for a few hundred milliseconds of jitter buffer, and if packets don't show up in time or at all, they are simply skipped, possible interpolated where supported by the codec. Also, a major part of TCP is flow control, to make sure you get as much througput as possible, but without overloading the network (which is kinda redundant, as an overloaded network will drop your packets, which means you'd have to do retransmits, which hurts throughput), UDP doesn't have any of that - which makes sense for applications like telephony, as telephony with a given codec needs a certain amount of bandwidth, you can not "slow it down", and additional bandwidth also doesn't make the call go faster. In addition to realtime/low latency applications, UDP makes sense for really small transactions, such as DNS lookups, simply because it doesn't have the TCP connection establishment and teardown overhead, both in terms of latency and in terms of bandwidth use. If your request is smaller than a typical MTU and the repsonse probably is, too, you can be done in one roundtrip, with no need to keep any state at the server, and flow control als ordering and all that probably isn't particularly useful for such uses either. And then, you can use UDP to build your own TCP replacements, of course, but it's probably not a good idea without some deep understanding of network dynamics, modern TCP algorithms are pretty sophisticated. Also, I guess it should be mentioned that there is more than UDP and TCP, such as SCTP and DCCP. The only problem currently is that the (IPv4) internet is full of NAT gateways which make it impossible to use protocols other than UDP and TCP in end-user applications.
比较TCP和UDP,像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是完美的VoIP地址,其中数据包必须发送不考虑其可靠性… 视频聊天是UDP的一个例子(你可以在任何视频聊天期间通过wireshark网络捕获来检查它)。 而且TCP不能与DNS和SNMP协议一起使用。 UDP没有任何开销,而TCP有很多开销