zoukankan      html  css  js  c++  java
  • 一个简单的ns2实验全过程

    实验名称:比较tcp和udp的丢包行为

    试验目的
    1. 熟练用ns2做网络仿真试验的整个流程;
    2. 练习写tcl脚本,了解怎么应用http和rtp;
    3. 练习用awk处理trace数据,了解怎么计算丢包率;
    4. 练习用gnuplot绘制曲线图,熟练gnuplot的使用。

    实验步骤
    1。确定网络拓扑。
       一个简单的三个节点的拓扑,两个运行cbr(const-bitrate)应用的发送结点,一个接收结点。一条链路使用tcp链接,一条链路使用udp连接。如图。

    一个简单的ns2实验全过程


    2。写tcl脚本。

    # jiqing 2007-6-5
    # this script is to compare the loss rates of http and rtp.

    set ns [new Simulator]

    #open a nam trace file
    set nf [open out.nam w]
    $ns namtrace-all $nf

    #open a trace file
    set tf [open out.tr w]
    $ns trace-all $tf

    #finish procedure
    proc finish {} {
     global ns nf tf
     $ns flush-trace
     close $nf
     close $tf
     exec ./nam out.nam &
     exit 0
    }

    #create nodes
    set node(http) [$ns node]
    set node(rtp) [$ns node]
    set node(recv) [$ns node]

    #create links
    $ns duplex-link $node(http) $node(recv) 0.9Mb 10ms DropTail
    $ns duplex-link $node(rtp) $node(recv) 0.9Mb 10ms DropTail

    #set queue size
    $ns queue-limit $node(http) $node(recv) 10
    $ns queue-limit $node(rtp) $node(recv) 10

    #relayout nodes
    $ns duplex-link-op $node(http) $node(recv) orient right-down
    $ns duplex-link-op $node(rtp) $node(recv) orient right-up

    #set colors
    $ns color 1 blue
    $ns color 2 red

    #set a tcp connection
    set tcp [new Agent/TCP]
    $ns attach-agent $node(http) $tcp
    set sink [new Agent/TCPSink]
    $ns attach-agent $node(recv) $sink
    $ns connect $tcp $sink
    $tcp set fid_ 1

    #set a cbr above tcp connection
    set cbr(http) [new Application/Traffic/CBR]
    $cbr(http) attach-agent $tcp
    $cbr(http) set type_ CBR
    $cbr(http) set packet_size_ 1000
    $cbr(http) set rate_ 1mb
    $cbr(http) set random_ false

    #set a rtp connection
    set rtp [new Agent/UDP]
    $ns attach-agent $node(rtp) $rtp
    set null [new Agent/Null]
    $ns attach-agent $node(recv) $null
    $ns connect $rtp $null
    $rtp set fid_ 2

    #set a cbr above tcp connection
    set cbr(rtp) [new Application/Traffic/CBR]
    $cbr(rtp) attach-agent $rtp
    $cbr(rtp) set type_ CBR
    $cbr(rtp) set packet_size_ 1000
    $cbr(rtp) set rate_ 1mb
    $cbr(rtp) set random_ false

    #schedule
    $ns at 0.1 "$cbr(http) start"
    $ns at 0.1 "$cbr(rtp) start"
    $ns at 4.0 "$cbr(http) stop"
    $ns at 4.0 "$cbr(rtp) stop"
    $ns at 4.1 "finish"

    $ns run

    3。仿真。在命令提示符下输入ns http_vs_rtp.tcl,回车。仿真结束,会调用nam演示动画。
    4。用awk处理trace数据。
       awk的语法和c很像,不同的是awk中使用变量不用事先声明。一个awk程序的结构如下面所示:
    BEGIN{
     ...
    }
    {
     ...
    }
    END{
     ...
    }
    可见,程序分为三块,BEGIN只在程序进入时执行一次,END只在程序退出前执行一次。而中间的程序块会在处理每行数据时都执行一次。
    这里用awk计算tcp和udp连接的丢包率,具体程序如下:

     

    BEGIN {
     tcp_droped = 0;
     udp_droped = 0;
     last_tcp = -1;
     last_udp = -1;
     total_tcp = 0;
     total_udp = 0;
     i = 0;
    }

    {
     action = $1;
     time = $2;
     type = $5;
     seq_no = $11;

     if(action == "+" && type == "tcp"){
      total_tcp = total_tcp + 1;
     }

     if(action == "+" && type == "cbr"){
      total_udp = total_udp + 1;
     }

     if(action=="r"){
      if(type=="tcp"){
       if(seq_no != last_tcp + 1){
        tcp_droped = tcp_droped + 1;
       }
       last_tcp = seq_no;
      }

      if(type=="cbr"){
       if(seq_no != last_udp + 1){
        udp_droped = udp_droped + 1;
       }
       last_udp = seq_no;
      }

      time_point[i] = time;
      if(total_tcp > 0)
       tcp_loss[i] = tcp_droped / total_tcp * 100;
      else
       tcp_loss[i] = 0;
      if(total_udp > 0)
       udp_loss[i] = udp_droped / total_udp * 100;
      else
       udp_loss[i] = 0;
      i = i + 1;
      
     }
     
    }

    END {
     printf("%.2f %.2f %.2f ",0,0,0);

     for(j=0;j < i; i++){
      printf("%.2f %.2f %.2f ",time_point[j], tcp_loss[j], udp_loss[j]);
     }
    }

    awk程序写好后,在命令行下输入awk -f measure_loss.awk > http_rtp.txt,将把printf输出的结果写入http_rtp.txt。参见http_rtp.txt。

    。。。。。。。

    0.91 6.45 0.00
    0.91 6.45 0.00
    0.92 6.38 0.00
    0.92 6.38 0.00
    0.92 6.38 0.00
    0.93 6.32 0.96
    0.93 6.32 0.96
    0.93 6.32 0.95
    0.94 6.25 0.95
    0.94 6.25 0.94
    0.94 6.25 0.94
    0.95 6.19 0.94
    0.95 6.19 0.93
    0.95 6.19 0.93
    0.95 6.12 0.93
    0.96 6.12 0.93

    。。。。。。。

    5。用gnuplot画图。
    怎么进入和退出gnuplot?
    开启xserver(运行startxwin.bat)后,输入gnuplot,将进入交互式的画图。如果要退出,输入quit。

    http_rtp.txt中有三列数据,分别是时间、tcp丢包率、udp丢包率。
    怎么控制gnuplot选择其中两列作为x,y轴的数据呢?
    plot "filename" using 2将会把第二列作为y轴的数据,第一列默认是x轴的数据。
    plot "filename" using 2:3将把第二列作为x轴的数据,第三列作为y轴的数据。

    怎么在一张图中绘制多条线?
    多个数据源之间用逗号隔开就可以了,如:
    plot "filename1" using 2, "filename2" using 2:3, ...

    怎么把图输出为图片?
    set terminal gif //设置输出格式
    set output "http_rtp.gif" //设置输出文件名
    replot  //输出

    怎么为图片增加说明?
    set title "title" //设置标题
    set xlabel "xlabel" //设置x轴说明
    set ylabel "ylabel" //设置y轴说明

    一个简单的ns2实验全过程

    实验中遇到的困难及解决办法:
    1。问题:不知道怎么使用rtp agent。
       解决:找不到关于这方面的资料,先用udp代替。
    2。问题:不知道怎么使用http
       解决:关于http的使用ns-2手册 39.7web cache一节,可供借鉴。但是建立一个http应用比较麻烦,需要http/server, http/client, http/cache。这里用cbr来代替。


    实验结果
    1。当链路带宽设为1mb,tcp和udp速率都为1000时,tcp有丢包现象,udp没有丢包现象,这大概是因为tcp有确认报文,而udp没有的缘故。当把链路带宽都设定为0.9Mb,tcp和udp都有严重的丢包现象。可见虽然tcp有重传机制,但在带宽太低的情况下,仍然会丢包。
    2。最终的曲线图说明,tcp的拥塞控制机制发挥了作用,丢包率逐步下降。而udp没有拥塞控制机制,当缓冲队列满后,丢包率迅速上升。

    参考文献:
    1. The ns Manual.
    2. ja2 chung, mark claypool. ns by example.
    3. 柯志亨的个人站点:http://140.116.72.80/~smallko/

  • 相关阅读:
    枚举
    IOS uitableview代理方法
    IOS图片拉伸模式
    IOS单例的设计模式
    圆角属性
    IOS 随机数
    IOS正则表达式
    添加 分类 自动适配图片
    用grep查找文件内容
    Openscada远程配置
  • 原文地址:https://www.cnblogs.com/jiqingwu/p/4042902.html
Copyright © 2011-2022 走看看