0% found this document useful (0 votes)
62 views6 pages

NST32031-Practical For Wireless Network: Department of ICT Faculty of Technology South Eastern University of Sri Lanka

The document describes how to set up and run a simulation of a peer-to-peer network connection using the ns-3 network simulator. It includes code to create two nodes, connect them with a point-to-point link, install networking stacks, assign IP addresses, set up an echo server and client, and run the simulation. It also provides instructions for running the simulation and viewing it with the NetAnim network animator tool.

Uploaded by

aiz15563
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views6 pages

NST32031-Practical For Wireless Network: Department of ICT Faculty of Technology South Eastern University of Sri Lanka

The document describes how to set up and run a simulation of a peer-to-peer network connection using the ns-3 network simulator. It includes code to create two nodes, connect them with a point-to-point link, install networking stacks, assign IP addresses, set up an echo server and client, and run the simulation. It also provides instructions for running the simulation and viewing it with the NetAnim network animator tool.

Uploaded by

aiz15563
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

2021/04/30

NST32031-Practical for Wireless Network


Department of ICT
Faculty of Technology
South Eastern University of Sri Lanka
Lab Sheet 02

Title: Understanding peer-to-peer network connection


Source Code:
 Can be downloaded from https://www.nsnam.org/doxygen
 Or it is saved inside your downloaded ns3 file example/tutorial location

Source Code Explanation (first.cc or first.py):


//Header files adding
#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"

// Default Network Topology


//
// 10.1.1.0
// n0 -------------- n1
// point-to-point

// This groups all ns-3-related declarations in a scope outside the global namespace,
using namespace ns3;
//Doxygen documentation system. this line declares a logging component called
FirstScriptExample that allows you to enable and disable console message logging by
reference to the name.
NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");
//Declaration of the main function of your program (script)
int
main (int argc, char *argv[])
{
CommandLine cmd (__FILE__);
cmd.Parse (argc, argv);
//sets the time resolution to one nanosecond
Time::SetResolution (Time::NS);
//enable two logging components that are built into the Echo Client and Echo Server
applications: These two lines of code enable debug logging at the INFO level for echo
clients and servers.
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);

//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 nodes;
//asks the container to create two nodes.
nodes.Create (2);

//constructing a point to point link


PointToPointHelper pointToPoint; //instantiates a PointToPointHelper object on the stack
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));

//NetDeviceContainer holds a list of all of the NetDevice objects that are created
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes); //After executing this call we will have two nodes,
each with an installed point-to-point net device and a single point-to-point channel between
them
// install an Internet Stack (TCP, UDP, IP, etc.) on each of the nodes in the node container
InternetStackHelper stack;
stack.Install (nodes);

//associate the devices on our nodes with IP addresses


//declare an address helper object and tell it that it should begin allocating IP addresses
from the network 10.1.1.0 using the mask 255.255.255.0 to define the allocable bits.
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
//performs the actual address assignment.
Ipv4InterfaceContainer interfaces = address.Assign (devices);

//Applications
UdpEchoServerHelper echoServer (9); //set up a UDP echo server application on one of the
nodes we have previously created.
//Applications require a time to “start” generating traffic and may take an optional time to
“stop”
ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));

//UdpEchoClientHelper
UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9); //declares the
UdpEchoServerHelper.
echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));


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

// run the simulation


Simulator::Run ();
Simulator::Destroy ();
return 0;
}
To run this;

First copy paste first.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/first.cc

To implement network simulation to below changes:

#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[])
{
CommandLine cmd (__FILE__);
cmd.Parse (argc, 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");

Ipv4InterfaceContainer interfaces = address.Assign (devices);

UdpEchoServerHelper echoServer (9);

ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));


serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));


clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
AnimationInterface anim(“first.xml”);
anim.SetConstantPosition(nodes.Get(0), 10.0,10.0);
anim.SetConstantPosition(nodes.Get(1), 20.0,20.0);

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

To run this;

1. ./waf –run scratch/first


2. cd ns-allinone-3.30/netaim-3.108/
3. ./NetAnim

You might also like