Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

#----------How to write NS-2 program---------- () <-------------> ()

#Step1:- Create the event scheduler (n1)link (n2)

set ns [new Simulator]

#Step2:- (a) Create the nam file

set namfile [open out.nam w]

$ns namtrace-all $namfile

#Step3:- Define the finish procedure

proc finish {} {

global ns namfile

$ns flush-trace

#Close the nam trace file

close $namfile

#Execute nam on the trace file

exec nam out.nam &

exit 0

#Step4:- Create the nodes

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

#Step5:- Create a link between nodes

$ns duplex-link $n1 $n2 1Mb 100ms DropTail

$ns duplex-link $n2 $n3 1Mb 100ms DropTail

#Step6:- Create an agent

set udp [new Agent/UDP]

$ns attach-agent $n1 $udp

set null [new Agent/Null]

$ns attach-agent $n2 $null

$ns connect $udp $null

set udp1 [new Agent/UDP]

$ns attach-agent $n2 $udp1


set null1 [new Agent/Null]

$ns attach-agent $n3 $null1

$ns connect $udp1 $null1

#Step7:- Traffic create:-

set cbr [new Application/Traffic/CBR]

$cbr attach-agent $udp

set cbr1 [new Application/Traffic/CBR]

$cbr1 attach-agent $udp1

#Schedule events for the CBR and FTP agents

$ns at 0.1 "$cbr start"

$ns at 2.5 "$cbr stop"

$ns at 0.25 "$cbr1 start"

$ns at 2.85 "$cbr1 stop"

#Call the finish procedure after 5 seconds of simulation time

$ns at 5.0 "finish"

#Run the simulation

$ns run

You might also like