NST32031-Practical For Wireless Network: Department of ICT Faculty of Technology South Eastern University of Sri Lanka
NST32031-Practical For Wireless Network: Department of ICT Faculty of Technology South Eastern University of Sri Lanka
// This groups all ns-3-related declarations in a scope outside the global namespace,
using namespace ns3;
cmd.Parse (argc,argv);
//Topology Helpers
//NodeContainer topology helper provides a convenient way to create, manage and access
any Node objects that we create in order to run a simulation
NodeContainer p2pNodes;
//point-to-point connection between n0 and n1
//asks the container to create two nodes.
p2pNodes.Create (2);
NodeContainer csmaNodes;
csmaNodes.Add (p2pNodes.Get (1));
csmaNodes.Create (nCsma); //nCsma = 3
//NetDeviceContainer holds a list of all of the NetDevice objects that are created
NetDeviceContainer p2pDevices;
p2pDevices = pointToPoint.Install (p2pNodes);
CsmaHelper csma;
csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
NetDeviceContainer csmaDevices;
csmaDevices = csma.Install (csmaNodes);
InternetStackHelper stack;
stack.Install (p2pNodes.Get (0)); // stack.Install (p2pNodes.Get (1))there is no need to
install
stack.Install (csmaNodes); //p2pNodes.Get(1)= csmaNodes.Get(0)
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer p2pInterfaces;
p2pInterfaces = address.Assign (p2pDevices);
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
pointToPoint.EnablePcapAll ("second");
//pcap will be opened using either wireshark or tcpdump
csma.EnablePcap ("second", csmaDevices.Get (1), true);
//if you want to get packets to other nodes;
//csma.EnablePcap ("second", csmaDevices.Get (3), true);
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
To run this;
First copy paste second.cc to scratch folder. Then run below codes in terminal;
1. >> cd ns-allinone-3.30/
2. >> cd ns-3.30/
3. >>. /waf --run scratch/second.cc
int
main (int argc, char *argv[])
{
bool verbose = true;
uint32_t nCsma = 3;
CommandLine cmd;
cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);
cmd.Parse (argc,argv);
if (verbose)
{
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
}
nCsma = nCsma == 0 ? 1 : nCsma;
NodeContainer p2pNodes;
p2pNodes.Create (2);
NodeContainer csmaNodes;
csmaNodes.Add (p2pNodes.Get (1));
csmaNodes.Create (nCsma);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
NetDeviceContainer p2pDevices;
p2pDevices = pointToPoint.Install (p2pNodes);
CsmaHelper csma;
csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
NetDeviceContainer csmaDevices;
csmaDevices = csma.Install (csmaNodes);
InternetStackHelper stack;
stack.Install (p2pNodes.Get (0));
stack.Install (csmaNodes);
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer p2pInterfaces;
p2pInterfaces = address.Assign (p2pDevices);
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
pointToPoint.EnablePcapAll ("second");
csma.EnablePcap ("second", csmaDevices.Get (1), true);
//Network Animator
AnimationInterface anim(“second.xml”);
anim.SetConstantPosition(p2pNodes.Get(0), 10.0,10.0);
anim.SetConstantPosition(p2pNodes.Get(1), 20.0,20.0);
anim.SetConstantPosition(csmaNodes.Get(1), 30.0,30.0);
anim.SetConstantPosition(csmaNodes.Get(2), 40.0,40.0);
anim.SetConstantPosition(csmaNodes.Get(3), 50.0,50.0);
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
To run this;