模拟一个网络环境,该环境中包含两个传输节点s1,s2,路由器r,和资料接收端d.
大概如下图所示:
源代码:
set ns [new Simulator]
$ns color 1 Blue
$ns color 2 Red
set nf [open out.nam w]
$ns namtrace-all $nf
set nd [open out.tr w]
$ns trace-all $nd
proc finish {} {
global ns nf nd
$ns flush-trace
close $nf
close $nd
exec nam out.nam &
exit 0
}
set s1 [$ns node]
set s2 [$ns node]
set r [$ns node]
set d [$ns node]
$ns duplex-link $s1 $r 2Mb 10ms DropTail
$ns duplex-link $s2 $r 2Mb 10ms DropTail
$ns duplex-link $r $d 1.7Mb 20ms DropTail
$ns queue-limit $r $d 10
$ns duplex-link-op $s1 $r orient right-down
$ns duplex-link-op $s2 $r orient right-up
$ns duplex-link-op $r $d orient right
$ns duplex-link-op $r $d queuePos 0.5
set tcp [new Agent/TCP]
$ns attach-agent $s1 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $d $sink
$ns connect $tcp $sink
$tcp set fid_ 1
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
set udp [new Agent/UDP]
$ns attach-agent $s2 $udp
set null [new Agent/Null]
$ns attach-agent $d $null
$ns connect $udp $null
$udp set fid_ 2
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set rate_ 1mb
$cbr set random_ false
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 4.0 "$ftp stop"
$ns at 4.5 "$cbr stop"
$ns at 4.5 "$ns detach-agent $s1 $tcp"
$ns at 4.5 "$ns detach-agent $d $sink"
$ns at 5.0 "finish"
$ns run
程序运行图
out.tr部分记录:
+ 0.1 1 2 cbr 1000 ------- 2 1.0 3.1 0 0
- 0.1 1 2 cbr 1000 ------- 2 1.0 3.1 0 0
+ 0.108 1 2 cbr 1000 ------- 2 1.0 3.1 1 1
- 0.108 1 2 cbr 1000 ------- 2 1.0 3.1 1 1
r 0.114 1 2 cbr 1000 ------- 2 1.0 3.1 0 0
数据格式如下:
r:receive(at to-node) src-addr:node.port
+:enqueue(at queue) dst-addr:node.port
-:dequeue(at queue)
d:drop(at queue)
以上述第一行数据为例,大概就是说有一个封包packet id 为0,资料流id为2,序号为0,长度为1000字节,type为 CBR ,它是从来源端1.0要到目的地3.1,在时间0.1秒的时候,从节点1(s2)进入了节点2(r)的队列中。
to be continued