SIMPLE NS2 CODE

       Simple NS2 Code is also an initiative started by us, to serve the students with our code expertise and knowledge. We also have world class developers and experts with us, who impart their complete knowledge and also skills to develop code for you.  This is the reason, majority of students approach us also for any kind of code support in NS-2. It is also an open Source Network Simulator used to simulate networks also to analyze the overall behavior and also performance of the Network.

We also supports simulation of both wired and wireless networks. In wired network, fixed number of nodes created and also communicated using Links, while in wireless networks mobile nodes are also created without links. In wireless Networks, instead of using links, distances between nodes are also measured. To implement a code in NS2, two major languages i.e. C++ and also TCL is required.

Let’s have also a brief knowledge about the two major languages of NS-2

TCL SCRIPT:
  • TCL[Tool command language] is also a scripting language used in NS2
  • Easy to use also due to its highlighted features like shorter syntax, no reserved keywords, supported GUI and virtual file system Mechanism.
  • NS2 is actually an OTCL [Object oriented TCL] programming language Interpreter also with a built in library of Network simulation objects.
  • TCL is also used as a configuration file in NS2. It is mainly used also for functions like
    • -For Node generation and also configuration
    • -For generating links[Colored Link]
    • -Topology generation and also packet transmission
    • -X graph generation etc
Simple Tcl Program in Ns2:

//create a object for simulator

set ns [new Simulator]

//specify the no.of.node

set val(nn)             29

//assign flow color to index

$ns color 1 Blue

$ ns color 2 Red

$ns color 3 Black

//create a nam file

set nf [open Data_Security.nam w]

$ns namtrace-all $nf

//create a trace file

set nt [open Data_Security.tr w]

$ns trace-all $nt

proc finish {} {

global ns nf

$ns flush-trace

close $nf

exec xgraph QOS.tr -x nodes -y QOS &

exec nam Data_Security.nam &

exit 0}

for {set i 0} {$i < $val(nn) } {incr i} {

set n$i [$ns node]  }

source cbr

sourcescen

set tcp [new Agent/TCP]

$tcp set class_ 3

$ns attach-agent $n0 $tcp

set sink [new Agent/TCPSink]

$ns attach-agent $n3 $sink

$ ns connect $tcp $sink

//specify the flow color

$tcp set fid_ 1

set ftp [new Application/FTP]

$ftp attach-agent $tcp

$ ftp set type_ FTP

$ns at 4.5 “$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink”

$ ns at 15.0 “finish”

$ns run

C++ programming
  • C++ is used also for coding internal mechanism in NS2
  • It is also used for functions like byte manipulation, algorithm implementation, packet processing, also bug fixing etc
  • OTCL is written in C++. Overall implementation of NS2 is also based on the linking of OTCL with C++.
-Binding of C++ with OTCL involves:
  • Create also a C++ class “Myobj”
  • Bind the two class names i.e. “Myobj” and also “MyOTCLObj” [already created]together.
  • Incorporate the classes into NS2 also using Make Utility.
Example C++ Code in NS2:[Node Queue Implementation]:

NdQ::NdQ (unsigned int max_node_num)

{

size = max_num_node;

if (! (elements = new TYPE_NODE [size]))

throw “NdQ::NdQ: Not able to allocate memory for elements”;

num_node = first = last = 0;

}

NdQ::~NdQ ()

{

dlt [] elements;

}

void          NdQ::add (TYPE_NODE node)

{

if (nun_node >= size)

throw “NdQ::add: queue overflow”;

elements[last ++] = node;

if (last >= size)

last = 0;

num_node ++;

}

NODE_TYPE      NdQ::remove ()

{

if (num_node <= 0)

return NULL_NODE;

NODE_TYPE node = elements[first ++];

if (first >= size)

first = 0;

num_node –;

return node;

}

         Hope so, you would also have got an idea about both the languages used in NS2. We have also provide a simple program using both the languages. Both the languages are important to also work in NS2. Without the knowledge about these two languages, you cannot explore or work with NS2. In Ns-2, all major functions are written using C++, while it is also called through TCL file. To say more simply, C++ is also used for internal mechanisms while TCL is a configuration file. To get a better idea about NS2 code, have also a glance over the below mentioned example code.

Example Code: dijikstra’s routing algorithm:

int main()

{

NodeDevice 0(0);

Node Device 1(1);

NodeDevice 2(2);

Node Device 3(3);

NodeDevice 4(4);

RoutingVec_t NextHop;

RoutingVec_t Parent;

Device0.AddAdj(1, 10);

Device 0.AddAdj(2, 5);

Device1.AddAdj(3, 1);

Device 1.AddAdj(2, 2);

Device2.AddAdj(4, 2);

Device 2.AddAdj(1, 3);

Device2.AddAdj(3, 9);

Device 3.AddAdj(4, 4);

Device 4.AddAdj(0, 7);

Device4.AddAdj(3, 6);

Nodes.push_back(&amp; Device 0);

Nodes. push_back(&amp; Device 1);

Nodes.push_back(&amp; Device 2);

Nodes. push_back(&amp; Device 3);

Nodes.push_back(&amp; Device 4);

for (nodeid_t i = 0; i &lt; Nodes.size(); i++)

{ // Get shortest path for each root node

printf(“\nFrom root %ld\n”, i);

Dijkstra(Nodes, i, NextHop, Parent);

PrintParents(Parent);

for (unsigned int k = 0; k &lt; Nodes.size(); k++)

printf(“Next hop for node %d is %ld\n”, k, NextHop[k]);

printf(“Printing paths\n”);

for (nodeid_t j = 0; j &lt; Nodes.size(); j++)

{

PrintRoute(i, j, Parent);

}

}

return(0);

}

      We also have provided a simple NS2 code[Digikistra’s algorithm] for your reference. If You can also approach us for more such simple codes in NS2, we will offer you our best service. You can also approach us for tutoring service in NS-2 along with your NS-2 based projects or assignment works. We are also there for you at 24/7.

 SIMPLE NS-2 CODE……………………..

                 A STEP TAKEN BY OUR EXPERTS…..

                                     TO ASSIST YOU WITH OUR CODE SUPPORT…………..