ns2相关学习——TCL脚本编写(3)
生活随笔
收集整理的這篇文章主要介紹了
ns2相关学习——TCL脚本编写(3)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在這里我們將學習動態(tài)網(wǎng)絡的建立
1、建立拓撲
當節(jié)點很多的時候,我們可以使用循環(huán)的方式來建立拓撲。
for {set i 0} {$i < 7} {incr i} {set n($i) [$ns node] }這里的數(shù)組不需要事先聲明。
2、建立鏈接
這里我們要把7個節(jié)點鏈成一個環(huán)兒,同樣使用循環(huán)的方式建立。
for {set i 0} {$i < 7} {incr i} {$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail }3、關掉鏈接,動態(tài)路由
這里有個hin有意思的東西,我們可以通過修改代碼使得特定的一段鏈路在一段的時間宕機。
$ns rtmodel-at 1.0 down $n(1) $n(2) $ns rtmodel-at 2.0 up $n(1) $n(2)n(1)和n(2)之間的鏈路在1.0~2.0s時候停止工作,發(fā)送出的包皆被丟棄。我們可以使用動態(tài)路由的方式來解決上述問題。 在創(chuàng)建模擬器對象后,在Tcl腳本的開始處添加以下行。
$ns rtproto DV再次啟動仿真,首先看到許多小數(shù)據(jù)包通過網(wǎng)絡運行。 點擊其中一個,會看到他們是'rtProtoDV'數(shù)據(jù)包,用于在節(jié)點之間交換路由信息。 當鏈路在1.0秒鐘再次下降時,路由將被更新,流量將通過節(jié)點6,5和4重新路由。
完整代碼
#Create a simulator object set ns [new Simulator]#Tell the simulator to use dynamic routing $ns rtproto DV#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 seven nodes for {set i 0} {$i < 7} {incr i} {set n($i) [$ns node] }#Create links between the nodes for {set i 0} {$i < 7} {incr i} {$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail }#Create a UDP agent and attach it to node n(0) set udp0 [new Agent/UDP] $ns attach-agent $n(0) $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 n(3) set null0 [new Agent/Null] $ns attach-agent $n(3) $null0#Connect the traffic source with the traffic sink $ns connect $udp0 $null0 #Schedule events for the CBR agent and the network dynamics $ns at 0.5 "$cbr0 start" $ns rtmodel-at 1.0 down $n(1) $n(2) $ns rtmodel-at 2.0 up $n(1) $n(2) $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)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【使用注意】Boolean是final类
- 下一篇: 【转载保存】接口压力测试安装与使用