How to create a CBR Application with UDP Protocol

94 views
Skip to first unread message

Amruth Gudigar

unread,
May 30, 2024, 12:48:03 AM5/30/24
to ns-3-users
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/mobility-module.h"
#include "ns3/internet-module.h"
#include "ns3/applications-module.h"
#include "ns3/olsr-helper.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/wifi-module.h"
#include "ns3/netanim-module.h"
#include "ns3/ipv4-routing-table-entry.h"
#include "ns3/ipv4-static-routing.h"
#include "ns3/ipv4-list-routing.h"
#include "ns3/yans-wifi-helper.h"
#include "ns3/node-container.h"
#include "ns3/application-container.h"
#include "ns3/ipv4-address.h"
#include "ns3/csma-module.h"
#include "ns3/trace-helper.h"
#include "ns3/flow-monitor-module.h"

#include <cassert>
#include <fstream>
#include <iostream>
#include <string>

using namespace ns3;

NS_LOG_COMPONENT_DEFINE("OLSR-Example");

void PrintRoutingTable (Ptr<Node> node)
{
  //Print Routing Table
  Ipv4StaticRoutingHelper routingHelper;
  Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> (&std::cout);
  routingHelper.PrintRoutingTableAt (Seconds (10.0), node, routingStream);

  //Print IP Address
  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
  Ipv4Address address = ipv4->GetAddress (1, 0).GetLocal ();
  std::cout << "Node " << node->GetId () << " IP Address: " << address << std::endl;
}

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

    // Allow the user to override any of the defaults and the above
    // DefaultValue::Bind ()s at run-time, via command-line arguments
    CommandLine cmd;
    cmd.Parse (argc, argv);

    NS_LOG_UNCOND("\n-------------------------------------------\n    OLSR Simulator Testing\n-------------------------------------------\n");

    Time::SetResolution(Time::NS);
    LogComponentEnable("OLSR-Example", LOG_LEVEL_INFO);
    LogComponentEnable("InternetStackHelper", LOG_LEVEL_INFO);
    LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
    LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
    //LogComponentEnable("OnOffApplication", LOG_LEVEL_INFO);
    LogComponentEnable("PacketSink", LOG_LEVEL_INFO);

    // Create 3 nodes
    NodeContainer nodes;
    nodes.Create(3);

    // Set up mobility with no mobility
    MobilityHelper mobility;
    mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
    mobility.Install(nodes);

    // Set node positions
    Ptr<Node> n0 = nodes.Get(0);
    Ptr<Node> n1 = nodes.Get(1);
    Ptr<Node> n2 = nodes.Get(2);
    Ptr<MobilityModel> locN0 = n0->GetObject<MobilityModel>();
    Ptr<MobilityModel> locN1 = n1->GetObject<MobilityModel>();
    Ptr<MobilityModel> locN2 = n2->GetObject<MobilityModel>();
    locN0->SetPosition(Vector(0, 0, 0));
    locN1->SetPosition(Vector(100, 0, 0));
    locN2->SetPosition(Vector(200, 0, 0));

    // setting up wifi phy and channel using helpers
    WifiHelper wifi;
    wifi.SetStandard (WIFI_STANDARD_80211b);
 
    YansWifiPhyHelper wifiPhy;
    YansWifiChannelHelper wifiChannel;
    wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
    wifiChannel.AddPropagationLoss ("ns3::RangePropagationLossModel","MaxRange",DoubleValue(110.0));
    wifiPhy.SetChannel (wifiChannel.Create ());
   
    // Add a mac and disable rate control
    WifiMacHelper wifiMac;
    wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
                                 "DataMode",StringValue ("DsssRate11Mbps"),
                                 "ControlMode",StringValue ("DsssRate1Mbps"));

   
    wifiMac.SetType ("ns3::AdhocWifiMac");
    NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);

    // Install internet stack with OLSR
    Ipv4ListRoutingHelper list;
    InternetStackHelper internet;
    OlsrHelper olsr;
    list.Add (olsr,100);
    internet.SetRoutingHelper(list); // has effect on the internet stack installed
    internet.Install(nodes);

    NS_LOG_INFO("Assigning IP Address");

    Ipv4AddressHelper ipv4;
    ipv4.SetBase("10.1.1.0", "255.255.255.0");
    Ipv4InterfaceContainer interfaces;
    interfaces = ipv4.Assign(devices);

    // UdpEchoServerHelper echoServer(9);

    // ApplicationContainer serverApps = echoServer.Install(nodes.Get(2));
    // serverApps.Start(Seconds(1.0));
    // serverApps.Stop(Seconds(30.0));

    // UdpEchoClientHelper echoClient(interfaces.GetAddress(2), 9);
    // echoClient.SetAttribute("MaxPackets", UintegerValue(10));
    // echoClient.SetAttribute("Interval", TimeValue(Seconds(0.002336)));
    // echoClient.SetAttribute("PacketSize", UintegerValue(1460));

    // ApplicationContainer clientApps = echoClient.Install(nodes.Get(0));
    // clientApps.Start(Seconds(1.0));
    // clientApps.Stop(Seconds(30.0));

    // // Populate routing tables
    // Ipv4GlobalRoutingHelper::PopulateRoutingTables();

    // Install CBR application
    // uint16_t port = 9;
    // OnOffHelper onoff ("ns3::UdpSocketFactory", InetSocketAddress (interfaces.GetAddress (2), port));
    // onoff.SetConstantRate (DataRate ("1Mbps"));
    // ApplicationContainer apps = onoff.Install (nodes.Get (0));
    // apps.Start (Seconds (1.0));
    // apps.Stop (Seconds (30.0));

    // PacketSinkHelper sink ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), port));
    // apps = sink.Install (nodes.Get (2));
    // apps.Start (Seconds (1.0));
    // apps.Stop (Seconds (30.0));

    NS_LOG_INFO("Create Applications.");
    uint16_t port = 9; // Discard port (RFC 863)

    OnOffHelper onoff1("ns3::UdpSocketFactory", InetSocketAddress(interfaces.GetAddress(0), port));
    onoff1.SetConstantRate(DataRate("448kb/s"));

    ApplicationContainer onOffApp1 = onoff1.Install(nodes.Get(2));
    onOffApp1.Start(Seconds(1.0));
    onOffApp1.Stop(Seconds(30.0));

    // // Create a similar flow from n3 to n1, starting at time 1.1 seconds
    // OnOffHelper onoff2("ns3::UdpSocketFactory", InetSocketAddress(nodes.GetAddress(0), port));
    // onoff2.SetConstantRate(DataRate("448kb/s"));

    // ApplicationContainer onOffApp2 = onoff2.Install(nodes.Get(3));
    // onOffApp2.Start(Seconds(1.0));
    // onOffApp2.Stop(Seconds(30.0));

    // // Create packet sinks to receive these packets
    // PacketSinkHelper sink("ns3::UdpSocketFactory", InetSocketAddress(Ipv4Address::GetAny(), port));
    // NodeContainer sinks = NodeContainer(nodes.Get(4), nodes.Get(1));
    // ApplicationContainer sinkApps = sink.Install(sinks);
    // sinkApps.Start(Seconds(1.0));
    // sinkApps.Stop(Seconds(30.0));

    NS_LOG_INFO ("Configure Tracing.");

    // Results and Metrics
    std::string tracePath = "scratch/OLSR";

    AsciiTraceHelper ascii;
    wifiPhy.EnableAsciiAll (ascii.CreateFileStream (tracePath + "/OLSR-Example-trace.tr"));

    wifiPhy.EnablePcapAll (tracePath + "/OLSR-Example-pcap");

    AnimationInterface anim ( tracePath + "/OLSR-Example-animation.xml");

    Simulator::Schedule(Seconds(30.0), &PrintRoutingTable, nodes.Get(0));
    Simulator::Schedule(Seconds(30.0), &PrintRoutingTable, nodes.Get(1));
    Simulator::Schedule(Seconds(30.0), &PrintRoutingTable, nodes.Get(2));

    NS_LOG_INFO ("Run Simulation.");

    // Run simulation
    Simulator::Stop(Seconds(50.0));
    Simulator::Run();
    Simulator::Destroy();

    return 0;
}

I am trying many ways to create application , still i am not create a application please help me

Tommaso Pecorella

unread,
May 30, 2024, 4:39:58 AM5/30/24
to ns-3-users
Can you please explain what exactly the problem is?

Amruth Gudigar

unread,
May 30, 2024, 5:45:02 AM5/30/24
to ns-3-users
I am trying to create an UDP application, there is no application packet is getting generated. 
Can you please help me to configure application correctly

Amruth Gudigar

unread,
May 30, 2024, 7:00:53 AM5/30/24
to ns-3-users
I need a help with adding a application in Ns-3

Tommaso Pecorella

unread,
May 30, 2024, 6:55:04 PM5/30/24
to ns-3-users
The problem doesn't seems to be the application. Rather it seems to be in the wifi or IP stacks.
We're investigating.

Amruth Gudigar

unread,
May 31, 2024, 2:30:37 AM5/31/24
to ns-3-users
Can you help me to make this scenario work

For your info
I was able to observe the Routing formed by OLSR correctly through Wireshark and IP Table 

Tommaso Pecorella

unread,
May 31, 2024, 8:31:18 AM5/31/24
to ns-3-users
What part of "We're investigating." is unclear?

Amruth Gudigar

unread,
May 31, 2024, 8:38:34 AM5/31/24
to ns-3-users
I am trying to identify the why there is no transmission between N1 and N3?

Tommaso Pecorella

unread,
May 31, 2024, 1:53:06 PM5/31/24
to ns-3-users
Because of this: https://gitlab.com/nsnam/ns-3-dev/-/issues/1095

Temporary solution: switch to 802.11n
Reply all
Reply to author
Forward
0 new messages