loop_in_codes

低调做技术__欢迎移步我的独立博客 codemaro.com 微博 kevinlynx

tcp要点学习-数据发送一

Author : Kevin Lynx

1. 什么是delayed ack algorithm
   delayed ack algorithm也就是<TCP/IP详解>中所谓的"经受时延的确认"(翻译得真饶舌 = =||)。在RFC1122中提到delayed ack
   的概念:
  

  "
      A host that is receiving a stream of TCP data segments can
      increase efficiency 
in both the Internet and the hosts by
      sending fewer than one ACK (acknowledgment) segment per data
      segment received; 
this is known as a "delayed ACK" [TCP:5].
    
"


   我在之前提到过,TCP在收到每一个数据包时,都会发送一个ACK报文给对方,用以告诉对方"我接收到你刚才发送的数据了"。并
   且会在报文的确认号字段中标志希望接收到的数据包。

   但是,如你所想,如果为每一个接收到的报文都发送一个ACK报文,那将会增加网络的负担。于是,为了解决这个问题,delayed
   ack被提出。也就是说,实现了delayed ack的TCP,并不见得会对每一个接收到的数据包发送ACK确认报文。

   实际情况是,TCP延迟发送这个ACK。延迟多久?<TCP/IP详解>中说的是200ms,在RFC1122中说的则是500ms。delayed ack有时候
   还会附加到数据报文段一起发送,如果在延迟时间内有报文段要发送的话,如果没有,那么当延迟时间到时,就单独发送ACK。

   在另一份文档中,作者讲到delayed ack的好处:
   a) to avoid the silly window syndrome;
   b) to allow ACKs to piggyback on a reply frame if one is ready to go when the stack decides to do the ACK;
   c) to allow the stack to send one ACK for several frames, if those frames arrive within the delay period.

   a) 所谓的糊涂窗口综合症(别人都这样翻译的,似乎有点搞笑:D)
   b) 将ACK与将要发送的数据报文一起发送
   c) 一个ack确认多个报文段,如果这几个报文段在延迟时间内到达

2. 什么是Nagle algoritm ?
   简而言之,nagle算法主要目的是减少网络流量,当你发送的数据包太小时,TCP并不立即发送该数据包,而是缓存起来直到数据包
   到达一定大小后才发送。(improving the efficiency of TCP/IP networks by reducing the number of packets that need to
   be sent over the network.)

   关于这个算法,我觉得wikipedia上讲的比较好。具体点说,当上层提交数据给TCP时,TCP觉得你的数据太小了(套用一般的例子,
   如果你要发送1一个字节的数据,当附加上TCP和IP头后,数据包通常就会增加到41字节,那么这显然是低效的),就缓存你的数据,
   当数据缓存到一定长度后,如果之前发送的数据得到了ACK确认且接收方有足够空间容纳数据,就发送这些数据,否则继续等待。

   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 

   
   TCP socket提供了关闭nagle算法的接口,你可以通过TCP_NODELAY选项决定是否开启该算法。不过MSDN上建议不要关闭此算法。如果
   你发送的数据不至于很小的话(<40byte),我也不建议你关闭。

posted on 2008-05-22 15:42 Kevin Lynx 阅读(3096) 评论(1)  编辑 收藏 引用 所属分类: network

评论

# re: tcp要点学习-数据发送一[未登录] 2012-06-06 16:42 春秋十二月

交互式程序如telnet和rlogin是关闭nagle算法的典型应用  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理