Sunday, 2 November 2014

NS 2 - Tutorial for beginners 1

Explanation for a Simple Script

To start we have to set a few variables like simulator object, trace file and object, nam file and object.

set ns [new Simulator]
This would set the simulator with the simulator object which is to be accessed in the script.

set nf [open out.nam w]
$ns namtrace-all $nf
This would set the nam file (network animation file) with the object and connect to ns.
set tr [open out.tr w]
$ns trace-all $tr
This would set the trace file and would connect to the simulator. The trace file is required to analyze the various packets which are send, received type of application used etc.


set n0 [$ns node]
Now the nodes could be set as many as you want, for loop could be used if many nodes are to be made.
$ns duplex-link $n0 $n1 10Mb 10ms DropTail
The connection for the various nodes with each other with the band width and rate.
$ns duplex-link-op $n0 $n1 orient right-up
The nodes could be given with various orientations with this option. right, right-up and right down could be used depending on the node.
For the application like tcp or udp to run, we need to set two agents and the application which should run in between.
When using tcp, we have ftp as the application and tcpsink as the end agent. connection must be made between tcp and tcpsink, same in udp with cbr and null respectively.
set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp
This would make a tcp agent and connect it with the node.
set ftp [new Application/FTP]
$ftp attach-agent $tcp

Now the ftp is connected with the tcp

set agent [new Agent/TCPSink]
$ns attach-agent $n3 $sink
Now the tcpsink is set to a node where the tcp packets are received.
The tcp and sink (agents) needs to be connected, such that the network flows.
$ns connect $tcp $sink
Similarly, for udp,
set udp [new Agent/UDP]
$ns attach-agent $n2 $udp

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

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

$ns connect $udp $null
We can use the routing protocols in the simulator using rtmodel (to break the link), rtproto (to use the protocol)
$ns rtmodel-at 1.0 down $n1 $n2
$ns rtmodel-at 2.0 up $n1 $n3
For distance vector we could use
$ns rtproto DV
For linkstate we could use
$ns rtproto LS
When all this is done the TCP could be started at some point and could call the finish procedure to end. 
The out.tr file is used to trace the packets. A normal awk command could be used to analyse the packets.
$ns at 0.0 "$ftp start"
$ns at 0.0 "$cbr start"


$ns at 5.0 "finish"
We could also stop the TCP or UDP in between using stop instead of start, hence nam out.nam need to be used if finish is not used.
run is used to run the whole simulation.

$ns run
The file should be saved in .tcl format and should use ns filename.tcl to run

No comments:

Post a Comment