NS2 by Example
NS2 by Example
NS2 by Example
NS2 by Example
Network Topology
1
11/12/2009
$ns node
$ d creates the
h new node
d object
bj
Using for loop:
◦ for {set i 0} {$i < 6} {incr i} {
set n($i) [$ns node] }
2
11/12/2009
Creating Flow1(TCP)
Source:
◦ set tcpp [[new Agent/TCP/Reno]
g ]
◦ $tcp set packetsize_ 1000 (set packet size to
1000 bytes)
◦ $ns attach-agent $n1 $tcp (attach it to node 1)
Sink:
◦ set sink [new Agent/TCPSink]
◦ $ns attach-agent $n5 $sink (attach sink to node5)
Connect source and sink
◦ $ns connect $tcp $sink (connect source and sink)
3
11/12/2009
4
11/12/2009
5
11/12/2009
Finish Procedure
Closes the trace files, starts NAM and flushes out
any old values/traces.
◦ proc finish
fi i h {} {
global ns nf f0
$ns flush-trace
#Close the NAM trace file
close $nf
close $f0
#Execute NAM on the trace file
exec nam out.nam &
exit 0
}
6
11/12/2009
Extras
Creating a diagram of your simulation on
NAM
◦ $ns duplex-link-op $n1 $n3 orient right-down
◦ $ns duplex-link-op $n2 $n3 orient right-up
◦ $ns duplex-link-op $n3 $n4 orient right
◦ $ns duplex-link-op $n4 $n5 orient right-up
◦ $ns duplex-link-op $n4 $n6 orient right-down
Extras
Setting up the queue size (link 3-4)
◦ $ns queue-limit
queue limit $n3 $n4 10 (10 packet queue
size)