How do I Code Ns3

          We can proceed to code Ns3 for that we have to follow some guidelines to get succeed in that process of coding. Now, let’s get into the step by step process of Ns3 coding.

      As the first process, we have to create the Ns3 main file and store the C++ code file in the scratch folder along with the extension of .cc.

Creation of Ns3 Main File

               For your reference, we have highlighted some codes based on CC in the following with its specifications.

  • C++ code for packet receive from neighbor nodes
TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
Ptr sink = Socket::CreateSocket (node, tid);
InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), port);
sink->Bind (local);
sink->SetRecvCallback (MakeCallback (&Experiment::ReceivePacket, this));
  • C++ code for OLSR based routing configurations
OlsrHelper olsr;
Ipv4StaticRoutingHelper staticRouting;
Ipv4ListRoutingHelper list;
if (enableRouting)
list.Add (staticRouting, 0);
list.Add (olsr, 10);
InternetStackHelper internet;
if (enableRouting)
internet.SetRoutingHelper (list); // has effect on the next Install ()
  • C++ code for randomly select the source and destination
uint32_t totalNodes = c.GetN ();
Ptr uvSrc = CreateObject ();
uvSrc->SetAttribute ("Min", DoubleValue (0));
uvSrc->SetAttribute ("Max", DoubleValue (totalNodes/2 -1));
Ptr uvDest = CreateObject ();
uvDest->SetAttribute ("Min", DoubleValue (totalNodes/2));
uvDest->SetAttribute ("Max", DoubleValue (totalNodes));
for (uint32_t i=0; i < totalNodes/3; i++)
ApplicationSetup (c.Get
(uvSrc->GetInteger ()), c.Get
(uvDest->GetInteger ()), 0, totalTime); }
  • C++ code for include header files
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/mobility-module.h"
#include "ns3/stats-module.h"
#include "ns3/random-variable-stream.h"
#include "ns3/wifi-module.h"
#include "ns3/internet-module.h"
#include "ns3/flow-monitor-helper.h"
#include "ns3/olsr-helper.h"
#include "ns3/ipv4-static-routing-helper.h"
#include "ns3/ipv4-list-routing-helper.h"

            Then, we have to implement the main code through the utilization of the below mentioned commands.

cd /home/research/ns-allinone-3.28/ns-3.28

sudo ./waf –run  CCMain  –vis

Implementation of Ns3 Main Code

           Additionally, we have highlighted the result that is conquered through the implementation of the main file.

Result of Ns3 main Code Execution

                     Throw away all your hesitation and reach us to clarify your issues based on ns3 coding process.