2008-01-10

TCP/IP 指数增长和线性增长的编程实现

Views: 20234 | 1 Comment

在 TCP 的慢启动和拥塞控制中, 当发生拥塞时, TCP 进行慢启动, 当慢启动进行到一定阀值后进行拥塞避免. 正如 "TCP/IP 协议详解" 中提到的, "慢启动" 这个术语并不准确, 拥塞窗口在慢启动期间以指数速率增长. 在拥塞避免算法进行时, 拥塞窗口以线性速率增长, 每次加 1.

这两个算法可以用编程语言来实现:

using System;

class Test
{
    static int cwnd1 = 1;
    static int cwnd2 = 1;
    static int ssthresh = 64;

    public static void Main(){
        int step = 20;
        for(int i=0; i<step; i++){
            grow1();
            grow2();
            Console.WriteLine("{0,8}\t{1,8}", cwnd1, cwnd2);
        }
    }

    // 完全指数增长.
    static void grow1(){
        int num = cwnd1;
        for(int n=0; n<num; n++){
            cwnd1 += 1;
        }
    }

    // 先指数增长, 然后加性增长.
    static void grow2(){
        int wnd = cwnd2;
        int ssthresh_var = 0;
        for(int n=0; n<wnd; n++){
            if(cwnd2 < ssthresh){
                cwnd2 += 1;
            }else{
                if (++ssthresh_var == wnd) {
                    ssthresh_var = 0;
                    cwnd2 += 1;
                }
            }
        }
    }
}

TCP/IP 源码的一个疑问:

TCP/IP 详解 1 这样描述: 设置拥塞窗口 cwnd 为1, 发送 1 个报文段, 收到一个 ACK 后, cwnd 加 1, 然后发送 2 个, 收到 2 个 ACK 后, cwnd 加 2, 然后发送 4 个...

这确实是指数增长, 但是, 因为延时 ACK 的存在, 发送的2个报文段可能只收到 1 个 ACK, 所以 cwnd 只增加 1 而不是增加 2. 在延时 ACK 影响的范围内, 这是线性增长.

希望熟悉 TCP/IP 的朋友帮忙解惑, 十分感谢!

Related posts:

  1. C++成员函数作为pthread_create参数
  2. C#封装log4net
  3. 生产者消费者模型中生产者的速度快于消费者时所产生的问题及其解决方法讨论
  4. 关于 C++ 中的函数指针
  5. 世界上最恐怖的厕所及最危险的路
Posted by ideawu at 2008-01-10 09:03:34

One Response to "TCP/IP 指数增长和线性增长的编程实现"

  • RFC3465 似乎对有delay_ack的情况做了优化 开启abc>1 且 确认的byts>=2mss,可以让cwnd有更快的增长? Reply

Leave a Comment