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;
//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);
//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);
//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));
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
#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 (__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");
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
To run this;