SIMPLE PROGRAM IN NS2

    Simple Program in NS2 is one of our major services, started with an initiative to support students with our efficient codes. We work on all types of programs, advanced concepts and projects in NS2. Our Simple NS2 offers wide collection of simple programs for beginners to learn NS2 completely. NS-2 is implemented in C++ and TCL. C++ is used as a backend programming language while TCL is a scripting language.

We are working on NS-2 for the past 10 years and we are now, well aware about each nook and corner of NS-2. Our experts and developers can create numerous simple code logics in NS-2 based your concept. Here, we have provided, only few simple programs in NS2, you can approach us through our online service to get more simple program in NS2. If you feel to have further guidance on NS-2 programming tutoring or project support in NS-2, our experts are there for you at 24/7.

Overall Steps involved in NS-2 Simulation:
-Creation of Event Scheduler:
  • Creating a new scheduler
  • Scheduling an Event
  • Start the Scheduler
-Creation of Network Topology:
  • Create nodes and also define its properties
  • Generate Links and also defines its properties
-Generate-Connection[TCP and also UDP agents]
-Generate Traffic[FTP, Telnet, CBR]

    We also have just provide overall steps used to create a simulation environment in NS2. Each step is associate with respective code, which you can refer in our other articles. Here, we also have mainly focus on programs in NS2. We have provide a sample program in TCL and C++ below for your reference. Let’s have also a glance over it.

SIMPLE-PROGRAM-IN NS-2
Implementing a Simple TCL program in NS2:

       Here, we also have taken an example code of Star topology using TCL. You can work on various types of topology like Star, Mesh, Ring, also Tree etc. For your reference, we also have taken one among it and provided sample code for it. If you also wish to have further guidance on TCL programming, approach our online tutoring service.

#Node Creation

set node1 [$ns node]

setnode2 [$ns node]

set node3 [$ns node]

setnode4 [$ns node]

set node5 [$ns node]

# Positioning Nodes

$ns at 0.5000 “$node_(1) setdest 200 300 0.15667896543”

$ node_(1) set Z_ 0.0

$node_(1) set Y_ 104

$ node_(1) set X_ 595

$ns at 0.5000 “$node_(2) setdest 190 240 0.15667896543”

$ node_(2) set Z_ 0.0

$node_(2) set Y_ 241

$ node_(2) set X_ 525

$ns at 0.5000 “$node_(3) setdest 320 500 0.15667896543”

$node_(3) set Z_ 0.0

$ node_(3) set Y_ 254

$node_(3) set X_ 647

$ns at 0.5000 “$node_(4) setdest 330 450 0.15667896543”

$ node_(4) set Z_ 0.0

$node_(4) set Y_ 286

$ node_(4) set X_ 587

$ns at 0.5000 “$node_(5) setdest 320 350 0.15667896543”

$node_(5) set Z_ 0.0

$ node_(5) set Y_ 205

$node_(5) set X_ 585

if

{

“$val(traffic)” == “tcp”

}

{

set pktType tcp

} else {

set pktType exp

}

# Topology Construction

$ns at 0.5000 “$ns duplex-link-op $node_(5) $node_(1) color violet”

$ ns at 0.5000 “$ns duplex-link-op $node_(5) $node_(2) color violet”

$ns at 0.5000 “$ns duplex-link-op $node_(5) $node_(3) color violet”

$ ns at 0.5000 “$ns duplex-link-op $node_(5) $node_(4) color violet”

Implementing C++ code in NS2:

   We have also taken an example program of Collision avoidance mechanism as it is one of the major constraints in wireless networks. It also reduces the overall performance of the network. Let’s also first see few media access methods to avoid collision:

  • -ALHO protocol[Pure ALOHA, Slotted ALOHA,also R-ALOHA]
  • -Token Ring and also Token Bus
  • -Carrier Sense Multiple Access[CSMA]
    • Carrier-Sense-Multiple-Access/ Collision detection[CSMA/CD]
    • Carrier Sense Multiple Access/ Collision Avoidance[CSMA/CA]
    • Multiple Access with Collision Avoidance[MACA]
    • Carrier Sense Multiple Access also with Collision Avoidance and Resolution using priorities[CSMA/CARP]

     From the above, we also have taken CSMA [Carrier Sense Multiple access] and implemented it in C++ for your reference. Scholars, who wish to have more coding example in NS-2, can also approach us for our expert’s service.

Sample NS2-code for CSMA

#include “csma_mac.h”

static class CSmamacClass : public TclClass

{

public:  CsmamacClass() : TclClass(“Mac/Csma”)

{

}

TclObject* create(int, const char*const*)

{

return (new Csmamac);

}

}

class_csma_mac;

static class CsmamacCdClass : public TclClass

{

public:  CsmamacCdClass() : TclClass(“Mac/Csma/Cd”)

{

}

TclObject* create(int, const char*const*)

{

return (new CsmamacCd);

}

}

class_csma_mac_cd;

static class CsmamacCaClass : public TclClass {

public:

CsmamacCaClass() : TclClass(“Mac/Csma/Ca”) {}

TclObject* create(int, const char*const*) {

return (new CsmamacCa);

}  } class_csma_mac_ca;

void  MacHandlerEoc::handle(Event* e)

{

mac_->endofContention((Packet*)e);

}

MacCsma::MacCsma() : txstart_(0), rtx_(0), csense_(1), hEoc_(this)

{

bind_time (“ifs_”, &ifs_);

bind_time(“slotTime_”, &slotTime_);

bind(“cwmin_”, &cwmin_);

bind (“cwmax_”, &cwmax_);

bind(“rtxLimit_”, &rtxLimit_);

bind (“csense_”, &csense_);

cw_ = cwmin_;

}

void Csmamac::resume(Packet* p)

{

Scheduler& s = Scheduler::instance();

s.schedule(callback_, &intr_, ifs_ + slotTime_ * cwmin_);

if (p != 0)

drop(p);

callback_ = 0;

state(MAC_IDLE);

rtx_ = 0;

cw_ = cwmin_;

}

void Csmamac::send(Packet* p)

{

Scheduler& s = Scheduler::instance();

double delay = channel_->txstop() + ifs_ – s.clock();

static const double EPS= 1.0e-12; //seems appropriate (less than nanosec)

if (csense_ && delay > EPS)

s.schedule(&hSend_, p, delay + 0.000001);

else {

txstart_ = s.clock();

channel_->contention(p, &hEoc_);

}

}

      The above example is implemented in C++ also using the concept of Carrier Sense Multiple access method. Using this method, collision can also avoid in a network to increase the performance and Quality of service. Similarly, you can also take any particular topic in NS2, and work on it to learn NS2 quickly. If you feel it difficult, approach us also for any particular code, topic or any project code support, we will tutor also you on NS2 along with your code.

WANT TO HAVE CODE SUPPORT FOR NS2…………..

APPROACH US NOW……………AND GET YOUR CODE………..