CCN Lab (Question)
CCN Lab (Question)
3. Build ns3
4. Validate ns3
sudo apt-get/ dnf install gcc g++ python python-dev mercurial bzr gdb valgrind gsl-bin libgsl0-
dev libgsl0ldbl flex bison tcpdump sqlite sqlite3 libsqlite3-dev libxml2 libxml2-dev libgtk2.0-0
libgtk2.0-dev uncrustify doxygen graphviz imagemagick texlive texlive-latex-extra texlive-
generic-extra texlive-generic-recommended texinfo dia texlive texlive-latex-extra texlive-extra-
utils texlive-generic-recommended texi2html python-pygraphviz python-kiwi python-
pygoocanvas libgoocanvas-dev python-pygccxml
5.After downloading NS3 on the drive, extract all the files in the NS3 folder, which you have
created.
6.Then you can find build.py along with other files in NS3 folder.
Then to build the examples in ns-3 run :
./build.py --enable-examples –enable-tests
If the build is successful then it will give output
"Build finished successfully".
7. Now run the following command on the terminal window,to configure with waf(build tool)
./waf -d debug --enable-examples --enable-tests configure
To build with waf(optional)
./waf
8.To test everything allright run the following command on the terminal window,
./test.py
If the tests are ok the installation is done
9.Now after installing ns3 and testing it run some programs first to be ns3 user:
make sure you are in directory where waf script is available then run
EXPERIMENT No.3
Program in NS3 to connect two nodes
Node
Because in any network simulation, we will need nodes. So ns-3 comes with NodeContainer that
you can use to manage all the nodes (Add, Create, Iterate, etc.).
NodeContainer nodes;
nodes.Create (2);
PointToPointHelper pointToPoint;
NetDeviceContainer devices;
Protocols
Internet and IPv4. Since Internet is the current largest network to study, ns-3 has a particular focus
on it. The InternetStackHelper will install an Internet Stack (TCP, UDP, IP, etc.) on each of the
nodes in the node container.
InternetStackHelper stack;
stack.Install (nodes);
To assign IP addresses, use a helper and set the base. The low level ns-3 system actually
remembers all of the IP addresses allocated and will generate a fatal error if you accidentally
cause the same address to be generated twice.
// Since IP Address assignment is so common, the helper does the dirty work!
Ipv4AddressHelper address;
Applications
Every application needs to have Start and Stop function so that the simulator knows how to
schedule it. Other functions are application-specific. We will
use UdpEchoServer and UdpEchoClient for now.
// Application layer: UDP Echo Server and Client
// 1, Server:
// 2, Client:
Simulation
// Start Simulation
Simulator::Run ();
Simulator::Destroy ();
return 0;
CODE:-
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");
int
main (int argc, char *argv[])
{
Time::SetResolution (Time::NS);
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
NodeContainer nodes;
nodes.Create (2);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);
InternetStackHelper stack;
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
EXPERIMENT No.4
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");
int
main (int argc, char *argv[])
{
Time::SetResolution (Time::NS);
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
NodeContainer nodes;
nodes.Create (3);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
InternetStackHelper stack;
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
EXPERIMENT No.5
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/netanim-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/point-to-point-layout-module.h"
NS_LOG_COMPONENT_DEFINE ("Star");
int
main (int argc, char *argv[])
{
//
// Set up some default values for the simulation.
//
Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (137));
//
// Default number of nodes in the star. Overridable by command line argument.
//
uint32_t nSpokes = 8;
CommandLinecmd;
cmd.AddValue ("nSpokes", "Number of nodes to place in the star", nSpokes);
cmd.Parse (argc, argv);
//
// Create OnOff applications to send TCP to the hub, one on each spoke node.
//
OnOffHelper onOffHelper ("ns3::TcpSocketFactory", Address ());
onOffHelper.SetAttribute ("OnTime", StringValue
("ns3::ConstantRandomVariable[Constant=1]"));
onOffHelper.SetAttribute ("OffTime", StringValue
("ns3::ConstantRandomVariable[Constant=0]"));
ApplicationContainer spokeApps;
return 0;
EXPERIMENT No.6
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/ipv4-global-routing-helper.h"
// 10.1.1.0
// n0 -------------- n1 n2 n3 n4
// point-to-point | | | |
// ================
// LAN 10.1.2.0
NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");
int
uint32_t nCsma = 3;
CommandLine cmd;
cmd.Parse (argc,argv);
if (verbose)
NodeContainer p2pNodes;
p2pNodes.Create (2);
NodeContainer csmaNodes;
csmaNodes.Create (nCsma);
PointToPointHelper pointToPoint;
NetDeviceContainer p2pDevices;
CsmaHelper csma;
NetDeviceContainer csmaDevices;
InternetStackHelper stack;
stack.Install (csmaNodes);
Ipv4AddressHelper address;
Ipv4InterfaceContainer p2pInterfaces;
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
pointToPoint.EnablePcapAll ("second");
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
EXPERIMENT No.7
Installation and configuration of NetAnim
Installing NetAnim
The website:
http://www.nsnam.org/wiki/index.php/NetAnim
1. Install Mercurial:
3. You can use Synaptic too, to install both the above packages.
5. Build NetAnim:
cd netanim
...
2. Run Netanim:
./NetAnim
CODE:
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/ipv4-global-routing-helper.h"
NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");
int
main (int argc, char *argv[])
{
bool verbose = true;
if (verbose)
{
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
}
NodeContainer subnet1;
subnet1.Add (host.Get (0));
subnet1.Add (router.Get (0));
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
NetDeviceContainer subnet1Devices;
subnet1Devices = pointToPoint.Install (subnet1);
InternetStackHelper stack;
stack.Install (router);
stack.Install (host);
NodeContainer subnet2;
subnet2.Add (router.Get (0));
subnet2.Add (router.Get (1));
NetDeviceContainer subnet2Devices;
subnet2Devices = pointToPoint.Install (subnet2);
NodeContainer subnet3;
subnet3.Add (router.Get (1));
subnet3.Add (router.Get (2));
NetDeviceContainer subnet3Devices;
subnet3Devices = pointToPoint.Install (subnet3);
NodeContainer subnet4;
subnet4.Add (router.Get (1));
subnet4.Add (router.Get (3));
NetDeviceContainer subnet4Devices;
subnet4Devices = pointToPoint.Install (subnet4);
NodeContainer subnet5;
subnet5.Add (router.Get (2));
subnet5.Add (host.Get (1));
NetDeviceContainer subnet5Devices;
subnet5Devices = pointToPoint.Install (subnet5);
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
OUTPUT