ns2相关学习——tcl脚本编写(1)
生活随笔
收集整理的這篇文章主要介紹了
ns2相关学习——tcl脚本编写(1)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
新建一個仿真實例:
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 fileclose $nf #Execute nam on the trace fileexec 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#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脚本编写(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【技巧】Chrome应用技巧
- 下一篇: 常见分数值归一化方法