To implement Ad Hoc Networks in ns-3 it consists of setup networks where nodes can interact directly with each other without depend on central infrastructure. Here is the procedure to setup a basic Ad hoc network environment in ns-3 using the WiFi and an ad hoc routing protocol like AODV.If you are looking for ad hoc routing protocol guidance contact us.
Step-by-Step Guide to Implement Ad Hoc Networks in ns-3
- Set Up Your Development Environment
- Install ns-3:
- Follow the official ns-3 installation guide.
- Install Required Modules:
- Ensure you have all necessary ns-3 modules installed, such as Internet, Mobility, and WiFi modules.
- Create a Basic Ad Hoc Network Simulation Script
The given below are the sample script to setup a basic Ad Hoc network environment using ns-3
#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/applications-module.h”
#include “ns3/aodv-module.h”
using namespace ns3;
NS_LOG_COMPONENT_DEFINE (“AdHocNetworkExample”);
int main (int argc, char *argv[])
{
// Set simulation parameters
uint32_t numNodes = 10;
double simTime = 20.0; // Simulation time in seconds
CommandLine cmd;
cmd.AddValue(“numNodes”, “Number of nodes”, numNodes);
cmd.AddValue(“simTime”, “Simulation time”, simTime);
cmd.Parse(argc, argv);
// Create nodes
NodeContainer nodes;
nodes.Create(numNodes);
// 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);
// Install the Internet stack on nodes
InternetStackHelper internet;
AodvHelper aodv;
internet.SetRoutingHelper(aodv);
internet.Install(nodes);
// Assign IP addresses to devices
Ipv4AddressHelper ipv4;
ipv4.SetBase(“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = ipv4.Assign(devices);
// Set up mobility
MobilityHelper mobility;
mobility.SetPositionAllocator(“ns3::RandomRectanglePositionAllocator”,
“X”, StringValue(“ns3::UniformRandomVariable[Min=0.0|Max=100.0]”),
“Y”, StringValue(“ns3::UniformRandomVariable[Min=0.0|Max=100.0]”));
mobility.SetMobilityModel(“ns3::RandomWaypointMobilityModel”,
“Speed”, StringValue(“ns3::UniformRandomVariable[Min=1.0|Max=10.0]”),
“Pause”, StringValue(“ns3::ConstantRandomVariable[Constant=2.0]”),
“PositionAllocator”, StringValue(“ns3::RandomRectanglePositionAllocator”));
mobility.Install(nodes);
// Install applications (e.g., UDP echo)
uint16_t port = 9;
UdpEchoServerHelper echoServer(port);
ApplicationContainer serverApps = echoServer.Install(nodes.Get(0));
serverApps.Start(Seconds(1.0));
serverApps.Stop(Seconds(simTime));
UdpEchoClientHelper echoClient(interfaces.GetAddress(0), port);
echoClient.SetAttribute(“MaxPackets”, UintegerValue(320));
echoClient.SetAttribute(“Interval”, TimeValue(Seconds(1.0)));
echoClient.SetAttribute(“PacketSize”, UintegerValue(1024));
ApplicationContainer clientApps;
for (uint32_t i = 1; i < numNodes; ++i) {
clientApps.Add(echoClient.Install(nodes.Get(i)));
}
clientApps.Start(Seconds(2.0));
clientApps.Stop(Seconds(simTime));
// Enable tracing
wifiPhy.EnablePcap(“ad-hoc-network”, devices);
// Run the simulation
Simulator::Stop(Seconds(simTime));
Simulator::Run();
Simulator::Destroy();
return 0;
}
Explanation of the Script
Here is the explanation for the Ad Hoc network process script
- Include Necessary Headers:
- Include headers for ns-3 core, network, internet, WiFi, mobility, applications, and AODV modules.
- Set Simulation Parameters:
- Define the number of nodes and simulation time.
- Create Nodes:
- Create a container for the nodes.
- Set Up WiFi:
- Set up WiFi using WifiHelper, YansWifiPhyHelper, and WifiMacHelper for ad-hoc communication.
- Install Internet Stack:
- Install the Internet stack on the nodes using InternetStackHelper. Use AodvHelper to set up AODV routing protocol.
- Assign IP Addresses:
- Assign IP addresses to the devices using Ipv4AddressHelper.
- Set Up Mobility:
- Define the positions and mobility models for the nodes using MobilityHelper. In this case, RandomWaypointMobilityModel is used to simulate nodes moving randomly.
- Install Applications:
- Install a UDP echo server on the first node and UDP echo clients on the remaining nodes to simulate communication.
- Enable Tracing:
- Enable pcap tracing to capture packet traces for analysis.
- Run the Simulation:
- Set the simulation stop time, run the simulation, and clean up using Simulator::Stop, Simulator::Run, and Simulator::Destroy.
Further Enhancements
Here we provide the future improvements for ad-hoc environment
- Advanced Mobility Models:
- Implement more realistic mobility models, such as models based on real-world movement patterns.
- Different Routing Protocols:
- Implement and evaluate different ad-hoc routing protocols such as DSDV, OLSR, or DSR.
- Quality of Service (QoS):
- Implement QoS mechanisms to prioritize critical data and ensure timely delivery.
- Network Performance Metrics:
- Collect and analyze performance metrics such as throughput, latency, packet delivery ratio, and energy consumption.
- Interference Modeling:
- Model interference and evaluate its impact on network performance, especially in densely deployed ad-hoc networks.
- Fault Tolerance and Resilience:
- Implement and evaluate fault tolerance mechanisms and resilience strategies for ad-hoc communication.
Finally, we have discussed and gained knowledge about ad hoc network in ns-3 environment, we support all kinds of ad hoc network environment