網路狀況:使用DV routing做環狀模擬,他會根據模擬節點數做最短路徑的搜尋。
由n0到n3有兩種路徑:(1) n0-n1-n2-n3 (2) n0-n6-n5-n4-n3
所以他會走第(1)條路徑最短路徑
在1秒時,將n1和n2的線路中斷
會採取第(2)條路徑
在2秒的時候n1,n2連線恢復, 網路便回到路徑(1)做最短路徑的封包傳輸
#Ring.tcl
# 產生一個模擬的物件
set ns [new Simulator]
# 網路模擬節點使用哪種路由通訊協定。
#(網路模擬節點使用DV(distance vector)指定使用距離向量演算法 路由通訊協定。
$ns rtproto DV
# 模擬的過程用視覺化
# nf 參數 打開nam的記錄檔
set nf [open out.nam w]
$ns namtrace-all $nf
#記錄封包傳送的過程
set nd [open out.tr w]
#用來記錄(trace)封包傳送過程
$ns trace-all $nd
# 定義結束的程序
proc finish {} {
# 取用外部變數ns nf nd
global ns nf nd
# 沖刷使用過的記憶體
$ns flush-trace
#關閉記錄檔
# nf為out.nam
# nd為out.tr
close $nf
close $nd
# 將記錄檔用NAM顯示出來
exec nam out.nam &
exit 0
}
#設定節點
#利用Loop設置節點
for {set i 0} {$i < 7} {incr i} {set n($i) [$ns node] }
#節點間傳輸設定
for {set i 0} {$i < 7} {incr i} {
$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail
}
#將節點和路由器連結
#設置一個LOOP 設立節點 I =0
#duplex-link建立節點與節點間線路
#節點間有1Mbps 的頻寬
#傳遞延遲時間10ms
#採用DorpTail管理機制 (把佇列最尾端的封包丟棄的管理機制
#設定UDP連線
set udp0 [new Agent/UDP]
$ns attach-agent $n(0) $udp0
#封包設定
set cbr0 [new Application/Traffic/CBR]
#封包大小為500 bytes
$cbr0 set packetSize_ 500
#每0.005秒傳送一個封包
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
set null0 [new Agent/Null]
$ns attach-agent $n(3) $null0
$ns connect $udp0 $null0
$ns at 0.5 "$cbr0 start"
# 在1.0秒關閉節點$n (1) $n (2)
$ns rtmodel-at 1.0 down $n(1) $n(2)
# 在2.0秒啟動節點$n (1) $n (2)
$ns rtmodel-at 2.0 up $n(1) $n(2)
$ns at 4.5 "$cbr0 stop"
# simulation time
$ns at 5.0 "finish"
$ns run
留言列表