Ns2 Coding for Wireless Network is a basic need for scholars working in this domain. Today the world is using different simulator for different domains. Still Ns2 is one of the best simulators preferred by many scholars.  In general, it is used to simulate protocols like TCP, UDP, FTP, HTTP, DSR etc. We can also create new protocol as an innovative approach which can be implemented using OTCL and C++.

Ns2 in Wireless Network:

  • In wireless network, simulation is done using TCL (Tool command Language) script with fixed number of nodes.
  • To start with TCL, we should know basics of Ns2 simulation implementation  i.e

Initialization and exit aspect of simulator:

  • For initialization, we use the command:

                          Set ns [new simulator]

To finish the work, we call finish procedure and type the command:

                          #ns at end time “finish”

How to define network nodes, links, queues and topology:

  •  To create a new node in a network, we use the command

                       Set node number [ns node]

       Like this we have basic code for every data structure using which we have to create nodes, queues, links etc.

Define network agents and applications:

  • This is the part, where we define what protocol we are going to use and how we are going to schedule events.

NAM-Network Animator:

  •  It is a TCL based animation tool used to view the simulation traces and packet traces.

Trace objects:

  • NS simulation creates visualization trace file which registers the entire network events. These are the basis of NS2 coding, which we have to know before we start our implementation.


     Let’s take an example for- Ns2 Coding for Wireless Network. There are plenty of examples which we can use to explain how Ns2 works in wireless network. We have taken here one example code to explain how Ns2 is coded in Wireless networks.


         ACO is generally used to find shortest path in wireless networks. It is based on the concept of real ant behavior. If we see real ants, we can see that it will search for food using the shortest path. While travelling to its source, it will leave certain amount of pheromone to identify the path. Again, it will come back to its destination using the same path. It is used in wireless networks to find shortest path where we use set of artificial ants as PACKETS and simulate the overall process.

        Following code explains about the node selection process for transmitting the packets. Packets (artificial ants) are simulated from source to destination to find the best path.


proc ChooseNextNode {} {

int nSelectedNode=-1;

double dbTotal=0.0;

for (int i=0;i<=N_SPLIT_COUNT;i++)


dbTotal=dbTotal+ g_Trial[m_nMovedNodeCount][i];


double dbTemp=0.0;

if (dbTotal > 0.0)



for (int i=0;i<=N_SPLIT_COUNT;i++)



if (dbTemp < 0.0)





return nSelectedNode;


Initially packets selects random nodes , after which Pheromone value will get updated by all the packets as it reaches the destination. After the updation process, all packets moves in the resulted shortest path.

proc Move {}

int nCityNo=ChooseNextNode();




proc Search {} {


while (m_nMovedNodeCount < N_X_COUNT)






proc RACO_MetaHeuristic {} {

while (not_termination)








         Here, we have given a simple example using ACO for student’s reference. It is just a sample code in Ns2 for wireless networks. Our developers have developed various routing protocols and algorithms in wireless networks. For every concept, we have shown that our output is best using performance metrics. We work on all the latest area in wireless networks using Ns2 simulation. Like ocean has no limit, we never limit our ideas or concept. Our ideas are never ending oceans which no one can find as our ideas are best innovative projects in the world. Students who feel to work in Ns2 are always invited by us, as our guidance can make you to stand out in your career.