欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UDP和TCP的优缺点比较

程序员文章站 2024-01-31 20:22:10
...

UDP vs TCP, how much faster is it?

http://*.com/questions/47903/udp-vs-tcp-how-much-faster-is-it

 

其中这段话是重点:

In some applications TCP is faster (better throughput) than UDP.

 

This is the case when doing lots of small writes relative to the MTU size. For example, I read an experiment in which a stream of 300 byte packets was being sent over Ethernet (1500 byte MTU) and TCP was 50% faster than UDP.

 

The reason is because TCP will try and buffer the data and fill a full network segment thus making more efficient use of the available bandwidth.

 

UDP on the other hand puts the packet on the wire immediately thus congesting the network with lots of small packets.

 

You probably shouldn't use UDP unless you have a very specific reason for doing so. Especially since you can give TCP the same sort of latency as UDP by disabling the Nagle algorithm (for example if you're transmitting real-time sensor data and you're not worried about congesting the network with lot's of small packets).

 

之前网上很多老文章都说关闭Nagle算法可以提高tcp通信性能,但是这应该看场景,如果客户端和服务端之前的小包消息频率不高,禁用nagle确实可以提高性能,但是反之:客户端服务端之间大部分时间都不是空闲而是不停收发消息,那么禁用nagle就降低性能了,如果是分布式服务器的应用场景,各个服务器之间用tcp通信,那么肯定不能禁用nagle,毕竟服务器压力较大时内部通信肯定极度频繁。

 

 

相关标签: tcp udp