天天看點

ns2 Tcl腳本程式解析

set ns [new Simulator]  ;#建立一個simulator對象

set tracefd [open no1.tr w]  ;#注意此處的no.1

$ns trace-all $tracefd

set namtracefd [open no1.nam w] ;#注意此處的no.1

$ns namtrace-all $namtracefd

proc finish {} {

     global ns tracefd namtracefd

     $ns flush-trace

     close $tracefd

     close $namtracefd

     exec nam no1.nam &   ;#注意此處no.1

     exit 0

}

#建立兩個node(節點),分别複制n0 和n1

set n0 [$ns node]   

set n1 [$ns node]

#建立一條雙向link(鍊路)  該鍊路的帶寬為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

#建立一個CBR的流量發生器 分組大小為500Byte 發送間隔為5ms 綁定到udp0上

set cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 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 連接配接起來

$ns connect $udp0 $null0

#告知cbr0在0.5s時啟動 在4.5s時停止

$ns at 0.5 "$cbr0 start"

$ns at 4.5 "$cbr0 stop"

#在5s時調用finish過程

$ns at 5.0 "finish"

$ns run

注意無論你給檔案起**.tcl 但是裡面的内容不能變