ns2.23——ns-simple.tcl样例解析
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ns2.23——ns-simple.tcl样例解析相关的知识,希望对你有一定的参考价值。
#Create a simulator object
set ns [new Simulator]
#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
#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 $nf #Close the NAM trace file
exec nam out.nam & #Execute NAM on the trace file
exit 0
}
#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
#Create links between the nodes (双向链接)
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.7Mb 20ms DropTail
Drop Tail(队列长度管理机制,丢尾)
它有点类似于FIFO(先入先出)的存储方式。Drop Tail最大的优点是原理简单。当路由器队列长度达到最大值时,通过丢包来指示拥塞,先到达路由器的分组首先被传输。由于路由器缓存有限,如果包到达时缓存已满,那么路由器就丢弃该分组。一旦发生丢包,发送端立即被告知网络拥塞,从而调整发送速率。这种做法不考虑被丢弃包的重要程度。
#Set Queue Size of link (n2-n3) to 10
$ns queue-limit $n2 $n3 10
#Give node position (for NAM)
$ns duplex-link-op $n0 $n2 orient right-down # n0在n2的-45°
$ns duplex-link-op $n1 $n2 orient right-up # n1在n2的45°
$ns duplex-link-op $n2 $n3 orient right # n2的右边是n3
#队列位置定义了队列与水平的夹角(水平线以上度数为正)
#right-down -45
#right-up 45
#right 0 右
#Monitor the queue for link (n2-n3). (for NAM)
$ns duplex-link-op $n2 $n3 queuePos 0.5
#Setup a TCP connection
set tcp [new Agent/TCP] #创建一个tcp代理
$tcp set class_ 2 #设置tcp流的颜色为红色(n0)
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink] #Sink:接收器(n3)
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
$tcp set fid_ 1 #set IP-layer flow ID
#Setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
#Setup a UDP connection
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
$ns attach-agent $n3 $null
$ns connect $udp $null
$udp set fid_ 2
#Setup a CBR over UDP connection
set cbr [new Application/Traffic/CBR] #创建cbr流量产生器agent
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set rate_ 1mb
$cbr set random_ false
#Schedule 调度cbr和ftp事件
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 4.0 "$ftp stop"
$ns at 4.5 "$cbr stop"
#Detach(分离) tcp and sink agents (not really necessary)
$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Print CBR packet size and interval
puts "CBR packet size = [$cbr set packet_size_]"
puts "CBR interval = [$cbr set interval_]"
#Run the simulation
$ns run
以上是关于ns2.23——ns-simple.tcl样例解析的主要内容,如果未能解决你的问题,请参考以下文章