用于一般协议消息交换,可以容忍一定的丢包。UDP比TCP效率高多少?


当前回答

Which protocol performs better (in terms of throughput) - UDP or TCP - really depends on the network characteristics and the network traffic. Robert S. Barnes, for example, points out a scenario where TCP performs better (small-sized writes). Now, consider a scenario in which the network is congested and has both TCP and UDP traffic. Senders in the network that are using TCP, will sense the 'congestion' and cut down on their sending rates. However, UDP doesn't have any congestion avoidance or congestion control mechanisms, and senders using UDP would continue to pump in data at the same rate. Gradually, TCP senders would reduce their sending rates to bare minimum and if UDP senders have enough data to be sent over the network, they would hog up the majority of bandwidth available. So, in such a case, UDP senders will have greater throughput, as they get the bigger pie of the network bandwidth. In fact, this is an active research topic - How to improve TCP throughput in presence of UDP traffic. One way, that I know of, using which TCP applications can improve throughput is by opening multiple TCP connections. That way, even though, each TCP connection's throughput might be limited, the sum total of the throughput of all TCP connections may be greater than the throughput for an application using UDP.

其他回答

I will just make things clear. TCP/UDP are two cars are that being driven on the road. suppose that traffic signs & obstacles are Errors TCP cares for traffic signs, respects everything around. Slow driving because something may happen to the car. While UDP just drives off, full speed no respect to street signs. Nothing, a mad driver. UDP doesn't have error recovery, If there's an obstacle, it will just collide with it then continue. While TCP makes sure that all packets are sent & received perfectly, No errors , so , the car just passes obstacles without colliding. I hope this is a good example for you to understand, Why UDP is preferred in gaming. Gaming needs speed. TCP is preffered in downloads, or downloaded files may be corrupted.

根据我的经验,UDP稍微快一点,但也快不了多少。选择不应该基于性能,而应该基于消息内容和压缩技术。

如果它是一种带有消息交换的协议,我建议使用TCP所带来的轻微性能损失是值得的。你得到了两个端点之间的连接它能提供你所需要的一切。不要尝试在UDP之上创建自己可靠的双向协议,除非你对自己的工作非常非常有信心。

请记住,TCP通常在网络上保存多条消息。如果你想在UDP中实现这一点,如果你想可靠地做到这一点,你将有相当多的工作。你的解决方案要么不太可靠,要么速度较慢,要么工作量巨大。有有效的UDP应用程序,但如果你问这个问题,你的可能不是。

如果不考虑网络情况,只讨论TCP或UDP是没有意义的。 如果两点之间的网络质量非常高,UDP绝对比TCP快,但在其他一些情况下,如GPRS网络,TCP可能比UDP更快,更可靠。

每个TCP连接都需要在数据传输之前进行初始握手。此外,TCP报头包含大量用于不同信号和消息传递检测的开销。对于消息交换,如果失败的可能性很小,UDP可能就足够了。如果必须验证收据,TCP是最好的选择。