Tuesday, 18 December 2012

NS2 simulation with TCP and UDP packets

set ns [new Simulator]

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

set nt [open out.tr w]
$ns trace-all $nt

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

set h1 [$ns node]
set r1 [$ns node]
set h2 [$ns node]
set h3 [$ns node]
set r2 [$ns node]
set h4 [$ns node]

$ns duplex-link $h1 $r1 10Mb 20ms DropTail
$ns duplex-link $h2 $r1 10Mb 20ms DropTail
$ns duplex-link $r1 $r2 1.5Mb 20ms DropTail
$ns duplex-link $r2 $h3 10Mb 20ms DropTail
$ns duplex-link $r2 $h4 5Mb 20ms DropTail

$ns duplex-link-op $h1 $r1 orient right-down
$ns duplex-link-op $h2 $r1 orient right-up
$ns duplex-link-op $r1 $r2 orient right
$ns duplex-link-op $r2 $h3 orient right-up
$ns duplex-link-op $r2 $h4 orient right-down

set udp [new Agent/UDP]
$ns attach-agent $h2 $udp

set cbr [new Application/Traffic/CBR]
#$cbr set interval_ 0.0005
$cbr attach-agent $udp

set null [new Agent/Null]
$ns attach-agent $h4 $null

set tcp [new Agent/TCP]
$ns attach-agent $h1 $tcp

set ftp [new Application/FTP]
#$ftp set interval_ 0.0005
$ftp attach-agent $tcp

set sink [new Agent/TCPSink]
$ns attach-agent $h3 $sink

$ns connect $udp $null
$ns connect $tcp $sink

$ns at 0.0 "$cbr start"
$ns at 0.0 "$ftp start"
$ns at 10.0 "finish"

$ns run

2 comments: