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

How To Implement D2D Communication in NS3

To implement the Device to Device (D2D) communication in ns-3 consists to set up networks, where the devices exchange the information directly with each other without going through central base station or access point. Get ns-3 support from us with best installation guidance. This type of interaction can specifically suitable in this environment like proximity based services and offloading traffic from cellular networks.

Here the given below steps are the procedures to how to set up a D2D communication in ns-3 environment.

Prerequisites

  • ns-3 installed on your system.
  • Basic understanding of ns-3 and C++ programming.

Steps to Implement D2D Communication in ns-3

  1. Install ns-3: Ensure you have ns-3 installed. You can download it from the official website and follow the installation instructions.

wget https://www.nsnam.org/release/ns-allinone-3.xx.tar.bz2

tar -xjf ns-allinone-3.xx.tar.bz2

cd ns-allinone-3.xx

./build.py –enable-examples –enable-tests

  1. Include Necessary Headers: Include the required headers for network modules, mobility, internet, and WiFi.

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/mobility-module.h”

#include “ns3/internet-module.h”

#include “ns3/wifi-module.h”

#include “ns3/applications-module.h”

  1. Create Network Topology: Create nodes representing the devices in the D2D communication network.

NodeContainer devices;

devices.Create(2); // Create 2 nodes for D2D communication

  1. Set Up Mobility Model: Configure the mobility model to simulate the placement and movement of the devices.

MobilityHelper mobility;

mobility.SetPositionAllocator(“ns3::GridPositionAllocator”,

                              “MinX”, DoubleValue(0.0),

                              “MinY”, DoubleValue(0.0),

                              “DeltaX”, DoubleValue(5.0),

                              “DeltaY”, DoubleValue(5.0),

                              “GridWidth”, UintegerValue(2),

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

mobility.SetMobilityModel(“ns3::ConstantPositionMobilityModel”);

mobility.Install(devices);

  1. Install WiFi Devices: Install WiFi devices on the nodes to enable direct communication

YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default();

YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default();

wifiPhy.SetChannel(wifiChannel.Create());

WifiHelper wifi;

wifi.SetStandard(WIFI_PHY_STANDARD_80211p); // Use 802.11p for D2D communication

NqosWaveMacHelper wifiMac = NqosWaveMacHelper::Default();

Ssid ssid = Ssid(“ns-3-ssid”);

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

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

  1. Install Internet Stack: Install the internet stack on the nodes.

InternetStackHelper stack;

stack.Install(devices);

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer interfaces = address.Assign(wifiDevices);

  1. Create Applications: Create and install applications to simulate data transmission directly between the devices.

uint16_t port = 9;

OnOffHelper onoff(“ns3::UdpSocketFactory”, Address(InetSocketAddress(interfaces.GetAddress(1), port)));

onoff.SetConstantRate(DataRate(“1Mbps”));

ApplicationContainer apps = onoff.Install(devices.Get(0));

apps.Start(Seconds(1.0));

apps.Stop(Seconds(10.0));

 

PacketSinkHelper sink(“ns3::UdpSocketFactory”, Address(InetSocketAddress(Ipv4Address::GetAny(), port)));

apps = sink.Install(devices.Get(1));

apps.Start(Seconds(0.0));

apps.Stop(Seconds(10.0));

  1. Run the Simulation: Finally, run the simulation and clean up.

Simulator::Stop(Seconds(10.0));

Simulator::Run();

Simulator::Destroy();

Example Complete Script

At present we provide the instances to complete the script for set up the fundamental D2D communication in ns-3 environment that are shown here:

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/mobility-module.h”

#include “ns3/internet-module.h”

#include “ns3/wifi-module.h”

#include “ns3/applications-module.h”

using namespace ns3;

 

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

{

    NodeContainer devices;

    devices.Create(2); // Create 2 nodes for D2D communication

    MobilityHelper mobility;

    mobility.SetPositionAllocator(“ns3::GridPositionAllocator”,

                                  “MinX”, DoubleValue(0.0),

                                  “MinY”, DoubleValue(0.0),

                                  “DeltaX”, DoubleValue(5.0),

                                  “DeltaY”, DoubleValue(5.0),

                                  “GridWidth”, UintegerValue(2),

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

    mobility.SetMobilityModel(“ns3::ConstantPositionMobilityModel”);

    mobility.Install(devices);

    YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default();

    YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default();

    wifiPhy.SetChannel(wifiChannel.Create());

    WifiHelper wifi;

    wifi.SetStandard(WIFI_PHY_STANDARD_80211p); // Use 802.11p for D2D communication

    NqosWaveMacHelper wifiMac = NqosWaveMacHelper::Default();

    Ssid ssid = Ssid(“ns-3-ssid”);

 

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

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

    InternetStackHelper stack;

    stack.Install(devices);

    Ipv4AddressHelper address;

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

    Ipv4InterfaceContainer interfaces = address.Assign(wifiDevices);

    uint16_t port = 9;

    OnOffHelper onoff(“ns3::UdpSocketFactory”, Address(InetSocketAddress(interfaces.GetAddress(1), port)));

    onoff.SetConstantRate(DataRate(“1Mbps”));

    ApplicationContainer apps = onoff.Install(devices.Get(0));

    apps.Start(Seconds(1.0));

    apps.Stop(Seconds(10.0));

    PacketSinkHelper sink(“ns3::UdpSocketFactory”, Address(InetSocketAddress(Ipv4Address::GetAny(), port)));

    apps = sink.Install(devices.Get(1));

    apps.Start(Seconds(0.0));

    apps.Stop(Seconds(10.0));

    Simulator::Stop(Seconds(10.0));

    Simulator::Run();

    Simulator::Destroy();

    return 0;

}

Running the Script

Compile and run the script using the following commands:

./waf configure –enable-examples

./waf build

./waf –run <script-name>

Lastly, we provide the basic knowledge about how to implement the D2D communication in ns-3 environment and additionally we provide all kinds of Device to Device communications assistance.