Nagle算法
Nagle算法要求一个TCP连接的通信双方在任意时刻都最多只能发送一个未被确认的TCP报文段,在该TCP报文段的确认到达之前不能发送其他TCP报文段。另一方面,发送方在等待确认的同时收集本端需要发送的微量数据,并在确认到来时以一个TCP报文段将它们全部发出。这样就极大地减少了网络上的微小TCP报文段的数量。该算法的另一个优点在于其自适应性:确认到达得越快,数据也就发送得越快。
TCP socket提供了关闭Nagle算法的接口,可以通过TCP_NODELAY选项决定是否开启该算法。
该算法的伪代码如下:
if there is new data to send
if the window size >= MSS and available data is >= MSS
send complete MSS segment now
else
if there is unconfirmed data still in the pipe
enqueue data in the buffer until an acknowledge is received
else
send data immediately
end if
end if
end if
本文介绍了Nagle算法的基本原理,该算法通过限制TCP连接中未确认报文的数量来减少网络上微小TCP报文段的数量。文章还解释了算法如何根据确认速度调整数据发送速率,并提到了如何在TCPsocket中启用或禁用此算法。

被折叠的 条评论
为什么被折叠?



