NS2入门学习(二)之tcl脚本示例

来源:互联网 发布:桌面文件夹软件 编辑:程序博客网 时间:2024/05/16 07:28

1、示例分析

tcl语法与python相差不多,以《NS与网络模拟》中的第一个tcl脚本为例,学习如下:

#建立一个Simulator对象的实例并把它赋值给变量nsset ns [new Simulator]#打开一个名为linktrace.tr的文件,用来记录模拟过程的trace数据,变量nf指向该文件set nf [open linktrace.tr w]$ns trace-all $nf#打开一个名为namtrace.nam的文件,用来记录nam的trace数据,变量namtracefd指向该文件set namtracefd [open linktrace.nam w]$ns namtrace-all $namtracefd#Define a 'finish' procedure#建立一个名为finish的过程,用来关闭两个trace文件,并调用nam程序proc finish {} {        global ns nf namtracefd        $ns flush-trace        #Close the NAM trace file        close $nfclose $namtracefd        #Execute NAM on the trace file        exec nam linktrace.nam &        exit 0} #Create four nodes 创建2个节点并分别赋值给n0和n1set n0 [$ns node]set n1 [$ns node]#Create links between the nodes ,设定带宽为1Mbit/s,延时为10ms,队列类型为DropTail$ns duplex-link $n0 $n1 1Mb 10ms DropTail#创建一个UDP Agent并把它绑定到n0上set udp0 [new Agent/UDP]$ns attach-agent $n0 $udp0#Setup a CBR over UDP connection,CBR的流量发生器,设置分组大小为500Bytes,发送间隔为#5ms,然后把它绑定到udp0上#有一条固定的传输速率的联机(Constant Bit Rate,CBR),#CBR应用程序是架构在UDP之上,因此必需在n0使用UDP agent来产生”udp”用来发送UDP封包,在n1上使用Null agent来产生”sink”以接收由n1传送过来的UDP封包,#然后把接收的封包释放。set cbr0 [new Application/Traffic/CBR]$cbr0 set packet_size_ 500$cbr0 set interval_ 0.005$cbr0 attach-agent $udp0#新建一个Null Agent并把它绑定到n1上,Null是一种数据接收器set null0 [new Agent/Null]$ns attach-agent $n1 $null0#将udp0和null0两个Agent连接起来$ns connect $udp0 $null0#告知Simulator对象在0.5s时启动cbr0(开始发送数据),在4.5s时停止cbr0(停止发送数据)#Schedule events for the CBR and FTP agents$ns at 0.5 "$cbr0 start"$ns at 4.5 "$cbr0 stop" #告知Simulator对象在5s时调用finish过程$ns at 5.0 "finish"#开始模拟$ns run
在执行脚本时出现问题如下


所以发现问题是在命名tr与nam文件时,名称需要相同:linktrace.tr     linktrace.nam(nam,这是给NAM用的,用来把模拟的过程用可视化的方式呈现出来,这可以让使用者用”看”的方式去了解封包传送是如何从来源端送到接收端

2、运行之后可以查看到nam仿真过程,如下图


3、在wxjtest.tcl相同目录下回会看到生成的linktrace.tr和linktrace.nam,接下来需要利用awk工具来分析数据并利用图形工具生成图形。

1 0
原创粉丝点击