用于一般协议消息交换,可以容忍一定的丢包。UDP比TCP效率高多少?
当前回答
我们已经做了一些工作,让程序员可以同时享受这两个世界的好处。
SCTP
它是一个独立的传输层协议,但它可以用作在UDP上提供附加层的库。通信的基本单位是消息(映射到一个或多个UDP包)。有内置的拥塞控制。该协议有许多旋钮和旋钮可以打开
按顺序传递信息 自动重传丢失的消息,与用户定义的参数
如果您的特定应用程序需要其中任何一个。
这样做的一个问题是建立连接是一个复杂的(因此是缓慢的过程)
其他类似的东西
https://en.wikipedia.org/wiki/Reliable_User_Datagram_Protocol
还有一个类似的专利实验
https://en.wikipedia.org/wiki/QUIC
这也试图改进TCP的三重握手,并改变拥塞控制以更好地处理快速线路。
更新2022:Quic和HTTP/3
QUIC(上面提到的)已经通过rfc标准化了,甚至在最初的答案写出来之后就成为了HTTP/3的基础。有各种各样的库,如lucas-clemente/quic-go或microsoft/msquic或谷歌/quiche或mozilla/neqo (web浏览器需要实现这个)。
这些库在UDP传输之上向程序员公开可靠的类tcp流。RFC 9221 (QUIC的不可靠数据报扩展)增加了处理单个不可靠数据包的功能。
其他回答
如果你需要在两个还没有通话的IP之间快速发送消息,那么UDP到达的速度至少要快3倍,通常是5倍。
UDP比TCP快,原因很简单,因为它不存在允许连续数据包流的确认数据包(ACK),而不是使用TCP窗口大小和往返时间(RTT)来确认一组数据包的TCP。
要了解更多信息,我推荐简单但非常容易理解的Skullbox解释(TCP vs. UDP)
每个TCP连接都需要在数据传输之前进行初始握手。此外,TCP报头包含大量用于不同信号和消息传递检测的开销。对于消息交换,如果失败的可能性很小,UDP可能就足够了。如果必须验证收据,TCP是最好的选择。
网络的设置对于任何测量都是至关重要的。如果您通过本地机器上的套接字或与世界的另一端进行通信,则会产生巨大的差异。
我想在讨论中补充三点:
您可以在这里找到一篇关于TCP与UDP的非常好的文章 游戏开发的背景。 此外,iperf (jperf增强iperf与GUI)是一个 这是一个很好的工具,可以通过测量来回答你的问题。 我用Python实现了一个基准测试(参见这个SO问题)。在平均10^6次迭代中,UDP发送8字节的差异大约是1-2微秒。
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.
推荐文章
- 如何通过windows命令行关闭TCP和UDP端口
- UDP和TCP比起来快多少?
- 一个干净、轻量级的Python扭曲的替代品?
- 将主机端口转发到docker容器
- 远程主机强制关闭现有连接
- TCP连接的最大数据包大小
- HTTP 1.1和HTTP 2.0的区别是什么?
- connectexception:拒绝连接
- 互联网上最大的安全UDP包大小是多少
- 关闭vs关闭套接字?
- 如何解决“java.net.BindException: Address already in use: JVM_Bind”错误?
- 增加Linux中TCP/IP连接的最大数量
- 模拟慢速网络连接的网络工具
- Android检查网络连接
- 在Linux上模拟延迟和丢弃的包