A complete example of a 6-nodes wired network topology in NS2 with output. Simple6_SN.tcl . The code of ns2 must be design in tcl as shown below.
Simple6_SN.tcl
#step:-1 define global simulator object
set sn [new Simulator]
#step:-2 Define different colors for data flows (for NAM) $sn color 1 green $sn color 2 Red
# Step:-3 Opening the NAM trace file set nt [open simulate.nam w] $sn namtrace-all $nt
#Step:-4 Opening the Trace file set tr [open simulate.tr w] $sn trace-all $tr
#Step:5 Define a ‘finish’ procedure proc finish {} { global sn nt tr $sn flush-trace #Close the NAM trace file close $nt close $tr exec nam simulate.nam & exit 0 }
#Step:-6 Creation of six nodes set na [$sn node] set nb [$sn node] set nc [$sn node] set nd [$sn node] set ne [$sn node] set nf [$sn node]
#Step:-7 Creating links between the nodes $sn duplex-link $na $nc 2Mb 10ms DropTail $sn duplex-link $nb $nc 2Mb 10ms DropTail $sn duplex-link $nc $nd 1.7Mb 20ms DropTail $sn duplex-link $nd $ne 2Mb 10ms DropTail $sn duplex-link $nd $nf 2Mb 10ms DropTail
#Step:-8 Setting Queue Size of link (nc-nd) to 10 $sn queue-limit $nc $nd 10
#Step:-9 Provide node positions to visualize in NAM window $sn duplex-link-op $na $nc orient right-down $sn duplex-link-op $nb $nc orient right-up $sn duplex-link-op $nc $nd orient right $sn duplex-link-op $nd $ne orient right-up $sn duplex-link-op $nd $nf orient right-down
#Step:-10 Monitoring the queue for link (nc-nd) (nd-ne). (for NAM) $sn duplex-link-op $nc $nd queuePos 0.5
#Step:-11 Setting up a TCP connection set tcp [new Agent/TCP] $tcp set class_ 1 $sn attach-agent $na $tcp
#Step:-12 If we setup tcp traffic source then connect it with tcp sink set sink [new Agent/TCPSink] $sn attach-agent $ne $sink $sn connect $tcp $sink $tcp set fid_ 2
#Step:-13 Setting up a FTP over TCP connection set ftp [new Application/FTP] $ftp attach-agent $tcp $ftp set type_ FTP
#Step:-14 Setup a UDP connection set udp [new Agent/UDP] $sn attach-agent $nb $udp
#Step:-15 If we setup udp traffic source then connect it with null set null [new Agent/Null] $sn attach-agent $nf $null $sn connect $udp $null $udp set fid_ 1
#Step:-16 Setting up a CBR over UDP connection set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp $cbr set type_ CBR $cbr set packet_size_ 1000 $cbr set rate_ 1mb $cbr set random_ false
#Step:-17 Scheduling events for the CBR and FTP agents $sn at 0.1 “$cbr start” $sn at 1.0 “$ftp start” $sn at 4.0 “$ftp stop” $sn at 4.5 “$cbr stop” $sn at 5.0 “finish”
#Step:-18 Run the simulation $sn run
|
Execute above file using command
$ ns Simple6_SN.tcl
In Output you will get two file one is Network Animation file (simulate.nam) and second one is Trace File (simulate.tr).
simulate.nam
simulate.tr