Skip to main content

NS 2 - Tutorial for beginners 1

Explanation for a Simple Script

Step 1: 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.

Step 2: Then, to set the nam (network animation) file with that 'ns' object and associate with it:

set nf [open out.nam w]

$ns namtrace-all $nf

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.

Step 3: Now the nodes could be set as many as you want, for loop could be used if many nodes are to be made.

set n0 [$ns node]

Step 4: Creating connection for the various nodes with each other with the band width and rate.

$ns duplex-link $n0 $n1 10Mb 10ms DropTail

Step 5: The nodes could be given with various orientations with this option. right, right-up and right down could be used depending on the node.

$ns duplex-link-op $n0 $n1 orient right-up

Step 6: 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.

Step 7: Connecting the FTP application with TCP

set ftp [new Application/FTP]
$ftp attach-agent $tcp

Step 8: Setting TCPSink to the node where the TCP packets are received:

set agent [new Agent/TCPSink]
$ns attach-agent $n3 $sink

Step 9: Connecting TCP and sink(agents) for making the network flow:

$ns connect $tcp $sink

Step 10: 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

Step 11: Routing protocols can be used 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.

 Step 12:   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.
$ns at 0.0 "$ftp start"
$ns at 0.0 "$cbr start"

$ns at 5.0 "finish"

Step 13: 'run' is used to run the whole simulation.

$ns run
Step 14: Save the file as example1.tcl; go to terminal and execute

ns example1.tcl 

 

Comments

Popular posts from this blog

NSG 2.1 Tcl/OTcl Script Generator

Are you a beginner for ns2 network simulator? Are you afraid of Tcl/oTcl script generation? Then there is a tcl script generator, named NSG 2.1 NSG - Network Simulation Generator NSG 2.1 is a java .jar file.  So, this application can run on all platforms (windows/linux/mac os). It deserves the java installed in you pc, prior to working with NSG 2.1. Java must be installed to run NSG2.1. So, initially, java must be installed. How to install java in Windows/ubuntu/mint/debian linux/ OS X ? Step 1 : Go to Terminal and run  java -version   to check the java version installed in your machine. Step 2 :  For Windows, click here  to download the java installer. Then, it is a typical next-next windows executable installation. For ubuntu/mint/debian linux operating systems, run the following commands in that terminal: sudo apt-get install default-jre sudo apt-get install default-jdk sudo apt-get install openjdk-7-jre sudo apt-get install openjdk-7- jdk How to

SUMO installation in linux (Debian/Ubuntu/Mint)

SUMO - S imulation of U rban Mo bility SUMO an open source, portal, microscopic, multi-modal road traffic simulation. It allows for intermodal simulation including pedestrians and comes with a large set of tools for scenario creation.  It allows to simulate as to how a given traffic demand which consists of single vehicles moves through a given road network. The simulator allows to address a large set of traffic management topics. It is purely microscopic: each vehicle is modeled explicitly, has an own route, and moves individually through the network. SUMO Installation Procedure: Installation in Linux and mac OS Step I: Install two pre-requisite packages to build SUMO with GUI. Go to terminal and run: $ sudo apt-get install libgdal1-dev proj libxerces-c2-dev $ sudo apt-get install libfox-1.6-dev libgl1-mesa-dev libglu1-mesa-dev Step II: If you are using Unbuntu 12.04 or older versions, as it doesn't ship with libgdal package, create a symbolic link: $ sudo ln -s /usr/lib/li

getattrib, Setattrib, hasattrib and delattrib in python

# `getattr(object, name[, default])` Function in Python The `getattr(object, name[, default])` function returns the value of a named attribute of an object, where `name` must be a string. If the object has an attribute with the specified `name`, then the value of that attribute is returned. On the other hand, if the object does not have an attribute with `name`, then the value of `default` is returned, or `AttributeError` is raised if `default` is not provided. ```python >>> t = ('This', 'is', 'a', 'tuple') >>> t.index('is') 1 >>> getattr(t, 'index') <built-in method index of tuple object at 0x10c15e680> >>> getattr(t, 'index')('is') 1 ``` when the attribute is not defined, ```python >>> getattr(t, 'len') Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'tuple' object has no attribute 'len