Network Simulator-Simulation of Topology

1
210

Here is a network topology. Now we are going to write NS2 program for this topology. This is a 8 node topology with proper orientation at both sides. The nodes are created using the “set” command.  The $ns duplex-link command is used to craft links to the nodes.

NS2-simulator-programs

Schedule Events
cbr1 starts from node1 at 0.1ms
ftp0 starts from node0 at 1.0ms
ftp6 starts from node6 at 1.5ms
ftp0 stops at node 5 at 4.0ms
ftp6 stops at node 7 at 4.2ms
cbr1 stops at node 4 at 4.5ms
Connections
Connect node0 to node5
Connect node6 to node7
Connect node1 to node4

PROCEDURE
STEP 1: Start
STEP 2: Create the simulator object ns for designing the given simulation
STEP 3: Open the trace file and nam file in the write mode
STEP 4: Create the nodes of the simulation using the ‘set’ command
STEP 5: Create links to the appropriate nodes using $ns duplex-link command
STEP 6: Set the orientation for the nodes in the simulation using ‘orient’ command
STEP 7: Create TCP agent for the nodes 0 and 6 and UDP agent for the node1 and attach
these agents to the nodes
STEP 8: The traffic generator used is FTP for both node0 and node6 and CBR for node1
STEP 9: Configure node5 and node7 as TCPSink and node4 as Null and attach it
STEP10: Connect node0 and node5 using ‘connect’ command
STEP11: Connect node6 and node7 using ‘connect’ command
STEP12: Connect node1 and node4 using ‘connect’ command
STEP 13: Setting color for the nodes
STEP 14: Schedule the events for FTP agent ftp0 from node0 at 1.0ms and ftp6 from node6
at 1.5ms and cbr1 from node1 at 0.1ms.
STEP 15: ftp0 stops at node 5 at 4.0ms and ftp6 stops at node 7 at 4.2ms and cbr1 stops at
node 4 at 4.5ms
STEP 16: Schedule the simulation for 5 minutes

Network Simulator

NS2 Topology Code

#create simulator object
set ns [new Simulator]
#open xgraph in write mode
set nr [open tr1.tr w]
$ns trace-all $nr
#open the nam file in write mode
set nf [open tr1.nam w]
$ns namtrace-all $nf
#define a finish procedure
proc finish { } {
global ns nr nf
$ns flush-trace
#close the nam trace file
close $nf
close $nr
exec nam tr1.nam &
exit 0
}
#Creation of Nodes:
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
set n6 [$ns node]
set n7 [$ns node]

#Creation of Links:
$ns duplex-link $n0 $n2 1Mb 10ms DropTail
$ns duplex-link $n6 $n2 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n2 $n3 3Mb 10ms DropTail
$ns duplex-link $n3 $n5 2Mb 10ms DropTail
$ns duplex-link $n3 $n7 2Mb 10ms DropTail
$ns duplex-link $n3 $n4 2Mb 10ms DropTail

#Orientation
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n6 $n2 orient right
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient center
$ns duplex-link-op $n3 $n5 orient right-up
$ns duplex-link-op $n3 $n7 orient right
$ns duplex-link-op $n3 $n4 orient right-down

#Connection and agent
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set ftp [new Application/FTP]
$ftp attach-agent $tcp0

set tcp6 [new Agent/TCP]
$ns attach-agent $n6 $tcp6
set ftp6 [new Application/FTP]
$ftp6 attach-agent $tcp6

set udp1 [new Agent/UDP]
$ns attach-agent $n1 $udp1
set cb1 [new Application/Traffic/CBR]
$cb1 attach-agent $udp1

set sink5 [new Agent/TCPSink]
$ns attach-agent $n5 $sink5
set sink7 [new Agent/TCPSink]
$ns attach-agent $n7 $sink7
set null4 [new Agent/Null]
$ns attach-agent $n4 $null4

$ns connect $tcp0 $sink5
$ns connect $udp1 $null4
$ns connect $tcp6 $sink7

#Setting color for the nodes
$tcp0 set fid_ 2
$ns color 2 Red
$tcp6 set fid_ 1
$ns color 1 Green
$udp1 set fid_ 3
$ns color 3 Blue

#Schedule events for the CBR and FTP agents
$ns at 1.0 “$ftp start”
$ns at 4.0 “$ftp stop”
$ns at 1.5 “$ftp6 start”
$ns at 4.2 “$ftp6 stop”
$ns at 0.1 “$cb1 start”
$ns at 4.5 “$cb1 stop”
$ns at 5.0 “finish”
$ns run

Output

Here is the output for the above program.

ns2-programming-guide

If you are a beginner in NS2 programming, try to stimulate different types of network topologies so that you will get a better idea of node creation.

1 COMMENT

LEAVE A REPLY