0% found this document useful (0 votes)
36 views15 pages

WNP

The document contains code for several network simulations using the NS-3 network simulator. The code defines node and link configurations for basic network topologies like a point-to-point network, an intermediate node network, a ring topology, and a client-server network with multiple nodes. It configures IP addresses and routing, and installs UDP echo client and server applications to test connectivity across the simulated networks.

Uploaded by

Bharath
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)
36 views15 pages

WNP

The document contains code for several network simulations using the NS-3 network simulator. The code defines node and link configurations for basic network topologies like a point-to-point network, an intermediate node network, a ring topology, and a client-server network with multiple nodes. It configures IP addresses and routing, and installs UDP echo client and server applications to test connectivity across the simulated networks.

Uploaded by

Bharath
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/ 15

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"

// Default Network Topology


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

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.5.1.0", "255.255.255.0");

Ipv4InterfaceContainer interfaces = address.Assign (devices);

UdpEchoServerHelper echoServer (9);

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


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

UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);


echoClient.SetAttribute ("MaxPackets", UintegerValue (2));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (2.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (1));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));

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

INTERMEDIATE NODE

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

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");

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 pointToPoint;/* */
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("500Mbps")); /* */
pointToPoint.SetChannelAttribute ("Delay", StringValue ("1ms")); /* */

PointToPointHelper pointToPoint1;/* */
pointToPoint1.SetDeviceAttribute ("DataRate", StringValue ("250Mbps"));/* */
pointToPoint1.SetChannelAttribute ("Delay", StringValue ("1ms"));/* */

InternetStackHelper stack;
stack.Install (nodes);

NetDeviceContainer devices1,devices2;/* */
devices1 = pointToPoint.Install (nodes.Get(0),nodes.Get(1));/* */
devices2 = pointToPoint1.Install (nodes.Get(1),nodes.Get(2));/* */

Ipv4AddressHelper address1,address2;/* */
address1.SetBase ("10.5.1.0", "255.255.255.0");/* */
address2.SetBase ("10.6.1.0", "255.255.255.0");/* */

Ipv4InterfaceContainer interfaces1 = address1.Assign (devices1);/* */


Ipv4InterfaceContainer interfaces2 = address2.Assign (devices2);/* */

Ipv4GlobalRoutingHelper::PopulateRoutingTables ();/* */

UdpEchoServerHelper echoServer (9);

ApplicationContainer serverApps = echoServer.Install (nodes.Get (2));/* */


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

UdpEchoClientHelper echoClient (interfaces2.GetAddress (1), 9);


echoClient.SetAttribute ("MaxPackets", UintegerValue (2));/* */
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));

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

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

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");

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 (6);

PointToPointHelper pointToPoint1;
pointToPoint1.SetDeviceAttribute ("DataRate", StringValue ("500Mbps"));
pointToPoint1.SetChannelAttribute ("Delay", StringValue ("1ms"));

PointToPointHelper pointToPoint2;
pointToPoint2.SetDeviceAttribute ("DataRate", StringValue ("500Mbps"));
pointToPoint2.SetChannelAttribute ("Delay", StringValue ("1ms"));

PointToPointHelper pointToPoint3;
pointToPoint3.SetDeviceAttribute ("DataRate", StringValue ("500Mbps"));
pointToPoint3.SetChannelAttribute ("Delay", StringValue ("1ms"));

PointToPointHelper pointToPoint4;
pointToPoint4.SetDeviceAttribute ("DataRate", StringValue ("500Mbps"));
pointToPoint4.SetChannelAttribute ("Delay", StringValue ("1ms"));

PointToPointHelper pointToPoint5;
pointToPoint5.SetDeviceAttribute ("DataRate", StringValue ("500Mbps"));
pointToPoint5.SetChannelAttribute ("Delay", StringValue ("1ms"));

PointToPointHelper pointToPoint6;
pointToPoint6.SetDeviceAttribute ("DataRate", StringValue ("500Mbps"));
pointToPoint6.SetChannelAttribute ("Delay", StringValue ("1ms"));

InternetStackHelper stack;
stack.Install (nodes);

NetDeviceContainer devices1,devices2,devices3,devices4,devices5,devices6;
devices1 = pointToPoint1.Install (nodes.Get(0), nodes.Get(1));
devices2 = pointToPoint2.Install (nodes.Get(1), nodes.Get(2));
devices3 = pointToPoint3.Install (nodes.Get(2), nodes.Get(3));
devices4 = pointToPoint4.Install (nodes.Get(3), nodes.Get(4));
devices5 = pointToPoint5.Install (nodes.Get(4), nodes.Get(5));
devices6 = pointToPoint6.Install (nodes.Get(5), nodes.Get(0));

Ipv4AddressHelper address1, address2,address3,address4,address5,address6;


address1.SetBase ("10.5.1.0", "255.255.255.0");
address2.SetBase ("10.6.1.0", "255.255.255.0");
address3.SetBase ("10.7.1.0", "255.255.255.0");
address4.SetBase ("10.8.1.0", "255.255.255.0");
address5.SetBase ("10.9.1.0", "255.255.255.0");
address6.SetBase ("10.10.1.0", "255.255.255.0");

Ipv4InterfaceContainer interfaces1 = address1.Assign (devices1);


Ipv4InterfaceContainer interfaces2 = address2.Assign (devices2);
Ipv4InterfaceContainer interfaces3 = address3.Assign (devices3);
Ipv4InterfaceContainer interfaces4 = address4.Assign (devices4);
Ipv4InterfaceContainer interfaces5 = address5.Assign (devices5);
Ipv4InterfaceContainer interfaces6 = address6.Assign (devices6);

Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

UdpEchoServerHelper echoServer (9);

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


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

UdpEchoClientHelper echoClient (interfaces6.GetAddress (0), 9);


echoClient.SetAttribute ("MaxPackets", UintegerValue (2));
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));

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

Client Server 2

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

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");

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 (4);

PointToPointHelper pointToPoint1;
pointToPoint1.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint1.SetChannelAttribute ("Delay", StringValue ("2ms"));
PointToPointHelper pointToPoint2;
pointToPoint2.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint2.SetChannelAttribute ("Delay", StringValue ("2ms"));
PointToPointHelper pointToPoint3;
pointToPoint3.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint3.SetChannelAttribute ("Delay", StringValue ("2ms"));
PointToPointHelper pointToPoint4;

NetDeviceContainer devices1,devices2,devices3;
devices1 = pointToPoint1.Install (nodes.Get(0),nodes.Get(1));
devices2 = pointToPoint2.Install (nodes.Get(1),nodes.Get(2));
devices3 = pointToPoint3.Install (nodes.Get(2),nodes.Get(3));

InternetStackHelper stack;
stack.Install (nodes);

Ipv4AddressHelper address1,address2,address3,address4;
address1.SetBase ("10.1.1.0", "255.255.255.0");
address2.SetBase ("10.2.1.0", "255.255.255.0");
address3.SetBase ("10.3.1.0", "255.255.255.0");

Ipv4InterfaceContainer interfaces1 = address1.Assign (devices1);


Ipv4InterfaceContainer interfaces2 = address2.Assign (devices2);
Ipv4InterfaceContainer interfaces3 = address3.Assign (devices3);

Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
UdpEchoServerHelper echoServer (9);

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


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

UdpEchoClientHelper echoClient (interfaces2.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 (1));


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

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

Second.cc

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

// Default Network Topology


//
// 10.1.1.0
// n0 -------------- n1 n2 n3 n4
// point-to-point | | | |
// ================
// LAN 10.1.2.0

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");

int
main (int argc, char *argv[])
{
bool verbose = true;
uint32_t nCsma = 4;

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);
}

nCsma = nCsma == 0 ? 1 : nCsma;

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"));
csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));

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

address.SetBase ("10.1.2.0", "255.255.255.0");


Ipv4InterfaceContainer csmaInterfaces;
csmaInterfaces = address.Assign (csmaDevices);

UdpEchoServerHelper echoServer (9);

ApplicationContainer serverApps = echoServer.Install (p2pNodes.Get (0));


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

UdpEchoClientHelper echoClient (p2pInterfaces.GetAddress (0), 9);


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

ApplicationContainer clientApps = echoClient.Install (csmaNodes.Get (nCsma));


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

Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

pointToPoint.EnablePcapAll ("second");
csma.EnablePcap ("second", csmaDevices.Get (1), true);

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

Second second

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

// Default Network Topology


//
// 10.1.1.0
// n0 -------------- n1 n2 n3 n4
// point-to-point | | | |
// ================
// LAN 10.1.2.0

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");

int
main (int argc, char *argv[])
{
bool verbose = true;
uint32_t nCsma = 4;

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);
}

nCsma = nCsma == 0 ? 1 : nCsma;

NodeContainer p2pNodes;
p2pNodes.Create (2);

NodeContainer csmaNodes;
csmaNodes.Add (p2pNodes.Get (0));
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"));
csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));

NetDeviceContainer csmaDevices;
csmaDevices = csma.Install (csmaNodes);

InternetStackHelper stack;
stack.Install (p2pNodes.Get (1));
stack.Install (csmaNodes);

Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer p2pInterfaces;
p2pInterfaces = address.Assign (p2pDevices);
address.SetBase ("10.1.2.0", "255.255.255.0");
Ipv4InterfaceContainer csmaInterfaces;
csmaInterfaces = address.Assign (csmaDevices);

UdpEchoServerHelper echoServer (9);

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


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

UdpEchoClientHelper echoClient (p2pInterfaces.GetAddress (1), 9);


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

ApplicationContainer clientApps = echoClient.Install (csmaNodes.Get (nCsma));


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

Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

pointToPoint.EnablePcapAll ("second");
csma.EnablePcap ("second", csmaDevices.Get (1), true);

Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Second but 2nodes with csma

NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");

Lnt

using namespace ns3;

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

ncsma ncsma 071: ncsma;

NodeContainer p2pNodes; p2pNodes.Create (2);

NodeContainer csmaNode1;

csmaNode1. Add (p2pNodes. Get (0)); csmaNode1.Create (4);

NodeContainer

csmaNode2;

csmaNode2.Add (p2pNodes.Get (1)); csmaNode2.Create (4);

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 s ^ prime prime )) ;


csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));

NetDeviceContainer csmaDevice1;
csmaDevice1 = csma. Install (csmaNode1);

NetDeviceContainer csmaDevice2; csmaDevice2 = csma. Install (csmaNode2);

InternetStackHelper stack; I stack. Install (csmaNode1);

stack. Install (csmaNode2);

Ipv4AddressHelper address;

address.SetBase 0.1 * 0.1 - 1 ", "255.255.255.0"); Ipv4InterfaceContainer p2pInterfaces;

p2pInterfaces = address.Assign (p2pDevices);


pzpancer

address.SetBase ("10.1.2.0", "255.255.255.0"); //address of 1st csma node Ipv4InterfaceContainer


csmaInterface1;

CsmaInterface1 = address.Assign (csmaDevice1

);

address.SetBase ("10.1.3.0", "255.255.255.0");//2nd csma node Ipv4InterfaceContainer


csmaInterface2;

CsmaInterface2 = address.Assign (csmaDevice2)

echoServer. Install (csmaNode1.Get (2));//install server

UdpEchoServerHelper echoServer (9)

ApplicationContainer serverApps =

serverApps.Start (Seconds (1.0));

serverApps.Stop (Seconds (10.0));

UdpEchoClientHelper echoClient (csmaInterface1.GetAddress (2), 9);//client gets server attributes


echoClient.SetAttribute ("MaxPackets", UintegerValue (1));

echoclient.SetAttribute ("Interval", TimeValue (Seconds (1.0))) echoClient.SetAttribute ("PacketSize",


UtntegerValue (1824));

ApplicationContainer clientApps echoClient. Install (csmaNode2. Get (3));//install client giving

server info to client

clientApps.stop (Seconds aligned (2, 0) :\\ (10,0)); aligned clientApps.Start (Seconds

Ipv4Global RoutingHelper::PopulateRoutingTables ();

pointToPoint, EnablePcapALL ("second");


Simulator::Run (); I Simulator::Destroy ();

return 0;

csma.EnablePcap ("second", csmaDevice2. Get (1), true);


Simulator::Run();
Simulator::Destroy()’
Return 0;
}

You might also like