GREATER NOIDA
(Year: 2008-09)
rd
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
8
3
4
S.No. Date
1
2
5
6
7
INDEX
Program
TCL – The Basics
The First TCL Script (template)
A TCL Script to show a simple topology with two nodes that are connected by a link
To create following nodes, links and establish a protocol between 1-1 & 2-2
Program to implement CRC (Cyclic Redundancy Check) Error
Detecting Code
Algorithm in C.
A TCL Script to show an example for a dynamic network where the routing adjusts to a link failure
Program to implement Dijkstra's algorithm in C
Program to implement Error Detection and Correction using
HAMMING CODE in C
To study different network topologies
Signature
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
We can build ns either from the the various packages (Tcl/Tk, otcl, etc.), or we can download an 'all-in-one' package. The disadvantage of the all-in-one distribution is the size, since it contains some components that you don't need anymore after you compiled ns and nam. It's still good for first tests, and you can always switch to the single-package distribution later.
Note: The all-in-one package only works on Unix systems.
We start ns with the command 'ns <tclscript>' (assuming that you are in the directory with the ns executable, or that our path points to that directory), where '<tclscript>' is the name of a Tcl script file which defines the simulation scenario (i.e. the topology and the events). We could also just start ns without any arguments and enter the Tcl commands in the Tcl shell, but that is definitely less comfortable.
Everything else depends on the Tcl script. The script might create some output on stdout, it might write a trace file or it might start nam to visualize the simulation. Or all of the above. These possibilities will all be discussed in later sections.
–
We can either start nam with the command 'nam <nam-file>' where '<nam-file>' is the name of a nam trace file that was generated by ns, or we can execute it directly out of the Tcl simulation script for the simulation which you want to visualize
In the left side we can see a screenshot of a nam window where the most important functions are being explained.
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
Aim –
The First TCL Script (template)
Program – set ns [new Simulator] set nf [open out.nam w]
$ns namtrace-all $nf
} proc finish {} { global ns nf
$ns flush-trace close $nf exec nam out.nam & exit 0
$ns at 5.0 "finish"
$ns run
Result –
C:\Program Files\Tcl\bin>ns program1.tcl
C:\Program Files\Tcl\bin>nam out.nam
(‘$ns run’ executes the script automatically.)
( Compiling the code )
( Executing the Script )
A Simple Template
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
Aim –
A TCL Script to show a simple topology with two nodes that are connected by a link.
Program – set ns [new Simulator] set nf [open out.nam w]
$ns namtrace-all $nf set n0 [$ns node] set n1 [$ns node]
$ns duplex-link $n0 $n1 Mb 10ms DropTail
#Create a UDP agent and attach it to node n0 set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
#Create a CBR traffic source and attach it to udp0 set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
# Create a Null agnet which acts as traffic sink and attach it to all node n1. set null0 [new Agent/Null]
$ns attach-agent $n1 $null0 proc finish {} { global ns nf
$ns flush-trace close $nf exec nam out.nam &
} exit 0
# Now Connecting both the agents together
$ns connect $udp0 $null0
$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"
$ns at 5.0 "finish"
$ns run
Result – Screen Shots to show the same.
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
(a) Two nodes created and assigned n0 & n1.
(b) node 0 starts sending data packets to node 1
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
Aim –
To create following nodes, links and establish a protocol between 1-1 & 2-2.
Program – set ns [new Simulator] set nf [open out.nam w]
$ns namtrace-all $nf set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] set n4 [$ns node] set n5 [$ns node]
$ns duplex-link $n0 $n2 Mb 10ms DropTail
$ns duplex-link $n1 $n2 Mb 10ms DropTail
$ns duplex-link $n2 $n3 Mb 10ms DropTail
$ns duplex-link $n3 $n4 Mb 10ms DropTail
$ns duplex-link $n3 $n5 Mb 10ms DropTail
#1-1 set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0 set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0 set null0 [new Agent/Null]
$ns attach-agent $n5 $null0
$ns connect $udp0 $null0
#2-2 set udp1 [new Agent/UDP]
$ns attach-agent $n1 $udp1 set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp0 set null1 [new Agent/Null]
$ns attach-agent $n4 $null1
$ns connect $udp1 $null1 proc finish {} { global ns nf
$ns flush-trace close $nf
} exec nam out.nam & exit 0
$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"
$ns at 0.5 "$cbr1 start"
$ns at 4.5 "$cbr1 stop"
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
$ns at 5.0 "finish"
$ns run
Result –
See the Screenshot.
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
Aim –
Program to implement CRC (Cyclic Redundancy Check) Error Detecting Code
Algorithm in C.
G(x)=x 4 + x + 1 (considered for the program)
Program –
#include<stdio.h>
#include<string.h>
#include<conio.h> char xor(char,char); void main()
{ int i,j,l,index,k; char code[20],temp[20],temp2[20],div[6]; clrscr(); printf("\n Enter the FRAME : "); scanf("%s",code); l=strlen(code); strcpy(temp,code); strncat(temp,"0000",l); strcpy(temp2,temp); strcpy(div,"10011"); l=+5; i=0;
//index=4; while(l>1)
{
for(j=0;j<5;j++,i++)
temp[i]=xor(temp[i],div[j]);
i=0;
while(temp[i]!='1')
{
i++;
}
l--;
} printf("\n The code to be transmitted = %s",code); while(temp[i]=='1')
{
while(temp[i])
{
printf("%c",temp[i]);
i++;
}
i++;
} getch();
} char xor(char a, char b)
{
if(a==b)
return '0';
else
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
return '1';
}
Result –
Enter the FRAME : 110101011
The code to be transmitted = 1101010111101010110000
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
Aim –
A TCL Script to show an example for a dynamic network where the routing adjusts to a link failure.
Program – set ns [new Simulator] set nf [open out.nam w]
$ns namtrace-all $nf proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}
$ns rtproto DV set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] set n4 [$ns node] set n5 [$ns node] set n6 [$ns node]
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n2 $n3 1Mb 10ms DropTail
$ns duplex-link $n3 $n4 1Mb 10ms DropTail
$ns duplex-link $n4 $n5 1Mb 10ms DropTail
$ns duplex-link $n5 $n6 1Mb 10ms DropTail
$ns duplex-link $n6 $n0 1Mb 10ms DropTail set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0 set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0 set null6 [new Agent/Null]
$ns attach-agent $n6 $null6
$ns connect $udp0 $null6
$ns at 0.5 "$cbr0 start"
$ns rtmodel-at 1.0 down $n0 $n6
$ns rtmodel-at 2.0 up $n0 $n6
$ns at 4.5 "$cbr0 stop"
$ns at 5.0 "finish"
$ns run
Result – Screen Shots to show the same.
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
Aim
– Program to implement Dijkstra's algorithm in C.
Program
–
#include <stdio.h>
#include <limits.h>
#include <assert.h> typedef enum {false, true} bool; typedef char *string;
#define oo UINT_MAX /* infinity is represented as the maximum unsigned int. */ typedef unsigned int value;
/* Firstly, the type of vertices for our particular graph (which was
given as an example in the lectures). The (non)vertex `nonexistent'
is used in order to indicate that a vertex hasn't got a predecessor
in the path under consideration. See below. */ typedef enum { HNL, SFO, LAX, ORD, DFW, LGA, PVD, MIA, nonexistent} vertex;
/* If you modify the above, in order to consider a different graph,
then you also have to modify the following. */ const vertex first_vertex = HNL; const vertex last_vertex = MIA;
#define no_vertices 8 /* NB. This has to be the same as the number
(last_vertex + 1), which unfortunately
doesn't compile... */
/* Now a dictionary for output purposes. The order has to match the
above, so that, for example, name[SFO]="SFO". */ string name[] = {"HNL","SFO","LAX","ORD","DFW","LGA", "PVD", "MIA"};
/* Now the weight matrix of our graph (as given in the
lectures). Because the graph is undirected, the matrix is
symmetric. But our algorithm is supposed to work for directed
graphs as well (and even for disconnected ones - see below). */ value weight[no_vertices][no_vertices] =
{
/* HNL SFO LAX ORD DFW LGA PVD MIA */
{ 0, oo, 2555, oo, oo, oo, oo, oo}, /* HNL */
{ oo, 0, 337, 1843, oo, oo, oo, oo}, /* SFO */
{ 2555, 337, 0, 1743, 1233, oo, oo, oo}, /* LAX */
{ oo, 1843, 1743, 0, 802, oo, 849, oo}, /* ORD */
{ oo, oo, 1233, 802, 0, 1387, oo, 1120}, /* DFW */
{ oo, oo, oo, oo, 1387, 0, 142, 1099}, /* LGA */
{ oo, oo, oo, 849, oo, 142, 0, 1205}, /* PVD */
{ oo, oo, oo, oo, 1120, 1099, 1205, 0} /* MIA */
};
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* The implementation of Dijkstra's algorithm starts here. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
/* We first declare the array `D' of overestimates, the array `tight'
which records which of those estimates are actually tight, and the
array `predecessor'. The last has the property that predecessor[z]
is the last vertex visited on the way from the start node to z. */ value D[no_vertices]; bool tight[no_vertices]; vertex predecessor[no_vertices];
/*
We begin with the following auxiliary function, which should be
called only when we know for sure that there is at least one node u
with tight[u] false. Otherwise, the program aborts; this happens
when the assertion below fails.
*/ vertex minimal_nontight() {
vertex j, u;
for (j = first_vertex; j <= last_vertex; j++)
if (!tight[j])
break;
assert(j <= last_vertex);
u = j;
/* Now u is the first vertex with nontight estimate, but not
necessarily the one with minimal estimate, so we aren't done
yet. */
for (j++; j <= last_vertex; j++)
if (!tight[j] && D[j] < D[u])
u = j;
/* Having checked all vertices, we now know that u has the required
minimality property. */
return u;
}
/*
The following definition assumes that the graph is directed in
general, which means that it would be wrong to have weight[z][u]
instead. The intended meaning is that the vertex u has the vertex z
as one of its successors. Recall that such a vertex z was
called a neighbour of the vertex u, but this latter terminology is
best reserved for undirected graphs. Notice that the example given
above happens to be undirected. But, as remarked above, this
program is intended to work for directed graphs as well.
*/ bool successor(vertex u, vertex z) {
return (weight[u][z] != oo && u != z);
}
/*
We finally arrive at the main algorithm. This is the same as given
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
above, now written in C, with the computation of the actual
paths included. The computation of paths is achieved via the use of
the array `predecessor' declared above. Strictly speaking,
Dijkstra's algorithm only records what is needed in order to recover
the path. The actual computation of the path is performed by the
algorithm that follows it.
*/ void dijkstra(vertex s) {
vertex z, u;
int i;
D[s] = 0;
for (z = first_vertex; z <= last_vertex; z++) {
if (z != s)
D[z] = oo;
tight[z] = false;
predecessor[z] = nonexistent;
}
for (i = 0; i < no_vertices; i++) {
u = minimal_nontight();
tight[u] = true;
/* In a disconnected graph, D[u] can be oo. But then we just move
on to the next iteration of the loop. (Can you see why?) */
if (D[u] == oo)
continue; /* to next iteration ofthe for loop */
for (z = first_vertex; z <= last_vertex; z++)
if (successor(u,z) && !tight[z] && D[u] + weight[u][z] < D[z]) {
D[z] = D[u] + weight[u][z]; /* Shortcut found. */
predecessor[z] = u;
}
}
}
/* The conditions (successor(u,z) && !tight[z]) can be omitted from
the above algorithm without changing its correctness. But I think
that the algorithm is clearer with the conditions included, because
it makes sense to attempt to tighten the estimate for a vertex only
if the vertex is not known to be tight and if the vertex is a
successor of the vertex under consideration. Otherwise, the
condition (D[u] + weight[u][z] < D[z]) will fail anyway. However,
in practice, it may be more efficient to just remove the conditions
and only perform the test (D[u] + weight[u][z] < D[z]). */
/* We can now use Dijkstra's algorithm to compute the shortest path
between two given vertices. It is easy to go from the end of the
path to the beginning.
To reverse the situation, we use a stack. Therefore we digress
slightly to implement stacks (of vertices). In practice, one is
likely to use a standard library instead, but, for teaching
purposes, I prefer this program to be selfcontained. */
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
#define stack_limit 10000 /* Arbitrary choice. Has to be big enough to
accomodate the largest path. */ vertex stack[stack_limit]; unsigned int sp = 0; /* Stack pointer. */ void push(vertex u); bool stack_empty(); vertex pop();
/* End of stack digression and back to printing paths. */ void print_shortest_path(vertex origin, vertex destination); int main()
{ print_shortest_path(SFO,MIA); getch(); return 0;
} void push(vertex u)
{
assert(sp<stack_limit); /* We abort if the limit is exceeded. This
will happen if a path with more
vertices than stack_limit is found. */
stack[sp]=u;
sp++;
} bool stack_empty()
{
return (sp == 0);
} vertex pop()
{
assert(!stack_empty()); /* We abort if the stack is empty. This will
happen if this program has a bug. */
sp--;
return stack[sp];
}
/* End of stack digression and back to printing paths. */ void print_shortest_path(vertex origin, vertex destination)
{
vertex v;
assert(origin != nonexistent && destination != nonexistent);
dijkstra(origin);
printf("The shortest path from %s to %s is:\n\n",name[origin], name[destination]);
for (v = destination; v != origin; v = predecessor[v])
if(v == nonexistent)
{
printf("nonexistent (because the graph is disconnected).\n");
return;
}
else
push(v);
push(origin);
while(!stack_empty())
printf(" %s",name[pop()]);
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
printf(".\n\n");
}
Result
–
The shortest path from SFO to MIA is:
SFO LAX DFW MIA.
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
Aim
– Program to implement Error Detection and Correction using HAMMING CODE in C.
Program
–
#include<stdio.h> int factors(int); main()
{ char code[16]; int i,j=0,l,num,freq[50]; int count_1=0,count_2=0,count_4=0,count_8=0; int esum=0; printf("\n Enter the CODE (max length = 15) -> "); scanf("%s",code); l=strlen(code); for(i=0;i<l;i++)
{
//printf("\ncode[%d] = %c ",i+1,code[i]);
if((i!=0 && i!=1 && i!=3 && i!=7)&&(code[i]=='1'))
{
//Building the frequency array.
//printf("\n--> code[%d] = %c ",i+1,code[i]);
num=i+1;
while(num>0)
{
freq[j]=factors(num);
num=num-freq[j];
j++;
}
}
}
// Checking the frequencies for(i=0;i<j;i++)
{
if(freq[i]==1)
{count_1++;}
if(freq[i]==2)
{count_2++;}
if(freq[i]==4)
{count_4++;}
if(freq[i]==8)
{count_8++;}
}
//for 1 if((count_1%2)==0 && code[0]=='1')
esum+=1;
else
if((count_1%2)==1 && code[0]=='0')
esum+=1;
//for 2 if((count_2%2)==0 && code[1]=='1')
esum+=2;
else
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
if((count_2%2)==1 && code[1]=='0')
esum+=2;
//for 4 if((count_4%2)==0 && code[3]=='1')
esum+=4;
else
if((count_4%2)==1 && code[3]=='0')
esum+=4;
//for 8 if((count_8%2)==0 && code[7]=='1')
esum+=8;
else
if((count_8%2)==1 && code[7]=='0')
esum+=8; if(esum!=0)
{
printf("\n The error is in '%d' digit :: code[%d] = %c",esum,esum,code[esum-1]);
if(code[esum-1]=='0')
code[esum-1]='1';
else
code[esum-1]='0';
printf("\n The Corrected Code = %s",code);
} else
printf("\n NO ERROR EXISTS !! "); getch();
} int factors(int num)
{
if(num>=8)
return 8;
else
if(num>=4)
return 4;
else
if(num>=2)
return 2;
else
return 1;
}
Result
–
1.
Enter the CODE(max length = 15) -> 00111100101
The error is in ' 6 ' digit :: code[6] = 1
The Corrected Code = 00111000101
2.
Enter the CODE (max length = 15) -> 0011100111
The error is in ' 9 ' digit :: code[9] = 1
The Corrected Code = 0011100101
3.
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
Enter the CODE(max length = 15) -> 00111000101
NO ERROR EXISTS !!
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
Aim
– To study different network topologies.
The arrangement or mapping of the elements of a network gives rise to certain basic topologies which may then be combined to form more complex topologies (hybrid topologies). The most common of these basic types of topologies are
(refer to the illustration at the top right of this page):
Bus (Linear, Linear Bus)
Star
Ring
Mesh partially connected mesh (or simply 'mesh') fully connected mesh (or simply fully connected)
Tree
Hybrid
There are also three basic categories of network topologies:
physical topologies
signal topologies
logical topologies
The terms signal topology and logical topology are often used interchangeably even though there is a subtle difference between the two and the distinction is not often made between the two.
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)
Devices are connected with many redundant interconnections between network nodes . In a true mesh topology every node has a connection to every other node in the network.
All devices are connected to a central hub .
Nodes communicate across the network by passing data through the hub.
All devices are connected to a central cable, called the bus or backbone .
All devices are connected to one another in the shape of a closed loop, so that each device is connected directly to two other devices, one on either side of it.
A hybrid topology. Groups of star-configured networks are connected to a linear bus backbone.
Plot No. 6-A&7, Institutional Area, Knowledge Park-1, Greater Noida-201306 (U.P.)