NS3 Broadcast Example

 

csma-broadcast.cc (put this file under scratch)

#include <iostream>

#include <fstream>

#include <string>

#include <cassert>

 

#include "ns3/core-module.h"

#include "ns3/network-module.h"

#include "ns3/csma-module.h"

#include "ns3/applications-module.h"

#include "ns3/internet-module.h"

 

using namespace ns3;

 

NS_LOG_COMPONENT_DEFINE ("CsmaBroadcastExample");

 

int

main (int argc, char *argv[])

{

  LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);

  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);

 

  CommandLine cmd (__FILE__);

  cmd.Parse (argc, argv);

 

  NodeContainer n;

  n.Create (3);

 

  CsmaHelper csma;

  csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));

  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));

  NetDeviceContainer devices = csma.Install (n);

 

  InternetStackHelper internet;

  internet.Install (n);

 

  Ipv4AddressHelper ipv4;

  ipv4.SetBase ("192.168.1.0", "255.255.255.0");

  ipv4.Assign (devices);

 

  UdpEchoServerHelper echoServer (9);

  ApplicationContainer serverApps = echoServer.Install (n.Get (1));

  serverApps.Add(echoServer.Install (n.Get (2)));

  serverApps.Start (Seconds (1.0));

  serverApps.Stop (Seconds (10.0));

 

  UdpEchoClientHelper echoClient (Address (InetSocketAddress (Ipv4Address ("255.255.255.255"), 9)));

  echoClient.SetAttribute ("MaxPackets", UintegerValue (1));

  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));

  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

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

  clientApps.Start (Seconds (2.0));

  clientApps.Stop (Seconds (10.0));

 

  AsciiTraceHelper ascii;

  csma.EnableAsciiAll (ascii.CreateFileStream ("csma-broadcast.tr"));

 

  NS_LOG_INFO ("Run Simulation.");

  Simulator::Run ();

  Simulator::Destroy ();

  NS_LOG_INFO ("Done.");

}

 

Execution

N0 ((192.168.1.1) send out a broadcast packet with destination ip address 255.255.255.255. N1 (192.168.1.2) and N2 (192.168.1.3) receive the packet and then send echo packet back to N0.

 

adhoc-broadcast.cc (put this file under scratch)

#include "ns3/core-module.h"

#include "ns3/global-route-manager.h"

#include "ns3/network-module.h"

#include "ns3/internet-module.h"

#include "ns3/wifi-module.h"

#include "ns3/mobility-module.h"

#include "ns3/netanim-module.h"

#include "ns3/applications-module.h"

 

using namespace ns3;

 

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

{

  LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);

  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);

 

  CommandLine cmd;

  cmd.Parse (argc, argv);

 

  NodeContainer n;

  n.Create (3);

 

  WifiHelper wifi;

  wifi.SetStandard (WIFI_STANDARD_80211a);

  WifiMacHelper wifiMac;

  YansWifiPhyHelper wifiPhy;

  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();

  wifiMac.SetType ("ns3::AdhocWifiMac");

  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",

                                "DataMode", StringValue ("OfdmRate54Mbps"));

  wifiPhy.SetChannel (wifiChannel.Create ());

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

 

  MobilityHelper mobility;

  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();

  positionAlloc->Add (Vector (0.0, 0.0, 0.0));

  positionAlloc->Add (Vector (5.0, 0.0, 0.0));

  positionAlloc->Add (Vector (10.0, 0.0, 0.0));

  mobility.SetPositionAllocator (positionAlloc);

  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");

  mobility.Install (n);

 

  InternetStackHelper stack;

  Ipv4ListRoutingHelper list;

  Ipv4StaticRoutingHelper staticRouting;

  list.Add (staticRouting, 0);

  stack.SetRoutingHelper (list);

  stack.Install (n);

 

  Ipv4AddressHelper ipv4;

  ipv4.SetBase ("10.1.1.0", "255.255.255.0");

  Ipv4InterfaceContainer i = ipv4.Assign (devices);

  

  UdpEchoServerHelper echoServer (9);

  ApplicationContainer serverApps = echoServer.Install (n.Get (1));

  serverApps.Add(echoServer.Install (n.Get (2)));

  serverApps.Start (Seconds (1.0));

  serverApps.Stop (Seconds (10.0));

 

  UdpEchoClientHelper echoClient (Address (InetSocketAddress (Ipv4Address ("255.255.255.255"), 9)));

  echoClient.SetAttribute ("MaxPackets", UintegerValue (1));

  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));

  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

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

  clientApps.Start (Seconds (2.0));

  clientApps.Stop (Seconds (10.0));

 

  AsciiTraceHelper ascii;

  wifiPhy.EnableAsciiAll (ascii.CreateFileStream ("adhoc-broadcast.tr"));

 

  uint32_t stopTime = 11;

  Simulator::Stop (Seconds (stopTime));

  Simulator::Run ();

  Simulator::Destroy ();

}

 

Execution

N1(10.1.1.3) and N2(10.1.1.1) are all within the transmission of N0 (10.1.1.1). So N1 and N2 can receive the packet sent from N0.

 

Back to NS3 Learning Guide

Last Modified: 2022/2/12 done

 

[Author]

Dr. Chih-Heng Ke

Department of Computer Science and Information Engineering, National Quemoy University, Kinmen, Taiwan

Email: smallko@gmail.com