Ns3 Projects for B.E/B.Tech M.E/M.Tech PhD Scholars.  Phone-Number:9790238391   E-mail: ns3simulation@gmail.com

How to Implement wireless routing protocol in ns3

To implement a wireless routing protocol in ns3, by using the built-in wireless routing protocols, like AODV (Ad hoc On-Demand Distance Vector), DSR (Dynamic Source Routing), or OLSR (Optimized Link State Routing). Wireless routing protocol in ns3 implementation are done by us, so if you feel any difficulties then reach out for our support. The steps given below will guide to implement AODV routing protocol in wireless network using ns3.

Step-by-Step Guide to Implement Wireless routing Protocols in ns-3

  1. Set Up Your Environment

Ensure that ns3 is installed.

  1. Create a New ns-3 Script

Create a new simulation script in the scratch directory of your ns3 installation. For example, create a file named wireless-aodv-simulation.cc.

cd ns-3.xx

cd scratch

touch wireless-aodv-simulation.cc

3. Include Necessary Headers

In your wireless-aodv-simulation.cc file, include the necessary headers for your simulation.

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/wifi-module.h”

#include “ns3/mobility-module.h”

#include “ns3/aodv-module.h”

#include “ns3/applications-module.h”

 

using namespace ns3;

4. Set Up the Network Topology

Define the network topology. Here we create a basic wireless network with a few nodes and use AODV as the routing protocol.

int main (int argc, char *argv[])

{

  CommandLine cmd;

  cmd.Parse (argc, argv);

  NodeContainer nodes;

  nodes.Create (10);

  // Set up WiFi

  WifiHelper wifi;

  wifi.SetStandard (WIFI_PHY_STANDARD_80211b);

  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();

  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();

  wifiPhy.SetChannel (wifiChannel.Create ());

  WifiMacHelper wifiMac;

  wifiMac.SetType (“ns3::AdhocWifiMac”);

  NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);

  // Set up mobility model

  MobilityHelper mobility;

  mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,

                                 “MinX”, DoubleValue (0.0),

                                 “MinY”, DoubleValue (0.0),

                                 “DeltaX”, DoubleValue (5.0),

                                 “DeltaY”, DoubleValue (5.0),

                                 “GridWidth”, UintegerValue (5),

                                 “LayoutType”, StringValue (“RowFirst”));

  mobility.SetMobilityModel (“ns3::RandomWalk2dMobilityModel”,

                             “Bounds”, RectangleValue (Rectangle (-50, 50, -50, 50)));

  mobility.Install (nodes);

  // Install the internet stack on nodes

  InternetStackHelper stack;

  AodvHelper aodv;

  stack.SetRoutingHelper (aodv);

  stack.Install (nodes);

  // Assign IP addresses to the devices

  Ipv4AddressHelper address;

  address.SetBase (“10.1.1.0”, “255.255.255.0”);

  Ipv4InterfaceContainer interfaces = address.Assign (devices);

  // Set up applications (e.g., a UDP echo server and client)

  UdpEchoServerHelper echoServer (9);

  ApplicationContainer serverApps = echoServer.Install (nodes.Get (9));

  serverApps.Start (Seconds (1.0));

  serverApps.Stop (Seconds (10.0));

  UdpEchoClientHelper echoClient (interfaces.GetAddress (9), 9);

  echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));

  echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));

  echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));

  ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));

  clientApps.Start (Seconds (2.0));

  clientApps.Stop (Seconds (10.0));

  // Enable tracing

  AsciiTraceHelper ascii;

  wifiPhy.EnableAsciiAll (ascii.CreateFileStream (“wireless-aodv-simulation.tr”));

  wifiPhy.EnablePcapAll (“wireless-aodv-simulation”);

  // Run the simulation

  Simulator::Run ();

  Simulator::Destroy ();

  return 0;

}

5. Build and Run the Simulation

After writing the script, we need to build and run it.

./waf build

./waf –run scratch/wireless-aodv-simulation

6. Analyze the Results

After running the simulation, we can analyze the results using the generated trace files (wireless-aodv-simulation.tr and wireless-aodv-simulation-0-0.pcap).

Complete Example Script for AODV

Here’s a complete example script for reference:

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/wifi-module.h”

#include “ns3/mobility-module.h”

#include “ns3/aodv-module.h”

#include “ns3/applications-module.h”

using namespace ns3;

int main (int argc, char *argv[])

{

  CommandLine cmd;

  cmd.Parse (argc, argv);

  NodeContainer nodes;

  nodes.Create (10);

  // Set up WiFi

  WifiHelper wifi;

  wifi.SetStandard (WIFI_PHY_STANDARD_80211b);

  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();

  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();

  wifiPhy.SetChannel (wifiChannel.Create ());

  WifiMacHelper wifiMac;

  wifiMac.SetType (“ns3::AdhocWifiMac”);

  NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);

  // Set up mobility model

  MobilityHelper mobility;

  mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,

                                 “MinX”, DoubleValue (0.0),

                                 “MinY”, DoubleValue (0.0),

                                 “DeltaX”, DoubleValue (5.0),

                                 “DeltaY”, DoubleValue (5.0),

                                 “GridWidth”, UintegerValue (5),

                                 “LayoutType”, StringValue (“RowFirst”));

  mobility.SetMobilityModel (“ns3::RandomWalk2dMobilityModel”,

                             “Bounds”, RectangleValue (Rectangle (-50, 50, -50, 50)));

  mobility.Install (nodes);

  // Install the internet stack on nodes

  InternetStackHelper stack;

  AodvHelper aodv;

  stack.SetRoutingHelper (aodv);

  stack.Install (nodes);

  // Assign IP addresses to the devices

  Ipv4AddressHelper address;

  address.SetBase (“10.1.1.0”, “255.255.255.0”);

  Ipv4InterfaceContainer interfaces = address.Assign (devices);

  // Set up applications (e.g., a UDP echo server and client)

  UdpEchoServerHelper echoServer (9);

  ApplicationContainer serverApps = echoServer.Install (nodes.Get (9));

  serverApps.Start (Seconds (1.0));

  serverApps.Stop (Seconds (10.0));

  UdpEchoClientHelper echoClient (interfaces.GetAddress (9), 9);

  echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));

  echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));

  echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));

  ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));

  clientApps.Start (Seconds (2.0));

  clientApps.Stop (Seconds (10.0));

  // Enable tracing

  AsciiTraceHelper ascii;

  wifiPhy.EnableAsciiAll (ascii.CreateFileStream (“wireless-aodv-simulation.tr”));

  wifiPhy.EnablePcapAll (“wireless-aodv-simulation”);

  // Run the simulation

  Simulator::Run ();

  Simulator::Destroy ();

  return 0;

}

Overall, we had learnt to implement wireless routing protocol using one of the built-in AODV  (Ad hoc On-Demand Distance Vector) routing protocol for simulation in ns3 environment. On all situation of AODV  (Ad hoc On-Demand Distance Vector) routing protocol we work , get best coding from us for your project.