Experiment 7: AIM - Simulate Bus Topology Using NS3 With 8 Csma Nodes. Theory

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Experiment 7

AIM – Simulate bus topology using NS3 with 8 csma nodes.


Theory –
 CsmaHelper - build a set of CsmaNetDevice objects. CsmaNetDevice is a device for a
Csma Network Link.
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/netanim-module.h"
#include "ns3/ipv4-global-routing-helper.h"
using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");

int main (int argc, char *argv[])


{
bool verbose = true;
uint32_t nCsma = 7;

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

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

ARIHAANT ARORA
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

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


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

Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
//static route is provided to network

pointToPoint.EnablePcapAll ("second"); //packet capture


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

AnimationInterface anim("secondscratch2.xml");
anim.SetConstantPosition(p2pNodes.Get(0),10.0, 10.0);
anim.SetConstantPosition(csmaNodes.Get(0),20.0, 20.0);
anim.SetConstantPosition(csmaNodes.Get(1),30.0, 30.0);
anim.SetConstantPosition(csmaNodes.Get(2),40.0, 40.0);
anim.SetConstantPosition(csmaNodes.Get(3),50.0, 50.0);
anim.SetConstantPosition(csmaNodes.Get(4),60.0, 60.0);
anim.SetConstantPosition(csmaNodes.Get(5),70.0, 70.0);
anim.SetConstantPosition(csmaNodes.Get(6),80.0, 80.0);
anim.SetConstantPosition(csmaNodes.Get(7),90.0, 90.0);

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

ARIHAANT ARORA
Output

Fig 7.1

Fig 7.2

ARIHAANT ARORA
Experiment 8
AIM: Implement and study of Link state routing protocol.
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];

ARIHAANT ARORA
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++;

ARIHAANT ARORA
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

Fig 8.1

ARIHAANT ARORA
Experiment 9
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]);
}
}

ARIHAANT ARORA
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;

ARIHAANT ARORA
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, weightrespectively\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

Fig.9.1

ARIHAANT ARORA
Experiment 10
AIM: Implementation of Go-Back-N and Selective Repeat Protocol
Code:
Go-Back-N:
#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;
}

ARIHAANT ARORA
OUTPUT:

Fig. 10.1

Selective Repeat:
#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)
{

ARIHAANT ARORA
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:

Fig10.2

ARIHAANT ARORA
Experiment 11
AIM: Create a Straight Cable and crossover cable using RJ45 Connector
An Ethernet cable is a network cable used for high-speed wired network connections between two
devices. This network cable is made of four-pair cable,which is consists of twisted pair conductors.
It is used for data transmission at both ends of the cable, which is called RJ45 connector. The
Ethernet cables are categorized as Cat 5, Cat 5e, Cat 6, and UTP cable. Cat 5 cable can support a
10/100 Mbps Ethernet network while Cat 5e and Cat 6 cable to support Ethernet
A registered jack(RJ) is a standardized physical network interface for connecting
telecommunications or data equipment. The physical connectors that registeredjacks 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 plugand jack
commonly referred to as an RJ45 connector.
An 8-pin/8-position plug or jack is com mainly used to connect computers ontoEthernet-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 andCrossover.

STRAIGHT THROUGH Ethernet cables are the standard cable used for almost allpurposes,
and are often called “patch cables”. It is highly recommend you duplicate thecolor order as
shown on the left. Note how the green pair is not side-by-side as are all the otherpairs. This
configuration allows for longer wire runs.
Straight-through cable is a type of CAT5 with RJ-45 connectors at each end, and each has the same
pin out. It is in accordance with either the T568A or T568B standards. It uses the same color code
throughout the LAN for consistency. This type of twisted-pair cable is used in LAN to connect a
computer or a network hubsuch as a router. It is one of the most common types of network cable.

CROSSOVER CABLES- The purpose of a Crossover Ethernet cable is to directlyconnect one


computer to another computer (or device) without going through a router, switchor hub.
A Crossover cable is a type of CAT 5 where one end isT568A configuration and theother end as
T568BConfiguration. In this type of cable connection, Pin 1 is crossedwith Pin 3, and Pin 2 is
crossed with Pin 6.
Crossover cable is used to connect two or more computing devices. The internal wiring of

ARIHAANT ARORA
crossover cables reverses the transmission and receive signals. It is widely used to connect two
devices of the same type: e.g., two computers or twoswitches to each other.
In regard to physical appearance, Crossover Ethernet cables are very much similar to regular
Ethernet cables. Still, they are different with regard to the order with which the wires are arranged.
This type of Ethernet cable is made to connect to network devices of the same kind over Ethernet
directly. Crossover cables are mostly used to connect two hosts directly.

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 theflat 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.

ARIHAANT ARORA
ARIHAANT ARORA
Experiment 12
AIM- Implementation and study of stop and wait protocol.
Code-
#include<bits/stdc++.h>
using namespace std;
int main()
{
int windowsize,i,sent,ack;
cout<<"Enter number of frames:\n";
cin>>windowsize;
for(int i=0;i<window;size;++i)
{
sent=i;
cout<<"Frame sent "<<sent<<endl;;
cout<<"Last acknowledgement
recieved? ";
cin>>ack;

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

ARIHAANT ARORA
Output

Fig12.1

ARIHAANT ARORA
Experiment 13
AIM: Simulate the given architecture in NS3.
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);

ARIHAANT ARORA
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.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 ();
Simulator::Destroy();
return 0;
}

ARIHAANT ARORA
Fig: 13.1 Terminal Output

Fig:- 13.2 NetAnim Output

ARIHAANT ARORA
Experiment 14
AIM:- Implementation of TCP Star Server

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 argumentsCommandLine
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

ARIHAANT ARORA
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 packetsuint16_t port =
50000;
Address sinkLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));PacketSinkHelper
sinkHelper ("ns3::TcpSocketFactory", sinkLocalAddress); ApplicationContainer sinkApp =
sinkHelper.Install (serverNode);

ARIHAANT ARORA
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.");

ARIHAANT ARORA
Simulator::Run ();
Simulator::Destroy ();
NS_LOG_INFO ("Done.");
return 0;
}

Fig:- 14.1 Terminal Output

Fig:- 14.2 NetAnim Output

ARIHAANT ARORA

You might also like