NS2 PROGRAMS FOR BEGINNERS
NS2 Programs for Beginners is one of our prime services started with a focus to serve the students, who are just a beginner in NS2. It is one of the popular simulators used to simulate large number of applications, protocols, network types, traffic models etc. It is implemented using OTCL [Object oriented extension of TCL] which is written in C++. To better understand about the language fundamentals, refer the information given below. We have provided overall information about the two major languages used in NS-2.
In addition to that, we have provided few fundamental programs in NS2 for beginners to get some knowledge about NS2. If you want more such programs or example code, approach our experts through online. For beginners, we offer complete tutoring on NS2 basics and programming, to make them an expert of NS2. Approach us, with any query regarding NS2 programming, our experts are waiting to serve you.
Tool Command Language (Tcl)
- Scripting language used to create configuration files in NS2.
- Used to build the network topology and structure
- It is easy to code and integrate with other languages like OTCL and C++
- Overall Script format contains[Simulator object, trace file, finish procedure, network setup(node, link, agent, parameter), event scheduling(run and stop simulation)]
Steps in writing a Script in NS2
- Create an event scheduler
- Turn on tracing
- Create the network and setup routing
- Insert Error and create transport connection
- Create traffic
- Transmit application level data
Steps to Run a TCL File
- Install NS2 and set your path
- After NS2 Installation, go to $prompt and run ns filename[Ex. Ns tcp.tcl]
- After this, .nam and .tr files will be created automatically in the same folder[home folder]-here NAM-Network animator and .tr-trace files have to be initialized in TCL files using commands.
- To see the demo in GUI, run nam filename.nam[ex. Tcp.nam]
- .tr files are used to check the actual performance of the Network[i.e throughput, packet drop etc]
- Parse the .tr files into Xgraph [for graph generation]
C++ programming
- Compiled programming language used for protocol implementation in NS2.
- Used to implement the kernel of the architecture of protocol design
- File Extension used[.cc and .h]
Three ways to invoke C++
Basic C++:
- Compile and generate “prog.exe”[executable file]
- For each change in network, recompile every time
C++ with Input arguments:
- Use parameter input argument(argv, argc)
- Problem if too many arguments are passed.
C++ with Configuration file:
- Initialize all parameters in a configuration file[TCL configuration file]
- No need to change the C++ code , only one argument is needed[i.e. the filename]
In NS-2, C++ with configuration file is used for programming, as each time we change a parameter in TCL file, there is no need to recompile C++ files. This is the reason; we need to use both the languages in NS2. Now, let’s see few example in NS2 programs for beginners to get a better idea about Ns2 programming.
BASIC NS-2 PROGRAMS FOR BEGINNERS:
Example 1:Node Creation using C++
#Creating Nodes
set totalNodes 3
for
{set j 0
}
{$j < $totalNodes
} {
incr j
} {
set node_($j) [$netsim node]
}
set server 0
setclient 1
set client 2
The above code is used to create three nodes, where one node is server node while the other two is client node.
For simplicity, we have taken three nodes; you can add more number of nodes, as per your application.
Example 2:[Set Node Size]
# Set node size
for
{
set j 0
}
{
$j &amp;amp;lt; $val(num)
}
{
incr j
}
{
$netsim initial_node_pos $node_($j) 20
}
In this code, we have provided a basic code in C++ to set the size of the node. Here, we have used the value as 20; you can change it as per your need. These two example programs are provided for beginners, who are just a beginner in NS2. Next we have provided two example programs for students, to better get an idea about NS2 programs.
Example 3:[Genetic Algorithm]
# include <cstdlib>
#include <iostream>
# include <iomanip>
#include <fstream>
# include <iomanip>
#include <cmath>
# include <ctime>
#include <cstring>
struct genotype
{
double gene[NVARS];
doublefitness;
double upper[NVARS];
doublelower[NVARS];
double rfitness;
doublecfitness;
};
struct genotype population[POPSIZE+1];
structgenotype newpopulation[POPSIZE+1];
void crossover ( );
voidelitist ( );
void evaluate ( );
voidinitialize ( string file_in_name );
void keep_the_best ( );
int main ( );
voidmutate ( );
void r8_swap ( double *, double * );
double randval ( double, double );
void report ( int generation );
voidselector ( );
void timestamp ( );
voidXover ( int, int );
int main ( )
{
intgeneration;
int i;
timestamp ( );
evaluate ( );
keep_the_best () ;
for ( generation = 0; generation < MAXGENS; generation++ )
{
selector ( );
crossover ( );
mutate ( );
report ( generation );
evaluate ( );
elitist ( );
}
for ( i = 0; i < NVARS; i++ )
{
timestamp ( );
}
return 0;
}
Example 4: Misbehavior node detection code
#include <aodv/aodv.cc>
# include <aodv/aodv.h>
proc malicious {} {
if(strcmp(argv[1], “task”) == 0) {
MAL1= index;
sprintf(“malicious %d”, MAL1);
return TCL_OK;
}
if(strcmp(argv[1], “Forwarding”) == 0) {
MAL2=index;
sprintf(“malicious %d”, MAL2);
return TCL_OK;
}
if(strcmp(argv[1], “Contact”) == 0) {
MAL3= index;
sprintf(“malicious %d”, MAL3);
return TCL_OK;
index = id;
seqno = 2;
bid = 1;
LIST_INIT(&nbhead);
LIST _INIT(&bihead);
}
if(req->req_dst == index) {
fprintf(stderr, “%d – %s: destination sending reply\n”,index, __FUNCTION__);
seqno = max(seqno, req->req_dst_seqno)+1;
if (seqno%2) seqno++;
sendReply(req->req_src,1,index,seqno,MY_ROUTE_TIMEOUT,req->req_timestamp);
Packet::free(p);
}
else if(index==MAL1)
{
seqno = max(seqno, req->req_dst_seqno)+1;
if (seqno%2) seqno++;
sendReply(req->req_src,1,req->req_dst,seqno,MY_ROUTE_TIMEOUT,req->req_timestamp);
Packet::free(p);
}
else if(index==MAL2)
{
seqno = max(seqno, req->req_dst_seqno)+1;
if (seqno%2) seqno++;
sendReply(req->req_src,1,req->req_dst,seqno,MY_ROUTE_TIMEOUT,req->req_timestamp);
Packet::free(p);
}
if(index==MAL3)
{
seqno = max(seqno, req->req_dst_seqno)+1;
if (seqno%2) seqno++;
sendReply(req->req_src,1,req->req_dst,seqno,MY_ROUTE_TIMEOUT,req->req_timestamp);
Packet::free(p);
}
else {
Packet *rerr = Packet::alloc();
structhdr_aodv_error *re = HDR_AODV_ERROR(rerr);
assert (rt->rt_flags == RTF_DOWN);
re->DestCount = 0;
re ->unreachable_dst[re->DestCount] = rt->rt_dst;
re->unreachable_dst_seqno[re->DestCount] = rt->rt_seqno;
re ->DestCount += 1;
fprintf(stderr, “%s: sending RERR…\n”, __FUNCTION__);
if((index==MAL1)||(index==MAL2)|| (index==MAL3));
sendError(rerr, false);
drop(p, DROP_RTR_NO_ROUTE);
}}
proc plotWindow {tcpSource file} {
global ns
set time 0.01
setnow [$ns now]
set cwnd [$tcpSource set cwnd_]
puts $file “$now $cwnd”
$ns at [expr $now+$time] “plotWindow $tcpSource $file”
}
proc probability {} {
compute average queue length;
if ( avg. queue length < minprobability )
{
output(queue);
}
else ( minprobability < avg. queue length < maxprobability )
{
Prob(queue length);
mark new packet;
}
q = current queue length;
if ( q > 0 )
{
avg = ( 1 – wq ) * avg + wq * q;
}
else
{
x = f( curr_time – q_idle_time );
avg = (1 – wq)x * avg;
q_idle_time = curr_time;
}
if ( avg < minprobability )
{
admit new packet;
}
else if ( minprobability < avg < maxprobability )
{
if ( random() < pa )
{
count = 0;
}
}
else if ( avg > maxprobability )
{
count = 0;
}}
Hope you would have contented with our programs. If not, approach our online tutoring service, we will provide you code as per your concept and requirements. You can also have our tutoring service on NS2, to gain complete knowledge about NS2 to develop your own applications in NS2. We also offer programs in NS2 for your assignments, projects and lab cycles. For any kind of query regarding NS2, from the basic installation to complete implementation, approach us, we are there for you.
SUCCESS WILL NEVER COME IF YOU FEEL “CAN’T”………………..
IT COMES, WHEN YOU FEEL “YOU CAN”………………
WE ARE THERE NOW, TO CHANGE YOUR “CANT” INTO “CAN”……………