Emailing Network Programming Lab -Part B Programs-1
Emailing Network Programming Lab -Part B Programs-1
Tech (CSE)
PART-B
NS-3
Many simulation tools exist for network simulation studies. Below are a few distinguishing features
of ns-3 in contrast to other tools.
1. ns-3 is designed as a set of libraries that can be combined together and also with other external
software libraries. While some simulation platforms provide users with a single, integrated graphical user
interface environment in which all tasks are carried out, ns-3 is more modular in this regard. Several
external animators and data analysis and visualization tools can be used with ns-3.
2. However, users should expect to work at the command line and with C++ and/or Python software
development tools.
3. ns-3 is primarily used on Linux systems, although support exists for FreeBSD, Cygwin (for
Windows), and native Windows Visual Studio support is in the process of being developed.
To install prerequisites-
2. sudo apt-get install gcc g++ python && sudo apt-get install gcc g++ python &&
sudo apt-get install mercurial python-setuptools git && sudo apt-get install qt5-
default && sudo apt-get install python-pygraphviz python-kiwi python-pygoocanvas
libgoocanvas-dev ipython && sudo apt-get install openmpi-bin openmpi-common
openmpi-doc libopenmpi-dev && sudo apt-get install autoconf cvs bzr unrar && sudo
apt-get install gdb valgrind && sudo apt-get install uncrustify && sudo apt-get
install doxygen graphviz imagemagick && sudo apt-get install texlive texlive-
extra-utils texlive-latex-extra texlive-font-utils texlive-lang-portuguese dvipng
&& sudo apt-get install python-sphinx dia && sudo apt-get install gsl-bin libgsl2
libgsl-dev && sudo apt-get install flex bison libfl-dev && sudo apt-get install
tcpdump && sudo apt-get install sqlite sqlite3 libsqlite3-dev && sudo apt-get
install libxml2 libxml2-dev && sudo apt-get install cmake libc6-dev libc6-dev-i386
libclang-dev && sudo pip install cxxfilt && sudo apt-get install libgtk2.0-0
libgtk2.0-dev && sudo apt-get install vtun lxc && sudo apt-get install libboost-
signals-dev libboost-filesystem-dev
3. mkdir ns3
4. cd ns3
5. wget https://www.nsnam.org/release/ns-allinone-3.27.tar.bz2
7. cd ns-allinone-3.27/
8. ls
Then you can find build.py along with other files.
10. cd ns3/ns-allinone-3.27/ns-3.27
./waf
12. ./test.py
2. cd ns3/ns-allinone-3.27/netanim-3.108
3. make clean
4. qmake NetAnim.pro
5. make
6. ./NetAnim
1. Write a C++ program to connect two nodes on NS-3 (for practice only)
// header files
#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"
#include "ns3/netanim-module.h"
NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");
int
main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
//Log files
Time::SetResolution (Time::NS); //Total time your application consumes
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
NodeContainer nodes;
nodes.Create (2); //Take number of nodes
UdpEchoServerHelper echoServer (9);//type of server you want to create with port number
AnimationInterface anim("np1.xml");
AsciiTraceHelper eventTraces;
pointToPoint.EnableAsciiAll(eventTraces.CreateFileStream("np1.tr"));
pointToPoint.EnablePcapAll ("np1");
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
OUTPUT
2. Write a C++ program to connect three nodes considering one as a central node on NS-3 (for
practice only)
#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"
#include "ns3/netanim-module.h"
NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");
int
main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
Time::SetResolution (Time::NS);
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
NodeContainer nodes;
nodes.Create (3);
PointToPointHelper p2p1;
p2p1.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
p2p1.SetChannelAttribute ("Delay", StringValue ("1ms"));
PointToPointHelper p2p2;
p2p2.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
p2p2.SetChannelAttribute ("Delay", StringValue ("5ms"));
InternetStackHelper stack;
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
NetDeviceContainer devices;
devices=p2p1.Install (nodes.Get (0), nodes.Get (1));
Ipv4InterfaceContainer interfaces = address.Assign (devices);
Ipv4GlobalRoutingHelper::PopulateRoutingTables();
AnimationInterface anim("np2.xml");
AsciiTraceHelper eventTraces;
p2p1.EnableAsciiAll(eventTraces.CreateFileStream("np2.tr"));
p2p1.EnablePcapAll ("np2");
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
OUTPUT
NS_LOG_COMPONENT_DEFINE ("Star");
int
main (int argc, char *argv[])
{
uint32_t nSpokes = 8;
CommandLine cmd;
cmd.AddValue ("nSpokes", "Number of nodes to place in the star", nSpokes);
cmd.Parse (argc, argv);
ApplicationContainer spokeApps;
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
AnimationInterface anim("np3.xml");
AsciiTraceHelper eventTraces;
pointToPoint.EnableAsciiAll(eventTraces.CreateFileStream("np3.tr"));
pointToPoint.EnablePcapAll ("np3");
return 0;
}
OUTPUT
NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");
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);
}
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"));
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 ();
AnimationInterface anim("np4.xml");
AsciiTraceHelper eventTraces;
pointToPoint.EnableAsciiAll(eventTraces.CreateFileStream("np4.tr"));
pointToPoint.EnablePcapAll ("np4");
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
OUTPUT
5. Write a C++ program showing the connection of two nodes and four routers such that the
extreme nodes act as client and server on NS-3
#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"
#include "ns3/netanim-module.h"
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);
NetDeviceContainer subnet2Devices;
subnet2Devices = pointToPoint.Install (subnet2);
address2.SetBase ("10.1.2.0", "255.255.255.0");
Ipv4InterfaceContainer subnet2Interfaces;
NetDeviceContainer subnet3Devices;
subnet3Devices = pointToPoint.Install (subnet3);
address3.SetBase ("10.1.3.0", "255.255.255.0");
Ipv4InterfaceContainer subnet3Interfaces;
subnet3Interfaces = address3.Assign (subnet3Devices);
NodeContainer subnet4; subnet4.Add (router.Get (1));
subnet4.Add (router.Get (3));
NetDeviceContainer subnet4Devices;
subnet4Devices = pointToPoint.Install (subnet4);
address4.SetBase ("10.1.4.0", "255.255.255.0");
Ipv4InterfaceContainer subnet4Interfaces;
subnet4Interfaces = address4.Assign (subnet4Devices);
NodeContainer subnet5; subnet5.Add (router.Get (2));
subnet5.Add (host.Get (1));
NetDeviceContainer subnet5Devices;
subnet5Devices = pointToPoint.Install (subnet5);
address5.SetBase ("10.1.5.0", "255.255.255.0");
Ipv4InterfaceContainer subnet5Interfaces;
subnet5Interfaces = address5.Assign (subnet5Devices);
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (subnet5.Get (1));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
AnimationInterface anim("np5.xml");
AsciiTraceHelper eventTraces;
pointToPoint.EnableAsciiAll(eventTraces.CreateFileStream("np5.tr"));
pointToPoint.EnablePcapAll ("np5");
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
OUTPUT