You are on page 1of 11

DATA COMMUNICATIONS AND

NETWORK LAB
(ETEC – 358)

Maharaja Surajmal Institute of Technology,


Affiliated to : GGSIPU University
C-4 Janakpuri, New Delhi - 110058

Submitted by : AAKRITI RAJ Submitted to :


ECE – 2 , 6TH Semester Mrs. SHIVANKA
Enrollment No. : 20515002819
Serial No. : 49
PROGRAM NO. 1

Objective :
To Study about simulation tools ns2 & ns3, comparison between ns2 & ns3 and
installation steps of ns3.

Theory :
Ns2 : Ns2(Network Simulator Version 2) is an open source object oriented simulator,
written in C++, with an OTL(Object Tool Command Language) interpreter as a frontend.
A simulator has two objectives: Detailed simulations of protocols- which requires a system
programming language which can efficiently manipulate bytes, packet headers and
implement algorithms that run over large data sets at high run time speeds; another large
part of network research involves slightly varying parameters or configurations or quickly
exploring a number of scenarios- which prefers faster iteration time.
Ns2 meets both of these needs with two languages, C++ and OTL – C++ is fast to run,
making it suitable for detailed protocol implementation, while OTL can be changed very
quickly (and interactively), making it ideal for simulation configuration.
Ns3 : Ns3(Network Simulation Version 3) has been developed to provide an open,
extensible network simulation platform, for networking research and education. It
provides models of how packet data networks work and perform and provides a
simulation engine for users to conduct simulation experiments.
Some of the reasons to use ns-3 include performing studies that are more difficult or not
possible to perform with real systems, to study system behavior in a highly controlled,
reproducable environment and to learn about how networks work. The available model
set in ns-3 focuses on modelling how Internet protocols and networks work, but ns-3 is not
limited to Internet systems; several users are using ns-3 to model non-internet-based
systems.
Ns-3 is actively maintained with an active, responsive users mailing list, while ns-2 is only
lightly maintained and has not seen significant development in its main code tree for over
a decade.
Ns-3 provides features not available in ns-2, such as an implantation code execution
environment (allowing users to run real implementation code in the simulator).
Ns-3 provides a lower base level of abstraction compared with ns-2, allowing it to align
better with how real systems are put together.
Comparison between Ns2 and Ns3 :
S.No. Difference Point Ns-2 Ns-3
1. Support Ns-2 is not actively maintained Ns-3 is actively maintained with very
and no longer support good support. It is not backward
provided. compatible with ns-2.

2. Architecture Ns-2 is made with C++ and OTCL Ns-3 is made with C++ and Python.
language. TCL is used as Instead of scripting language, there is
scripting language for network object-oriented language and another
simulation. option is Python (provided that
module contains python wrapper).

3. Programming C++, OTCL, TCL script. C++, Python.


language
4. Recompilation Recompilation is long process Recompilation is fast and within done
and many times fails. in single command i.e. ./waf
command.

5. Post analysis .nam for network animation, .tr .xml for network animation, .tr for
supported file for trace parameters, .xg for trace analysis, .pcap for wireshark
xgraph. analysis, .csv as comma separated
formats
values.

6. Network Nam animator. Pyviz as python visualizer, NetAnim as


Visualization network animation.

7. Other Only simulation. With simulation there is emulation


also, DCE environment is provided in
which used to simulate Linux kernel
network stack.

Installation steps of NS3 version 3.27 on ubuntu (steps might differ a little bit in
change of version or different operating system):
(1.) download ns3 and save on desktop tar file

(2.) cd Desktop

(3.) tar -xvf ns-allinone-3.27.tar.bz2

(4.) cd ns-allinone-3.27

(5.) ./build.py --enable-examples --enable-tests

(6.) sudo apt-get install gcc g++ python python-dev qt4-dev-tools libgtk-3-dev python-
pygoocanvas python-pygraphviz wireshark gnuplot openjdk-7-jdk
(7.) ls

(8.) cd ns-3.27

(9.) ./test.py -c core

(10.) ./waf --run scratch/first

(11.) cd ..

(12.) ls

(13.) cd netanim-3.108

(14.) make clean

(15.) qmake NetAnim.pro

(16.) make

(17.) ./NetAnim

Then new window will open for animation.

- Check .xml file in folder ns- 3.27 folder and run that xml file to check animation.

-Before run any program, first of all copy that program in scratch folder and add header file
(in starting of program) as well as animation steps (before simulator:: run)

Desktop-allinone folder-ns-3.27-examples-tutorials-first.cc

then copy this first.cc program and paste in scratch folder (exist in ns3.27 folder), and
remove all other files from scratch folder, there should be one single program first.cc, now
add animation step in this program.

Ns3 commands description:


1. $ ./build.py --enable-examples --enable-tests
Because we are working with examples and tests in this tutorial, and because they are not
built by default in ns-3, the arguments for build.py tells it to build them for us. The program
also defaults to building all available modules. Later, you can build ns-3 without examples and
tests, or eliminate the modules that are not necessary for your work, if you wish.

2. sudo command:
The sudo command allows you to run programs with the security privileges of another user
(by default, as the superuser). It prompts you for your personal password and confirms your
request to execute a command by checking a file, called sudoers , which the system
administrator configures.
3. You can run the unit tests of the ns-3 distribution by running the ./test.py script:
$ ./test.py

These tests are run in parallel by Waf. You should eventually see a report saying that:
{92 of 92 tests passed (92 passed, 0 failed, 0 crashed, 0 valgrind errors)}
This is the important message to check for; failures, crashes, or valgrind errors indicate
problems with the code or incompatibilities between the tools and the code.

4. waf : to call the Waf build tool to do the actual building. An installation of Waf is
bundled with the ns-3 source code. Most users quickly transition to using Waf directly
to configure and build ns-3. So, to proceed, please change your working directory to the
ns-3 directory that you have initially built.
Running a Script :
We typically run scripts under the control of Waf. This allows the build system to ensure that
the shared library paths are set correctly and that the libraries are available at run time. To
run a program, simply use the --run option in Waf. Let’s run the ns-3 equivalent of the
ubiquitous hello world program by typing the following:
$ ./waf --run scratch/first
Waf first checks to make sure that the program is built correctly and executes a build if
required. Waf then executes the program.

5. make and qmake:


qmake is a utility that automates the generation of make-files. Make-files are used by the
program make to build executable programs from source code; therefore qmake is a make-
file tool, or make for short.
PROGRAM NO. 2(a)

Objective :
To simulate a network containing two nodes (server and client) using NS3.

Program :
#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"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");

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

Time::SetResolution (Time::NS);
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);

NodeContainer nodes;
nodes.Create (2);

PointToPointHelperpointToPoint;
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");

Ipv4InterfaceContainer interfaces = address.Assign (devices);

UdpEchoServerHelperechoServer (9);
ApplicationContainerserverApps = echoServer.Install (nodes.Get (1));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));

UdpEchoClientHelperechoClient (interfaces.GetAddress (1), 9);


echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

ApplicationContainerclientApps = echoClient.Install (nodes.Get (0));


clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));

AnimationInterfaceanim("m1.xml");
anim.SetConstantPosition(nodes.Get(0),1.0,2.0);
anim.SetConstantPosition(nodes.Get(1), 5.0,5.0);

Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Output:
PROGRAM NO. 2(b)

Objective :
To simulate a network containing three nodes (server and client) using NS3.

Program:
#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"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");

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

Time::SetResolution (Time::NS);
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);

NodeContainer nodes;
nodes.Create (3);

PointToPointHelperpointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));

NetDeviceContainer devices;
devices = pointToPoint.Install (nodes.Get(0),nodes.Get(1));

InternetStackHelper stack;
stack.Install (nodes);

Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");

Ipv4InterfaceContainer interfaces = address.Assign (devices);


PointToPointHelper pointToPoint1;
pointToPoint1.SetDeviceAttribute ("DataRate", StringValue
("5Mbps"));
pointToPoint1.SetChannelAttribute ("Delay", StringValue ("2ms"));
devices = pointToPoint1.Install(nodes.Get(1),nodes.Get(2));
address.SetBase("10.1.2.0","255.255.255.0");
interfaces = address.Assign(devices);

Ipv4GlobalRoutingHelper::PopulateRoutingTables();

UdpEchoServerHelperechoServer (9);

ApplicationContainerserverApps = echoServer.Install (nodes.Get (2));


serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));

UdpEchoClientHelperechoClient (interfaces.GetAddress (1), 9);


echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

ApplicationContainerclientApps = echoClient.Install (nodes.Get (0));


clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));

AnimationInterfaceanim("m1.xml");
anim.SetConstantPosition(nodes.Get(0),1.0,2.0);
anim.SetConstantPosition(nodes.Get(1), 5.0,5.0);
anim.SetConstantPosition(nodes.Get(2),10.0,10.0);

Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Output:

You might also like