ns2相关学习——tcl脚本编写(1)
2017-03-22 13:08
357 查看
新建一个仿真实例:
set ns [new Simulator]
为了让nam文件和trace文件有地方可以依托,我们要打开.nam文件进行写入,并且使用句柄nf
set nf [open out.nam w]
$ns namtrace-all $nf
设置拓扑图
1、设置节点的脚本语言:建了两个节点,叫n0,n1
set n0 [$ns node]
set n1 [$ns node]
2、建立一个链接
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
该行告诉模拟器对象将节点n0和n1用带宽1Megabit的双工链路连接,延迟10ms、DropTail队列
发送数据
1、为了能真正的发送数据,要设置“代理”——n0发送数据的代理和n1接收数据的代理
首先,设置发送数据的代理
#Create a UDP agent and attach it to node n0
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
脚本创建一个UDP代理并将其附加到节点n0,然后将一个CBR流量生成器附加到UDP代理。数据包大小被设置为500字节,并且每0.005秒发送一个数据包(即每秒200个数据包)。
然后 设置接收数据的代理
set null0 [new Agent/Null]
$ns attach-agent $n1 $null0
以上脚本创建一个Null代理,作为流量接收器并将其附加到节点n1。
为了能互相发送数据,要将两个代理连接起来,使用以下语句:
$ns connect $udp0 $null0
然后,我们要告诉CBR(发送数据的那个)什么时候开始发送,什么时候结束发送
$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"
上面语句就是说,在0.5s的时候开始发送,在4.5s的时候结束发送
下面是整个完整的简单的TCL脚本语言:
#Create a simulator object
set ns [new Simulator]
#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Execute nam on the trace file
exec nam out.nam &
exit 0
}
#Create two nodes
set n0 [$ns node]
set n1 [$ns node]
#Create a duplex link between the nodes
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
#Create a UDP agent and attach it to node n0
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
4000
#Create a Null agent (a traffic sink) and attach it to node n1
set null0 [new Agent/Null]
$ns attach-agent $n1 $null0
#Connect the traffic source with the traffic sink
$ns connect $udp0 $null0
#Schedule events for the CBR agent
$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Run the simulation
$ns run
set ns [new Simulator]
为了让nam文件和trace文件有地方可以依托,我们要打开.nam文件进行写入,并且使用句柄nf
set nf [open out.nam w]
$ns namtrace-all $nf
设置拓扑图
1、设置节点的脚本语言:建了两个节点,叫n0,n1
set n0 [$ns node]
set n1 [$ns node]
2、建立一个链接
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
该行告诉模拟器对象将节点n0和n1用带宽1Megabit的双工链路连接,延迟10ms、DropTail队列
发送数据
1、为了能真正的发送数据,要设置“代理”——n0发送数据的代理和n1接收数据的代理
首先,设置发送数据的代理
#Create a UDP agent and attach it to node n0
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
脚本创建一个UDP代理并将其附加到节点n0,然后将一个CBR流量生成器附加到UDP代理。数据包大小被设置为500字节,并且每0.005秒发送一个数据包(即每秒200个数据包)。
然后 设置接收数据的代理
set null0 [new Agent/Null]
$ns attach-agent $n1 $null0
以上脚本创建一个Null代理,作为流量接收器并将其附加到节点n1。
为了能互相发送数据,要将两个代理连接起来,使用以下语句:
$ns connect $udp0 $null0
然后,我们要告诉CBR(发送数据的那个)什么时候开始发送,什么时候结束发送
$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"
上面语句就是说,在0.5s的时候开始发送,在4.5s的时候结束发送
下面是整个完整的简单的TCL脚本语言:
#Create a simulator object
set ns [new Simulator]
#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Execute nam on the trace file
exec nam out.nam &
exit 0
}
#Create two nodes
set n0 [$ns node]
set n1 [$ns node]
#Create a duplex link between the nodes
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
#Create a UDP agent and attach it to node n0
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
4000
#Create a Null agent (a traffic sink) and attach it to node n1
set null0 [new Agent/Null]
$ns attach-agent $n1 $null0
#Connect the traffic source with the traffic sink
$ns connect $udp0 $null0
#Schedule events for the CBR agent
$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Run the simulation
$ns run
相关文章推荐
- ns2相关学习——TCL脚本编写(3)
- ns2相关学习——TCL脚本编写(2)
- python 编写管理redmine的相关小脚本,python学习中
- Oracle基础学习四之表的定义及脚本编写二
- FPGA学习笔记6-Quartus II中的TCL脚本
- javascript学习随笔(编写浏览器脚本 Navigator Scripting )
- Shell脚本学习--护眼程序编写心得
- linux shell编写以日期时间为文件名的脚本学习笔记
- NSG2-一个很好用的ns2的tcl脚本自动生成软件
- spider tcl脚本编写
- shell时间,shell创建文件,linux shell编写以日期时间为文件名的脚本学习笔记 (转)
- sql server 2008学习9 编写脚本和批处理
- javascript学习随笔(编写浏览器脚本 Navigator Scripting )
- 编写简单的网卡配置的脚本文件和相关的批处理文件
- NS2中tcl脚本文件分析(转)
- FPGA学习笔记6-Quartus II中的TCL脚本(翻译)
- linux shell 编写屏幕产生颜色的脚本学习笔记
- NS2中tcl脚本文件分析(转)
- QTP 学习笔记--编写翻页脚本
- 工作中编写shell脚本设计到相关信息