Creating topology with routers in ns-3

2.3k Views Asked by At

I am trying to create a very simple topology containing a server and client connected via a router. The following code I got through a GUI. I am trying to run it but it throws me a long error I am new to ns-3, so kindly bear with me.

#include "ns3/simulator-module.h"
#include "ns3/node-module.h"
#include "ns3/core-module.h"
#include "ns3/common-module.h"
#include "ns3/global-route-manager.h"
#include "ns3/helper-module.h"
#include "ns3/bridge-module.h"

using namespace ns3;

int main(int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);

/* Configuration. */

/* Build nodes. */
NodeContainer term_0;
term_0.Create (1);
NodeContainer term_1;
term_1.Create (1);
NodeContainer router_0;
router_0.Create (1);

/* Build link. */
CsmaHelper csma_hub_0;
csma_hub_0.SetChannelAttribute ("DataRate", DataRateValue (100000000));
csma_hub_0.SetChannelAttribute ("Delay",  TimeValue (MilliSeconds (10000)));
CsmaHelper csma_hub_1;
csma_hub_1.SetChannelAttribute ("DataRate", DataRateValue (100000000));
csma_hub_1.SetChannelAttribute ("Delay",  TimeValue (MilliSeconds (10000)));

/* Build link net device container. */
NodeContainer all_hub_0;
all_hub_0.Add (router_0);
all_hub_0.Add (term_0);
NetDeviceContainer ndc_hub_0 = csma_hub_0.Install (all_hub_0);
NodeContainer all_hub_1;
all_hub_1.Add (router_0);
all_hub_1.Add (term_1);
NetDeviceContainer ndc_hub_1 = csma_hub_1.Install (all_hub_1);

/* Install the IP stack. */
InternetStackHelper internetStackH;
internetStackH.Install (term_0);
internetStackH.Install (term_1);
internetStackH.Install (router_0);

/* IP assign. */
Ipv4AddressHelper ipv4;
ipv4.SetBase ("10.0.0.0", "255.255.255.0");
Ipv4InterfaceContainer iface_ndc_hub_0 = ipv4.Assign (ndc_hub_0);
ipv4.SetBase ("10.0.1.0", "255.255.255.0");
Ipv4InterfaceContainer iface_ndc_hub_1 = ipv4.Assign (ndc_hub_1);

/* Generate Route. */
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

/* Generate Application. */
uint16_t port_udpEcho_0 = 9;
UdpEchoServerHelper server_udpEcho_0 (port_udpEcho_0);
ApplicationContainer apps_udpEcho_0 = server_udpEcho_0.Install (term_1.Get(0));
apps_udpEcho_0.Start (Seconds (0.0));
apps_udpEcho_0.Stop (Seconds (2.0));
Time interPacketInterval_udpEcho_0 = Seconds (1.0);
UdpEchoClientHelper client_udpEcho_0 (iface_ndc_hub_1.GetAddress(1), 9);
client_udpEcho_0.SetAttribute ("MaxPackets", UintegerValue (1));
client_udpEcho_0.SetAttribute ("Interval", TimeValue (interPacketInterval_udpEcho_0));
client_udpEcho_0.SetAttribute ("PacketSize", UintegerValue (1024));
apps_udpEcho_0 = client_udpEcho_0.Install (term_0.Get (0));
apps_udpEcho_0.Start (Seconds (0.1));
apps_udpEcho_0.Stop (Seconds (2.0));

/* Simulation. */
/* Pcap output. */
/* Stop the simulation after x seconds. */
uint32_t stopTime = 3;
Simulator::Stop (Seconds (stopTime));
/* Start and clean simulation. */
Simulator::Run ();
Simulator::Destroy ();
}
1

There are 1 best solutions below

0
On

If you are new, you should start your program by looking the examples already provided by ns-3. Like this: https://www.nsnam.org/doxygen/simple-routing-ping6_8cc_source.html. Looking the network topology, it is what you want.

Trying to run your code, I had to change a lot of headers to make it compile (maybe different versions, given the time - or your GUI is outdated). Remove yours and put these:

#include "ns3/core-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/csma-helper.h"

And finally, you'd printed nothing, so I am not sure of what you were trying to do. As you are using, UdpEchoClientHelper and UdpEchoServerHelper, I would recommend to enable their logs. You can do this with these two lines:

/* Configuration. */
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);

I am new too and I do not know what you are trying to do. So I will stop my answer here. Good luck.