【NS2仿真】UDP协议

#
# cbr
#  \    
#  udp                        sink  
#    \                         /
#     n0--------5M 2ms---------n1
#
#

set ns [new Simulator]

set f [open out.tr w]
$ns trace-all $f
set nf [open out.nam w]
$ns namtrace-all $nf

proc finish {} {
    global ns nf f
    $ns flush-trace
    close $f
    close $nf
    exec nam out.nam &
    exit 0
}

#设置并连接两个节点
set n0 [$ns node]
set n1 [$ns node]

$ns duplex-link $n0 $n1 5M 2ms DropTail
$ns duplex-link-op $n0 $n1 orient right

#设置传输层并连接
set udp [new Agent/UDP]
$ns attach-agent $n0 $udp
set null [new Agent/Null]
$ns attach-agent $n1 $null
$ns connect $udp $null

#设置应用层
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp

#设置执行与停止顺序
$ns at 0.01 "$cbr start"
$ns at 0.09 "finish"

$ns run

 

你可能感兴趣的:(【NS2仿真】UDP协议)