0% found this document useful (0 votes)
87 views41 pages

NS3 Network Simulation Experiments

The document describes a list of experiments related to network simulation using the NS3 simulator. The experiments include installing and simulating NS3 tools, various network topologies like point-to-point, bus, and star, protocols like stop-and-wait and routing algorithms. Source code is provided to simulate a network with TCP client-server communication between two nodes and a more complex network architecture with four nodes connected via routers across multiple subnets.

Uploaded by

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

NS3 Network Simulation Experiments

The document describes a list of experiments related to network simulation using the NS3 simulator. The experiments include installing and simulating NS3 tools, various network topologies like point-to-point, bus, and star, protocols like stop-and-wait and routing algorithms. Source code is provided to simulate a network with TCP client-server communication between two nodes and a more complex network architecture with four nodes connected via routers across multiple subnets.

Uploaded by

Kartikey Rana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

List of Experiments

1Introduction to Discrete Event Simulation tools NS2 , NS3 and installation of Ns3.

2.Simulate point to point topology using NS3

3.Simulate the given architecture in NS3

4.Simulate Bus Topology using NS3.

5.Create a Straight Cable and crossover cable using RS232C Connector.

6.Implementation and study of Stop and Wait Protocol.

7.Implementation and study of Go-back-n and Selective Repeat Protocol.

8.Implementation and study of Distance Vector Routing algorithm.

9.Implementation and study of Link state routing protocol.

10.Simulate Star Topology using TCP server in NS3.


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

Source Code:
#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);

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("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:
AIM:To simulate a network containing three nodes with point to point connection using
NS3.
Source Code:
#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;
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 ("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));

AnimationInterface anim("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:
AIM:To simulate a network containing four nodes with point to point connection using
NS3.
Source Code:
#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 (4);
PointToPointHelper pointToPoint;
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);

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

Ipv4GlobalRoutingHelper::PopulateRoutingTables();

UdpEchoServerHelperechoServer (9);

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


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("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);
anim.SetConstantPosition(nodes.Get(3),15.0,15.0);

Simulator::Run ();
Simulator::Destroy ();
return 0;
}
OUTPUT:
AIM: Simulate given architecture in ns3

Source Code:

#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[])


{

NodeContainer hosts;
NodeContainer routers;
hosts.Create(1);
routers.Create(4);
InternetStackHelper stack;
Ipv4AddressHelper address;
stack.Install(routers);
stack.Install(hosts);
PointToPointHelper p1, p2, p3, p4;

NodeContainer subnet1;
subnet1.Add(hosts.Get(0));
subnet1.Add(routers.Get(0));
NetDeviceContainer subnet1devices=p1.Install(subnet1);
address.SetBase("10.1.1.0","255.255.255.0");
Ipv4InterfaceContainer subnet1interfaces=address.Assign(subnet1devices);

NodeContainer subnet2;
subnet2.Add(routers.Get(0));
subnet2.Add(routers.Get(1));
NetDeviceContainer subnet2devices=p2.Install(subnet2);
address.SetBase("10.1.2.0","255.255.255.0");
Ipv4InterfaceContainer subnet2interfaces=address.Assign(subnet2devices);

NodeContainer subnet3;
subnet3.Add(routers.Get(1));
subnet3.Add(routers.Get(2));
NetDeviceContainer subnet3devices=p3.Install(subnet3);
address.SetBase("10.1.3.0","255.255.255.0");
Ipv4InterfaceContainer subnet3interfaces=address.Assign(subnet3devices);

NodeContainer subnet4;
subnet4.Add(routers.Get(1));
subnet4.Add(routers.Get(3));
NetDeviceContainer subnet4devices=p4.Install(subnet4);
address.SetBase("10.1.4.0","255.255.255.0");
Ipv4InterfaceContainer subnet4interfaces=address.Assign(subnet4devices);
Ipv4GlobalRoutingHelper::PopulateRoutingTables();

UdpEchoServerHelper echoServer(9);
ApplicationContainer serverApps = echoServer.Install (routers.Get(3));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));

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


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

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


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

AnimationInterface anim("Mallika.xml");
anim.
Simulator::Destroy ();
return 0; SetConstantPosition(hosts.Get(0),10.0,10.0);
anim.SetConstantPosition(routers.Get(0),20.0,10.0);
anim.SetConstantPosition(routers.Get(1),30.0,10.0);
anim.SetConstantPosition(routers.Get(2),40.0,20.0);
anim.SetConstantPosition(routers.Get(3),40.0,0.0);
Simulator::Run ();

}
Output:
AIM: Simulate Bus Topology using NS3.
CODE:
#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"

// 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 = 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 == 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 (csmaNodes.Get (nCsma));


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

UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9);


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

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


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

Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

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

AnimationInterface anim("animX.xml");
anim.SetConstantPosition(p2pNodes.Get(0),1.0,2.0);
anim.SetConstantPosition(csmaNodes.Get(0),2.0,3.0);
anim.SetConstantPosition(csmaNodes.Get(1),3.0,3.0);
anim.SetConstantPosition(csmaNodes.Get(2),4.0,3.0);
anim.SetConstantPosition(csmaNodes.Get(3),5.0,3.0);

Simulator::Run ();
Simulator::Destroy ();
return 0;
}
OUTPUT
AIM: Create a Straight Cable and crossover cable using RJ45 Connector
A registered jack(RJ) is a standardized physical network interface for connecting
telecommunications or data equipment. The physical connectors that registered jacks use are
mainly of the modular connector and 50-pin miniature ribbon connector types. The most
common twisted-pair connector is an 8-position,8-contact(8P8C) modular plug and jack
commonly referred to as an RJ45 connector.
An 8-pin/8-position plug or jack is com mainly used to connect computers onto Ethernet-
based
local area networks (LAN).
The cable required is Category 5e or CAT5e.
The tool used is R.J-45 Crimping tool
Here are two kinds of Ethernet cables you can make, Straight Through and Crossover.

STRAIGHT THROUGH Ethernet cables are the standard cable used for almost all
purposes,
and are often called “patch cables”. It is highly recommend you duplicate the color order as
shown on the left. Note how the green pair is not side-by-side as are all the other pairs. This
configuration allows for longer wire runs.

CROSSOVER CABLES- The purpose of a Crossover Ethernet cable is to directly connect


one
computer to another computer (or device) without going through a router, switch or hub.
Steps to Make Ethernet Cable
1.Unroll the required length of network cable and add a little extra wire.
2. Carefully remove the outer jacket of the cable.
3.Inspect the newly revealed wires for any cuts or scrapes that expose the copper wire inside.
4.Untwist the pairs so they will lay flat between your fingers.
5.Arrange the wires based on the wiring specifications you are following.
6.Press all the wires flat and parallel between your thumb and forefinger.
7. Keep the wires flat and in order as you push them into the RJ-45 plug with the flat surface
of
the plug on top.
8. Place the wired plug into the crimping tool.
9. Repeat all of the above steps with the other end of the cable.
10. Test the cable to ensure that it will function in the field.
Aim: Implementation and study of Stop and Wait Protocol
Source Code:
#include<stdio.h>

int main()
{ int windowsize,i, sent, ack;
printf("Enter number of frames ");
scanf("%d",&windowsize);

for(i=0;i<windowsize;i++)
{ sent=i;
printf("Frame %d is sent \n",sent);
printf("Last acknowledgement recieved? ");
scanf("%d", &ack);

if(ack!=sent)
{
i--;
continue;
}

}
return 1;
}
OUTPUT:
AIM: Implementation of Go Back-N Protocol
Code:

#include<stdio.h>

int main()
{ int windowsize, ack, sent=0,i;
printf("Enter window size ");
scanf("%d", &windowsize);

while(1)
{
for(i=0;i<windowsize;i++)
{
printf("Frame %d has been transmitted \n", sent+1);
sent++;
if(sent==windowsize)
break;
}

printf("Enter number of acknowledgements recieved ");


scanf("%d",&ack);

if(ack==windowsize)
break;
else
sent=ack;
}
printf("ALL FRAMES RECIEVED");
return 1;
}
OUTPUT:
AIM: Implementation of Selective Repeat Protocol
Code:
#include<stdio.h>

int main()
{ int windowsize, nack, sent=0,i;
int ans=1;
printf("Enter window size ");
scanf("%d", &windowsize);

while(1)
{
for(i=0;i<windowsize;i++)
{
printf("Frame %d has been transmitted \n", sent+1);
sent++;
if(sent==windowsize)
break;
}

do
{
printf("Is there a negative acknowledgement ? ");
scanf("%d",&ans);

if(ans==1)
{
printf("Enter negative acknowledgement recieved ");
scanf("%d",&nack);
printf("Frame %d has been transmitted again \n",nack);
}

}while(ans==1);

if(sent==windowsize)
break;

printf("ALL FRAMES RECIEVED");


return 1;
}
OUTPUT:
AIM: Implement and study of Distance Vector Routing algorithm.
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
struct Edge
{
int source, destination, weight;
};
struct Graph
{
int V, E;
struct Edge* edge;
};
struct Graph* createGraph(int V, int E)
{
struct Graph* graph = (struct Graph*) malloc( sizeof(struct Graph));
graph->V = V;
graph->E = E;
graph->edge = (struct Edge*) malloc( graph->E * sizeof( struct Edge ) );
return graph;
}
void FinalSolution(int dist[], int n)
{
printf("\nVertex\tDistance from Source Vertex\n");
int i;
for (i = 0; i < n; ++i){
printf("%d \t\t %d\n", i, dist[i]);
}
}
void BellmanFord(struct Graph* graph, int source)
{
int V = graph->V;
int E = graph->E;
int StoreDistance[V];
int i,j;
for (i = 0; i < V; i++)
StoreDistance[i] = INT_MAX;
StoreDistance[source] = 0;
for (i = 1; i <= V-1; i++)
{
for (j = 0; j < E; j++)
{
int u = graph->edge[j].source;
int v = graph->edge[j].destination;
int weight = graph->edge[j].weight;
if (StoreDistance[u] + weight < StoreDistance[v])
StoreDistance[v] = StoreDistance[u] + weight;
}
}
for (i = 0; i < E; i++)
{
int u = graph->edge[i].source;
int v = graph->edge[i].destination;
int weight = graph->edge[i].weight;
if (StoreDistance[u] + weight < StoreDistance[v])
printf("This graph contains negative edge cycle\n");
}

FinalSolution(StoreDistance, V);
return;
}
int main()
{
int V,E,S;
printf("Enter number of vertices in graph\n");
scanf("%d",&V);
printf("Enter number of edges in graph\n");
scanf("%d",&E);
printf("Enter your source vertex number\n");
scanf("%d",&S);
struct Graph* graph = createGraph(V, E);
int i;
for(i=0;i<E;i++){
printf("\nEnter edge %d properties Source, destination, weight respectively\n",i+1);
scanf("%d",&graph->edge[i].source);
scanf("%d",&graph->edge[i].destination);
scanf("%d",&graph->edge[i].weight);
}

BellmanFord(graph, S);

return 0;
}
OUTPUT
AIM: Implement and study of Link state routing algorithm.
CODE:
#include<stdio.h>
#include<conio.h>
#define INFINITY 9999
#define MAX 10
void dijkstra(int G[MAX][MAX],int n,int startnode);
int main()
{
int G[MAX][MAX],i,j,n,u;
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");

for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
printf("\nEnter the starting node:");
scanf("%d",&u);
dijkstra(G,n,u);
return 0;
}
void dijkstra(int G[MAX][MAX],int n,int startnode)
{
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];

for(i=0;i<n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
}
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count<n-1)
{
mindistance=INFINITY;
for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}
visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance+cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}
for(i=0;i<n;i++)
if(i!=startnode)
{
printf("\nDistance of node%d=%d",i,distance[i]);
printf("\nPath=%d",i);

j=i;
do
{
j=pred[j];
printf("<-%d",j);
}while(j!=startnode);
}
}

OUTPUT
AIM: Star Topology
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>

#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/ipv4-global-routing-helper.h"
#include "ns3/netanim-module.h"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("TcpServer");

int
main (int argc, char *argv[])
{
// Users may find it convenient to turn on explicit debugging
// for selected modules; the below lines suggest how to do this

//LogComponentEnable ("TcpServer", LOG_LEVEL_INFO);


//LogComponentEnable ("TcpL4Protocol", LOG_LEVEL_ALL);
//LogComponentEnable ("TcpSocketImpl", LOG_LEVEL_ALL);
//LogComponentEnable ("PacketSink", LOG_LEVEL_ALL);
// Set up some default values for the simulation.
Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (250));
Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("5kb/s"));
uint32_t N = 9; //number of nodes in the star

// Allow the user to override any of the defaults and the above
// Config::SetDefault()s at run-time, via command-line arguments
CommandLine cmd;
cmd.AddValue ("nNodes", "Number of nodes to place in the star", N);
cmd.Parse (argc, argv);

// Here, we will create N nodes in a star.


NS_LOG_INFO ("Create nodes.");
NodeContainer serverNode;
NodeContainer clientNodes;
serverNode.Create (1);
clientNodes.Create (N-1);
NodeContainer allNodes = NodeContainer (serverNode, clientNodes);

// Install network stacks on the nodes


InternetStackHelper internet;
internet.Install (allNodes);

//Collect an adjacency list of nodes for the p2p topology


std::vector<NodeContainer> nodeAdjacencyList (N-1);
for(uint32_t i=0; i<nodeAdjacencyList.size (); ++i)
{
nodeAdjacencyList[i] = NodeContainer (serverNode, clientNodes.Get (i));
}
// We create the channels first without any IP addressing information
NS_LOG_INFO ("Create channels.");
PointToPointHelper p2p;
p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
std::vector<NetDeviceContainer> deviceAdjacencyList (N-1);
for(uint32_t i=0; i<deviceAdjacencyList.size (); ++i)
{
deviceAdjacencyList[i] = p2p.Install (nodeAdjacencyList[i]);
}

// Later, we add IP addresses.


NS_LOG_INFO ("Assign IP Addresses.");
Ipv4AddressHelper ipv4;
std::vector<Ipv4InterfaceContainer> interfaceAdjacencyList (N-1);
for(uint32_t i=0; i<interfaceAdjacencyList.size (); ++i)
{
std::ostringstream subnet;
subnet<<"10.1."<<i+1<<".0";
ipv4.SetBase (subnet.str ().c_str (), "255.255.255.0");
interfaceAdjacencyList[i] = ipv4.Assign (deviceAdjacencyList[i]);
}

//Turn on global static routing


Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

// Create a packet sink on the star "hub" to receive these packets


uint16_t port = 50000;
Address sinkLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", sinkLocalAddress);
ApplicationContainer sinkApp = sinkHelper.Install (serverNode);
sinkApp.Start (Seconds (1.0));
sinkApp.Stop (Seconds (3.0));

// Create the OnOff applications to send TCP to the server


OnOffHelper clientHelper ("ns3::TcpSocketFactory", Address ());
clientHelper.SetAttribute("OnTime", StringValue
("ns3::ConstantRandomVariable[Constant=1]"));
clientHelper.SetAttribute("OffTime", StringValue
("ns3::ConstantRandomVariable[Constant=0]"));
//normally wouldn't need a loop here but the server IP address is different
//on each p2p subnet
ApplicationContainer clientApps;
for(uint32_t i=0; i<clientNodes.GetN (); ++i)
{
AddressValue remoteAddress
(InetSocketAddress (interfaceAdjacencyList[i].GetAddress (0), port));
clientHelper.SetAttribute ("Remote", remoteAddress);
clientApps.Add (clientHelper.Install (clientNodes.Get (i)));
}
clientApps.Start (Seconds (2.5));
clientApps.Stop (Seconds (4.0));

//configure tracing
AsciiTraceHelper ascii;
p2p.EnableAsciiAll (ascii.CreateFileStream ("tcp-star-server.tr"));
p2p.EnablePcapAll ("tcp-star-server");

// ApplicationContainer clientApps=echoClient.Install(nodes.Get(0));
//clientApps.Start(Seconds(2.0));
//clientApps.Stop(Seconds(10.0));
AnimationInterface anim("rd.xml");
anim.SetConstantPosition(serverNode.Get(0), 50,20);
anim.SetConstantPosition(clientNodes.Get(1), 20,10);
anim.SetConstantPosition(clientNodes.Get(2), 20,20);
anim.SetConstantPosition(clientNodes.Get(3), 20,30);
anim.SetConstantPosition(clientNodes.Get(4), 20,40);
anim.SetConstantPosition(clientNodes.Get(5), 70,10);
anim.SetConstantPosition(clientNodes.Get(6), 70,40);
anim.SetConstantPosition(clientNodes.Get(7), 70,30);

NS_LOG_INFO ("Run Simulation.");


Simulator::Run ();
Simulator::Destroy ();
NS_LOG_INFO ("Done.");

return 0;
}

You might also like