到达一定大小后才发送。
2. 当应用程序每次发送的数据很小,特别是只发送1个字节,加上TCP和IP头的封装,TCP头占20个字节,IP头也占20个字 节,这时候发一个包是41个字节,效率太低。而nagle算法允许计算机缓冲数据,当数据缓存到一定长度后,如果之前发送 的数据得到了ACK确认且接收方有足够空间容纳数据 (当然也要考虑MSS),就发送这些数据,否则继续等待。
3. TCP socket提供了关闭nagle算法的接口,可以通过TCP_NODELAY选项决定是否开启该算法。
wikipedia上面关于这个算法的资料:h
wikipedia上有一段nagle的伪代码便于理解:
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 