You are on page 1of 34

CCN Laboratory (TEL76) 2018-19

Part A:
Implementation of experiments using C Language

VII Semester Dept. of TCE, Dr. AIT Page 1


CCN Laboratory (TEL76) 2018-19

1. BIT STUFFING AND DESTUFFING

Theory:
Framing involves identifying the beginning and end of a block of information within
a digital stream. In asynchronous data transmission, since transmissions do not occur at
regular intervals, the receiver resynchronizes at the start of each eight bit character by the use
of a start bit that precedes and a stop bit that ends each character .In synchronous data
transmission bits are transmitted at regular intervals and the receiver has the circuitry that
recovers and tracks the frequency and bit transitions of the received data Framing may
involve delineating the boundaries between frames that are of fixed length or it may involve
delineating between frames that are of variable length. Variable length frames need more
information to delineate. The methods available include:

Special characters to identify beginning and end of frame.

Special bit patterns-“flags” to identify the beginning and end of frames and character
counts

Bit Stuffing:

Flag based synchronization was developed to transfer an arbitrary number of bits


within a frame. The figure below shows the structure of an HDLC frame. The beginning and
end of an HDLC frame is indicated by the presence of an eight bit flag. The flag in HDLC
consists of the byte 01111110 that is HEX 7E . Bit stuffing prevents the occurrence of the
flag inside the frame. The transmitter examines the contents of the frame and inserts an extra
0 after each instance of five consecutive 1s. The transmitter then attaches the flag at the
beginning and end of the resulting bit stuffed frame. The receiver looks for five consecutive
1s in the received sequence. Five 1s followed by a 0 indicate that the 0 is a stuffing bit and so
the bit is removed. Five consecutive 1s followed by 10 indicate a flag. Five 1 s followed by
11 indicate an error.

HDLC Frame
The high level data link control protocol is a bit oriented protocol which uses bit
stuffing for data transparency. HDLC frame is as shown.
Bits 8 8 8 >0 16 8
Frame 0111 1110 Address Control Data Checksum 0111 1110

The Control field is used for sequence numbers and acknowledgements and other purposes.
The Data field may contain arbitrary information. The Checksum field is a minor variation of

VII Semester Dept. of TCE, Dr. AIT Page 2


CCN Laboratory (TEL76) 2018-19

the CRC code using CRC-CCITT as the generator. Frames are delimited by 0111 1110 and
this sequence is not allowed in the data field.
Algorithm:

 Input data sequence.


 Add start of frame (flag) bits to output sequence.
 For every bit in input
o Append bit to output sequence
o Is bit a 1?
 Yes:
▬ Increment count,
▬ If count is 5,append 0 to output sequence and reset count
 No:
▬ Set count to 0.
 Add stop of frame (flag) bits to output sequence.

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
int si=0,di=0,count=0;
char flag[]="01111110";
char src[100],des[100];
clrscr();
printf("Enter the frame data in binary");
scanf("%s",src);
strcpy(des,flag);
di=strlen(flag);
while(src[si] != '\0')
{
if(src[si] == '1')
count++;
else
count=0;
des[di++] = src[si++];
if(count == 5 )
{

VII Semester Dept. of TCE, Dr. AIT Page 3


CCN Laboratory (TEL76) 2018-19

des[di++] = '0';
count=0;
}
}
des[di] = '\0';
strcat(des,flag);
printf("\nBit stuffed frame is : \n%s",des);
di=strlen(flag); si=0;count=0;
while(des[di] != '\0')
{
if(des[di] == '1')
count++;
else
count=0;
src[si++] = des[di++];
if(count == 5 )
{
di++;
count=0;
}
}
src[si-7] = '\0';
printf("\nBit destuffed frame is : \n%s",src);
getch();
}

Output:
For example:

Enter the frame data in binary:


11111111011
Bit stuffed frame is:
01111110 111110111011 01111110

Flag /Inserted „0‟ after five ones/flag

Bit destuffed frame is :


11111111011

Note: Check the output for 2 different sets of input.

VII Semester Dept. of TCE, Dr. AIT Page 4


CCN Laboratory (TEL76) 2018-19

2. CHARACTER STUFFING AND DESTUFFING


Theory:
Character based frame synchronization methods are used when the information in a
frame consists of an integer number of characters. For example, asynchronous transmission
systems are used extensively to transmit sequences of printable characters using 8 bit ASCII
code . To delineate a frame of characters, special eight bit codes that do not correspond to
printable characters are used as control characters. Character-oriented framing was popular
when only text was exchanged by the data link layers.
The beginning and end of an HDLC frame is indicated by the presence of an eight bit
flag. The flag in HDLC is DLE . Bit stuffing prevents the occurrence of the flag inside the
frame. The transmitter examines the contents of the frame and inserts an extra ESC before
DLE. The transmitter then attaches the flag at the beginning and end of the resulting bit
stuffed frame. The receiver looks for DLE. If ESC is followed by DLE, indicates that the
ESC is stuffed and so the ESC is removed.

Algorithm:

 Input data sequence


 Add start of frame (DLE) characters to output sequence.
o Append character to output sequence
o Is character DLE?
 Yes: Add ESC to output sequence.
o Is character ESC?
 Yes: Add ESC to output sequence.
 Add stop of frame (DLE) characters to the output sequence.

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
int si=0,di=0,count=0;
char flag[]="DLE";
char src[100],dest[100];
clrscr();
printf("Enter the frame data in ASCII\n");
gets(src);
VII Semester Dept. of TCE, Dr. AIT Page 5
CCN Laboratory (TEL76) 2018-19

strcpy(dest,flag);
di=strlen(flag);
while(src[si] != '\0')
{
if(src[si] == 'D' && src[si+1]=='L' && src[si+2] == 'E' )
{
dest[di+0] = 'E';dest[di+1] = 'S';dest[di+2] = 'C';
dest[di+3] = 'D';dest[di+4] = 'L';dest[di+5] = 'E';
di+=6; si+=3;
}
else
if(src[si] == 'E' && src[si+1]=='S' && src[si+2] == 'C' )
{
dest[di+0] = 'E';dest[di+1] = 'S';dest[di+2] = 'C';
dest[di+3] = 'E';dest[di+4] = 'S';dest[di+5] = 'C';
di+=6; si+=3;
}
else
dest[di++] = src[si++];
}
dest[di] = '\0';
strcat(dest,flag);
printf("\nCharecter stuffed frame is : \n %s",dest);
si=0;
for(di=strlen(flag);di<(strlen(dest)-strlen(flag));di++)
{
if(dest[di] == 'E'&&dest[di+1]=='S'&&dest[di+2]=='C')
{
src[si] = dest[di+3]; src[si+1] = dest[di+4];src[si+2] = dest[di+5];
di=di+5;
si+=3;
}
else
src[si++] = dest[di];
}
src[si]='\0';
printf("\nAfter destuffing : \n%s",src);
getch();
}

VII Semester Dept. of TCE, Dr. AIT Page 6


CCN Laboratory (TEL76) 2018-19

Output:
For example:
Enter frame data in ASCII :
AN IDLE MIND ALWAYS ESCAPES FROM WORK
Character stuffed frame is :
DLEAN IESCDLE MIND ALWAYS ESCESCAPES FROM WORKDLE
After destuffing :
AN IDLE MIND ALWAYS ESCAPES FROM WORK
Note: Check the output for 2 different sets of input.

VII Semester Dept. of TCE, Dr. AIT Page 7


CCN Laboratory (TEL76) 2018-19

3. ENCRYPTION AND DECRYPTION USING


SUBSTITUTION METHOD
Theory:
The science and art of manipulating messages to make them secure is called
Cryptography. An original message to be transformed is called the plain text and the
resulting message after the transformation is called the cipher text. The process of converting
the plain text into cipher text is called encryption. The reverse process is called decryption.
The algorithm used for encryption and decryption is often called a cipher. Substitution
ciphers are a common technique for altering messages in games and puzzles. Each letter of
the alphabet is mapped into another letter. The cipher text is obtained by applying the
substitution defined by the mapping to the plain text. Transposition ciphers are another type
of encryption scheme. Here the order in which the letters appear is altered. The letters may be
written into an array in one order and read out in a different order.
In cryptography, the messages to be encrypted; known as plaintext, are transformed
by a function that is parameterized by a key. The output of the encryption process, known as
cipher text, is then transmitted, often by messenger or radio. We assume that the enemy or
intruder, hears and accurately copies down the complete cipher text. However, unlike the
intended recipient, he does not know what the decryption key is and so cannot decrypt the
cipher text easily. Sometimes the intruder cannot only listen to the communication channel
(passive intruder) but can also record messages and play them later, inject his own messages,
or modify legitimate messages before they get to the receiver (active intruder). The art of
breaking ciphers is called cryptanalysis.

In a substitution cipher, each letter or group of letters is replaced by another letter by adding
key to disguise it.

Plaintext: a b c d e f g h i j k l m n o p q r s t u v w x y z
Key=2
Ciphertext: c d e f g h i j k l m n o ......
Example : attack
Key=3
Encrypted data: dwwdfo

Algorithm:
(a) Encryption:
 Read data to be encoded.
 Get the key, ie the number of shifts to be performed.
 For every character of data, remove all the special characters and change to capitals.

VII Semester Dept. of TCE, Dr. AIT Page 8


CCN Laboratory (TEL76) 2018-19

 Perform the required number of shifts according to the key.


 Print the encoded data.

(b) Decryption:
 Get the data for decryption.
 For every character of cipher subtract the key for decryption from the cipher
 Print the decryption data.

Program:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>

void main()
{
int shift,pi,ci;
char plain[100],cipher[100];
clrscr();
printf("\n\n\t\tEncrypt n decrypt using sub cipher\n");
printf("Enter the plaintext\n");
gets(plain);

while(1)
{
printf("\n Key for encryption : ");
scanf("%d",&shift);
if(shift<1 || shift >25 )
printf("\nEnter within range");
else
break;
}

printf("\nAfter removing non-alpha char and capatilising\n");


for( ci=0,pi=0;plain[pi]!='\0';pi++)
if(isalpha(plain[pi]))
{
putchar(toupper (plain[pi]));
cipher[ci++]= ((toupper(plain[pi])-'A')+shift%26)%26 + 'A';
}
cipher[ci]='\0';
VII Semester Dept. of TCE, Dr. AIT Page 9
CCN Laboratory (TEL76) 2018-19

printf("\n\nAfter encryption :\n%s",cipher);


while(1)
{
printf("\n\nkey for decryption : ");
scanf("%d",&shift);
if(shift<1 || shift >25 )
printf("\nEnter within range");
else
break;
}

for( ci=0,pi=0;cipher[ci]!='\0';ci++)
plain[pi++]= ((cipher[ci])-'A'+(26-shift))%26 + 'A';
plain[pi]='\0';
printf("\n\nAfter decryption :\n %s",plain);
getch();
}

Output:
For example:

Encrypt n decrypt using SUB CIPHER


Enter the plaintext :
Telecommunication 03

Key for encryption : 3

After removing non-alpha char and capatilising :


TELECOMMUNICATION
After encryption :
WHOHFRPPXQLFDWLRQ

key for decryption : 3

After decryption :
TELECOMMUNICATION

Note: Check the output for 2 different sets of input.

VII Semester Dept. of TCE, Dr. AIT Page 10


CCN Laboratory (TEL76) 2018-19

4. ENCRYPTION AND DECRYPTION USING


TRANSPOSITION METHOD
Theory:
Substitution ciphers preserve the order of the plaintext symbols but disguise them.
Transposition ciphers, in contrast, reorder the letters but do not disguise them. The figure
below depicts a common transposition cipher, the columnar transposition. The cipher is
keyed by a word or phrase not containing any repeated letters. In this example, MEGABUCK
is the key. The purpose of the key is to number the columns, column 1 being under the letter
closet to the start of the alphabet and so on. The plain text is written horizontally, in rows.
The cipher text is read out by columns, starting with the column whose key letter is the
lowest.

Algorithm:

(a) Encryption:
 Get sequence of characters in cipher i.e. DRAIT.
 Get data to be encoded.
 Arrange data horizontally under DRAIT.
 Add „.‟To make last row complete.
 For every column of DRAIT.
 Find next column to send using sequence.
 Send data under this letter i.e print the data under this letter.
 Jump back to 5 until all the columns are done.

(b) Decryption:
 Get sequence of characters in word i.e. DRAIT.
 Get data to be decoded.
 See if input has a multiple of length of word characters. If it is not multiple.
Print it is an error and quit.
 Find position of first character ie A
 Put size of data or word characters underneath it.
 Repeat steps 4 and 5 of DRAIT in alphabetical order.
 Print all characters row wise starting from D to T.

This is explained below:


Let keyword be: DRAIT
Plain text be : TELECOMMUNICATION

VII Semester Dept. of TCE, Dr. AIT Page 11


CCN Laboratory (TEL76) 2018-19

Now, plain text is written horizontally as a series of rows. This is shown below.

D R A I T Keyword

2 4 1 3 5 Order in which columns are to be read.

T E L E C

O M M U N

I C A T I

O N T C E Fill in with dummy characters to complete last


Row

Cipher text is read column wise as LMATTOIOEUTCEMCN

Program:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
void main()
{
charpln[100],out[100],key[20],ch;
int i,j,ilen,keylen,k;
clrscr();
printf("\n Enter the Plain text to be encrypted: ");
gets(pln);
printf("\n Enter the key: ");
gets(key);
ilen = strlen(pln);
keylen = strlen(key);
for(ch='A',k=0;ch<='z';ch++)
{
for(j=0;j<keylen;j++)
{
if(toupper(key[j]==ch))
{
for(i=j;i<ilen;i+=keylen)
{
out[k++]=pln[i];

VII Semester Dept. of TCE, Dr. AIT Page 12


CCN Laboratory (TEL76) 2018-19

}
}
}
}
out[k]='\0';
printf("\n Encrypted data is : %s",out);

printf(“\n Enter the key for Decryption :” );


gets(key);

for(ch='A',k=0;ch<='z';ch++)
{
for(j=0;j<keylen;j++)
{
if(toupper(key[j]==ch))
{
for(i=j;i<ilen;i+=keylen)
{
pln[i]=out[k++];
}
}
}
}
pln[++i]='\0';
printf("\n The Decrypted data is : %s",pln);
getch();
}

Output:
For example:

Encryption & Decryption using TRANSPOSITION CIPHER

Enter Plain text:


TELECOMMUNICATION

Enter the key for Encryption:


DRAIT

The Encrypted data is:

VII Semester Dept. of TCE, Dr. AIT Page 13


CCN Laboratory (TEL76) 2018-19

LMATTOIOEUTCEMCN

Enter the key for Decryption:


DRAIT

The Decrypted data is:


TELECOMMUNICATION

Note: Check the output for 2 different sets of input.

VII Semester Dept. of TCE, Dr. AIT Page 14


CCN Laboratory (TEL76) 2018-19

5. COMPUTATION OF POLYNOMIAL CHECKSUM


Theory:
The polynomial code (also known as a cyclic redundancy code or CRC code) is
widely used. Polynomial codes are based upon treating bit strings as representations of
polynomials with coefficients of 0 and 1 only. A k-bit frame is regarded as the coefficient list
for a polynomial with k terms, ranging from xk-1 to x0. Such a polynomial is said to be of a
degree k-1. The high-order (left most) bit is the coefficient of xk-1; the next bit is the
coefficient of xk-2, and so on. For example, 110001 has 6 bits and thus represents a six-term
polynomial with coefficients 1,1,0,0,0 and 1 : x5 + x4 + x0.
When the polynomial code method is employed, the sender and receiver must agree
upon a generator polynomial, G(x), in advance. Both the high and low-order bits of the
generator must be 1. To compute the checksum for some frame with m bits, corresponding to
the polynomial M(x), the frame must be longer than the generator polynomial. The idea is to
append a checksum to the end of the frame in such a way that the polynomial represented by
the check summed frame is divisible by G (x). When the receiver gets the check summed
frame, it tries dividing it by G(x). If there is a remainder, there has been a transmission error.

Algorithm:
(a) Encryption:
 Get message polynomial
 Get generator polynomial = (x16 +x12 + x5)
 Multiply M(x) by (x16) to get v(x)
 Divide v(x) by G(x) to get the remainder polynomial R(x) of the degree<15
 Add R(x) to V(x) to get the encoded polynomial.
 Display the encoded polynomial.

(b) Decryption:
 Obtain received message in M(x) and store the CRC generate polynomial in G(x)
 Divide M(x) by G(x) to get a remainder polynomial
 Check for r(x)
 If r(x)=0,then no errors have occurred in the transmission.
 Otherwise some errors were induced during transmission.
 Output y the value of R(x).

Program:

#include<stdio.h>
#include<conio.h>

VII Semester Dept. of TCE, Dr. AIT Page 15


CCN Laboratory (TEL76) 2018-19

#include<ctype.h>
#include<string.h>

unsigned int xor2_div(char *i, char *o, int mode)


{
unsigned int j,k;
char g[100]={"10011"};
strcpy(o,i);

if (mode)
strcat(o,"0000");

for(j=0;j<strlen(i);j++)
if(*(o+j)=='1')
for(k=0;k<strlen(g);k++)
{
if(((*(o+j+k)=='0')&&(g[k]=='0'))||((*(o+j+k)=='1')&&(g[k]=='1')))
*(o+j+k)='0';
else
*(o+j+k)='1';
}
for(j=0;j<strlen(o);j++)
if(o[j]=='1')
return(1);
return(0);
}

void main()
{
char i[100]={{'\0'}}, o[100]={{'\0'}}, r[100]={{'\0'}};
clrscr();
printf("Enter the Binary data frame:");
scanf("%s", i);
xor2_div(i,o,1);
printf("\n CRC-CCITT Code is:%s %s", i,o+strlen(i));
printf("\n Enter the received code:");
scanf("%s",r);

if(!xor2_div(r,o,0))
printf("\n Received Code is Error Free");
else
VII Semester Dept. of TCE, Dr. AIT Page 16
CCN Laboratory (TEL76) 2018-19

printf("\n /Erroneous");
getch();
}

Output:

For example:

1. Enter the Binary data frame: 100011001

CRC-CCITT Code is: 100011001 0010

Enter the Received Code: 1000110010010

Received Code is Error Free.

2. Enter the Binary data frame: 10101010

CRC-CCITT Code is: 10101010 1001

Enter the Received Code: 101000101001

Erroneous.

Note: Check the output for 2 different sets of input.

VII Semester Dept. of TCE, Dr. AIT Page 17


CCN Laboratory (TEL76) 2018-19

6. DIJKSTRA'S ALGORITHM TO FIND THE SHORTEST


ROUTING PATH
Theory:

In order to transfer packets from a source host to the destination host, the network
layer must determine the path or route that the packets are to follow. This is the job of the
network layer routing protocol. At the heart of any routing protocol is the routing algorithm
that determines the path for a packet from source router to destination router. Given a set of
routers, with links connecting the routers, a routing algorithm finds a good path from source
router to destination router, according to some cost criterion. These can be,

1. Hop count: The cost is inversely proportional to the link capacity. One assigns higher
costs to lower capacity links. The objective is to send a packet through a path with the
highest capacity. If each link has equal capacity, then the shortest path is the path
with the minimum number of hops.
2. Transmission speed: The speed at which the various links operate is an important part
of a route‟s efficiency. Faster links obviously take precedence over slow ones.
3. Congestion: Network congestion caused by the current traffic pattern is considered
when evaluating a route and links that are overly congested are bypassed.
4. Route cost: The route cost is a metric assigned by the network administrator used to
rate the relative usability of various routes . The cost can refer to the literal financial
expense incurred by the link or any other pertinent factor.
5. Packet delay: The cost is proportional to an average packet delay which includes
queuing delay in the switch buffer ad propagation delay in the link. The shortest
path represents the fastest path to reach the destination.

Dijkstra's method of computing the shortest path or shortest path routing is a static
routing algorithm. It involves building a graph of the subnet, with each node of the graph
representing a router and each arc representing a communication line or a link. To find a
route between a pair of routers, the algorithm just finds the shortest path between them on the
graph.
In Dijkstra's algorithm the metric used for calculation is distance. Each node is
labeled with its distance from the source node along with the previous node in the path.
Initially no paths are known so all nodes are labeled with infinity. As the algorithm proceeds
and paths are found, the labels may change, reflecting better paths. A label may either be
tentative or permanent. Initially all nodes are tentative and once it is discovered that the
shortest possible path to a node is got it is made permanent and never changed after that.

VII Semester Dept. of TCE, Dr. AIT Page 18


CCN Laboratory (TEL76) 2018-19

Algorithm:

 Input graph data.


 Make all nodes TENTATIVE.
 Input source and destination.
 Make source, working node.
 Make the working node PERMANENT.
 Check all tentative nodes, which are connected to working node. Update weight if
required.
 Find TENTATIVE node with smallest weight, Make this the working node.
 If working node is destination, go to step 8 else go to 5.
 Trace back from destination to source.

Program:

#include<stdio.h>
#include<conio.h>
# define infinty 999
void dijkstra (int cost[20][20],int n,int source,int distance[20])
{
int visited[20],min,u,i,j;
for(i=1;i<=n;i++)
{
distance[i]=cost[source][i];
visited[i]=0;
}
visited[source] =1;
for(i=1;i<=n;i++)
{
min=infinty;
for(j=1;j<=n;j++)
if (visited[j]==0 && distance[j]<min)
{
min =distance[j];
u=j;
}
visited[u]=1;
for(j=1;j<=n;j++)
if(visited[j]==0 &&(distance[u]+cost[u][j]<distance[j]))
distance[j]=distance[u]+cost[u][j];
VII Semester Dept. of TCE, Dr. AIT Page 19
CCN Laboratory (TEL76) 2018-19

}
}
void main()
{
int cost[20][20],distance[20],i,j,src,dst,sum;
unsigned int n;
clrscr();
printf("\n Enter the number of nodes:\t");
scanf("%d",&n);
printf("\n enter the cost matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if (cost[i][i]!=0)
{
printf("\n invalid entry");
getch();
exit(0);
}
}
printf("\n enter the src node:\t");
scanf("%d",&src);
printf("\n enter the dst node\t");
scanf("%d%",&dst);
dijkstra(cost,n,src,distance);
printf("\n\n shortest dist from %d to %d is %d",src,dst,distance[dst]);
getch();
}

VII Semester Dept. of TCE, Dr. AIT Page 20


CCN Laboratory (TEL76) 2018-19

Sample run:

Example: Find the shortest path from 1 to 4 for the arrangement shown below:

2 1
1 2 5

1 3 4 1

3 4
3
Output:
For example:

***Implementation of DIJKSTRA‟S Algorithm***


Enter the number of Nodes: 5
[Enter the Cost Matrix i,e., Type only the numbers within bracket in matrix form]

1 2 3 4 5

1 0 2 1 999 999
2 2 0 3 4 1
3 1 3 0 3 999
4 999 4 3 0 1
5 999 1 999 1 0

Enter the source Node : 1


Enter the Destination Node : 4

The Shortest Path from 1 to 4 is 4


** Check for any source and any destination

Note: Check the output for 2 different sets of input.

VII Semester Dept. of TCE, Dr. AIT Page 21


CCN Laboratory (TEL76) 2018-19

7. TO FIND MINIMUM SPANNING TREE OF A SUBSET


Theory:

Given a connected, undirected graph, a spanning tree of that graph is a subgraph


which is a tree and connects all the vertices together. A single graph can have many different
spanning trees. For instance, a complete graph of 4 nodes can have 16 spanning trees. We
can also assign a weight to each edge, which is a number representing how unfavorable it is,
and use this to assign a weight to a spanning tree by computing the sum of the weights of the
edges in that spanning tree. A minimum spanning tree (MST) or minimum weight
spanning tree is then a spanning tree with weight less than or equal to the weight of every
other spanning tree.
There may be several minimum spanning trees of the same weight having a minimum
number of edges in particular, if all the edge weights of a given graph are the same, then
every spanning tree of that graph is minimum. If there are n vertices in the graph, then each
tree has n-1 edges.
A spanning tree includes all the routers but contains no loops. If each router knows
which of its lines belong to the spanning tree, it can copy an incoming broadcast packet onto
all the spanning tree lines except the one it arrived on. This method makes excellent use of
bandwidth, generating the absolute minimum number of packets necessary to do the job. The
only problem is that each router must have knowledge of some spanning tree for it to be
applicable. Sometimes this information is available (e.g. with link state routing) but
sometimes it is not (e.g. with distance vector routing).
There are two algorithms commonly used, Prim's algorithm and Kruskal's algorithm.

Prim’s Algorithm:

 Input number of nodes and edges.


 Input the edge weights.
 Initially a node is chosen and designated as root.
 The nodes of the graph are appended to the tree one at a time until all the nodes are
included in the tree.
 The node added to the tree at each point is that node adjacent to a node of a tree by
an arc of minimum weight.
 Repeat till edges are marked making sure that there are no closed loops.
 Display the selected edges and the overall length of spanning tree.

VII Semester Dept. of TCE, Dr. AIT Page 22


CCN Laboratory (TEL76) 2018-19

Program:

#include<stdio.h>
#include<conio.h>
#define infinity 999
int prime(int cost[10][10],int source,int n)
{
int i,j,sum=0,visited[10],cmp[10],vertex[10];
int min,u,v;
for(i=1;i<=n;i++)
{
vertex[i]=source;
visited[i]=0;
cmp[i]=cost[source][i];
}
visited[source]=1;
for(i=1;i<=n-1;i++)
{
min=Infinity;
for(j=1;j<=n;j++)
if(!visited[j] && cmp[j]<min)
{
min=cmp[j];
u=j;
}
visited[u]=1;
sum=sum+cmp[u];
printf("\n %d-> %d sum=%d",vertex[u],u,cmp[u]);
for(v=1;v<=n;v++)
if(!visited[v] && cost[u][v] < cmp[v])
{
cmp[v]=cost[u][v];
vertex[v]=u;
}
}
return sum;
}
void main()
{
int a[10][10],n,i,j,m,source;

VII Semester Dept. of TCE, Dr. AIT Page 23


CCN Laboratory (TEL76) 2018-19

clrscr();
printf("\n Enter the number of vertices");
scanf("%d",&n);
printf("\n Enter the cost matrix:0 self loop& 999 no edge\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(a[i][j]!=a[j][i]||(a[i][i]!=0))
{
printf("\n Invalid entry \n cost matrix should be symmetrical &
the diagonal elements are zero");
getch();
exit(0);
}
printf("\n Entert the source:");
scanf("%d",&source);
m=prime(a,source,n);
printf("\n\n total cost=%d",m);
getch();
}

Sample Run:

2 1
2 5
1
1 3 4 1

3 4
3

Enter the number of Nodes(Vertices): 5


[Enter the Cost Matrix i,e., Type only the numbers within bracket in matrix form]

VII Semester Dept. of TCE, Dr. AIT Page 24


CCN Laboratory (TEL76) 2018-19

1 2 3 4 5
1 0 2 1 999 999
2 2 0 3 4 1
3 1 3 0 3 999
4 999 4 3 0 1
5 999 1 999 1 0

(** If the diagonal elements are not zero, it is Invalid entry.)

Enter the source node(Root node) : 1

1 3 Sum =1
1 2 Sum =2
2 5 Sum =1
5 4 Sum =1

Total Cost = 5

Note: Check the output for 2 different sets of input.

VII Semester Dept. of TCE, Dr. AIT Page 25


CCN Laboratory (TEL76) 2018-19

8. CONGESTION CONTROL USING LEAKY BUCKET


ALGORITHM.
Theory:

Policing
1. Network monitors traffic flows continuously to ensure they
meet their traffic contract.
2. The process of monitoring and enforcing the traffic flow is called policing.
3. When a packet violates the contract, network can discard or tag the
packet giving it lower priority
4. If congestion occurs, tagged packets are discarded first
5. Leaky Bucket Algorithm is the most commonly used policing mechanism
(i) Bucket has specified leak rate for average contracted rate
(ii) Bucket has specified depth to accommodate variations in arrival rate
(iii) Arriving packet is conforming if it does not result in overflow
Leaky Bucket algorithm can be used to police arrival rate of a packet stream

Algorithm:
1. The above figure shows the leaky bucket algorithm that can be used to police the
traffic flow.
2. At the arrival of the first packet, the content of the bucket is set to zero and the last
conforming time (LCT) is set to the arrival time of the first packet.
3. The depth of the bucket is L+I, where l depends on the traffic burstiness.
4. At the arrival of the kth packet, the auxiliary variable X‟ records the difference

VII Semester Dept. of TCE, Dr. AIT Page 26


CCN Laboratory (TEL76) 2018-19

between the bucket content at the arrival of the last conforming packet and the
inter-arrival time between the last conforming packet and the kth packet.
5. If the auxiliary variable is greater than L, the packet is considered as
nonconforming, otherwise the packet is conforming. The bucket content and the
arrival time of the packet are then updated.

Leaky Bucket Example:

The operation of the leaky bucket algorithm is illustrated in the below figure.
1. Here the value I is four packet times, and the value of L is 6 packet times.
2. The arrival of the first packet increases the bucket content by four (packet
times).
3. At the second arrival the content has decreased to three, but four more are
added to the bucket resulting in total of seven.
4. The fifth packet is declared as nonconforming since it would increase the
content to 11, which would exceed L+I (10).
5. Packets 7, 8, 9 and 10 arrive back to back after the bucket becomes empty.
Packets 7, 8 and 9 are conforming, and the last one is nonconforming.
6. Non-conforming packets not allowed into bucket & hence not included in
calculations.

Program:
//leacky bucket program

#include<stdio.h>
#define bucketsize 1000
#define n 5 // user defined function to output the contents of bucket at a constant rate
void bucketoutput(int *bucket, int op)
{

VII Semester Dept. of TCE, Dr. AIT Page 27


CCN Laboratory (TEL76) 2018-19

if(*bucket > 0 && *bucket > op) // if the no. of bytes in the bucket >output rate
{

*bucket= *bucket-op; //no. of bytes in bucket – output rate


printf("\n%d-outputed remaining is %d",op,*bucket);
}

else if(*bucket > 0) // if the bucket is not empty


{

printf("\n remaining data output = %d",*bucket);


*bucket=0;
}
}
int main()
{
int op,newpack,oldpack=0,wt,i,j,bucket=0; // op – ouput rate, wt- waiting time, bucket- no.of
bytes in the bucket at any point of time
printf("enter output rate"); // input the output rate scanf("%d",&op);
for(i=1;i<=n;i++)
{

newpack=rand()%500; //new packet with random size is generated printf("\n\n new packet
size = %d",newpack); newpack=oldpack+newpack;
wt=rand()%5; // random waiting time is generated if(newpack<bucketsize)
bucket=newpack;

else
{
printf("\n%d = the newpacket and old pack is greater than bucketsize reject",newpack);
bucket=oldpack;
}
printf("\nthe data in bucket = %d",bucket);
printf("\n the next packet will arrive after = %d sec",wt);
// calling output rate function with wait time for(j=0;j<wt;j++)
{

bucketoutput(&bucket,op); sleep(1);
}

oldpack=bucket;
VII Semester Dept. of TCE, Dr. AIT Page 28
CCN Laboratory (TEL76) 2018-19

while(bucket>0)
bucketoutput(&bucket,op); return 0;
}

Output
enter output rate 500
new packet size = 383
the data in bucket = 383
the next packet will arrive after = 1 sec
remaining data output = 383
new packet size = 277
the data in bucket = 277
the next packet will arrive after = 0
sec new packet size = 293
the data in bucket = 570
the next packet will arrive after = 0 sec
new packet size = 386
the data in bucket = 956
the next packet will arrive after = 2 sec
500-outputed remaining is 456
remaining data output = 456
new packet size = 149
the data in bucket = 149
the next packet will arrive after = 1 sec
remaining data output = 149

VII Semester Dept. of TCE, Dr. AIT Page 29


CCN Laboratory (TEL76) 2018-19

Part B:
CCN Experiments using Hardware

VII Semester Dept. of TCE, Dr. AIT Page 30


CCN Laboratory (TEL76) 2018-19

CCN EXPERIMENTS USING HARDWARE

INTRODUCTION: The series DCT-03, DATA COMMUNICATION TRAINER is a


modular system for the development of exercises and theoretical-experimental courses to
understand the basic concept and working of modes and protocols in serial and parallel
communications.

The unit consists of a set of modules, each including one or more functional blocks
typical of communication systems.
The system is highly innovative from a technological as well as an educational point
of view. The modules are used as “Basic Blocks” to build up in a flexible way the different
communication systems, and to examine all the peculiar operating characteristics.

Data communication is a term referred when the sender and receiver are digital
devices, which communicates each other by means of binary information. The objective of
the experiment is to clear the various aspects of the data communications which comprise of

 The information source or sender.


 The medium carrying of information.
 The information receiver.
 The communication protocols , which ensures proper transfer of data.

Procedure for DCT kit

 Connect the RS232 of the kit to the usb port of the PC(com1) on both the sides
 Connect the power supply.
 Load the software and open it (data communication trainer).
 Update the OS.
 Select the com1 in the window.
 Select the bit rate (Should be the same both sides).
 Select flow control as X on/ X off.
 Now it is ready for transmission.
 For echo (To display the type message).
 Go to  Properties Settings  ASCII setup  highlight echo….  Ok
 For transferring
 Go to  Transfer Send file  Browse  Send.
VII Semester Dept. of TCE, Dr. AIT Page 31
CCN Laboratory (TEL76) 2018-19

1. RS 232
INTRODUCTION: RS-232 standard is defined for a point-to-point communication. The data
transfer is defined between DTE ( Data Transfer Equipment ) and the DCE ( Data
communication Equipment ).

DLE- Data Communication Equipment such as modem in analog network or network


terminating unit in digital network. It is regarded as the boundary of a network . Please note
that a minicomputer can be a DLE if it is located at network boundary.

DTE- Data Terminating Equipment such as computer port or computer terminal in a network.

RS-232communication is asynchronous. That is a clock signal is not sent with data.


Each word is synchronized using its start bit, and an interval clock on each side, keeps tabs
on the timing.

The RS-232 line, when idle is in the mark state( logic 1). A transmission starts with a
start bit, which is ( logic 0 ). Then each bit is sent down the line, one at a time. The LSB is
sent first. A stop bit ( logic 1 ) is then appended to the signal to make up the transmission.

EQUIPMENT: DCT-03
9 pin D-connector cable.
Computers-PC-2.
Power Supply.

Procedure:

 Connect power supply with proper polarity to kit DCT-03.


 Refer to the Block diagram to make connections.
 Connect TD1 port to RD2 port.
 Connect TD2 port to RD1 port
 Keep switch setting of S4 as shown.
 Switch on the Power supply.
 Run DCT-software and select serial communication software link on both PC‟s.
 This will provide link on the hyper terminal software.

VII Semester Dept. of TCE, Dr. AIT Page 32


CCN Laboratory (TEL76) 2018-19

2. MODEM COMMUNICATION

INTRODUCTION: FSK modem


The name modem is a contraction of the term modulator and demodulator. When
used in the transmitting mode, the modem accepts digital data and converts it to a analog
signals for use in modulating a carrier signal. At the receiver end of the of the systems, the
carrier is demodulated to recover the data.

FSK modulation:
In this form of modulation, the sine wave carrier assumes two values of frequency,
determined by the binary data signal. Logic high or mark and logic low or space are assigned
two different carrier frequencies. The carrier frequency shifts between two frequencies
corresponding to the mark or space level at the input level of the modulator.

FSK demodulation:
The most common circuit for demodulating FSK signals is the PLL. The FSK signal
at the PLL input has two values of frequency shifts, thus providing a two-level signal
corresponding to the original binary data stream. The PLL demodulator is followed by a low
pass filter, which removes the residual carrier components, and by a pulse forming circuit
which restores the correct shape of the data signal.

Procedure:
 Connect power supply with proper polarity to kit DCT-03.
 Refer to the Block diagram to make connections
 Connect TD1 port to input port of FSK modulator and out port of FSK demodulator
to RD2.
 Connect telephone link between RJ11 as provided.
 Connect RD1 to TD2 port.
 Keep switch setting of S4 as shown.
 Switch on power supply run a file transfer protocol using hyperlink on both the PC‟s.

VII Semester Dept. of TCE, Dr. AIT Page 33


CCN Laboratory (TEL76) 2018-19

3. FIBER OPTIC COMMUNICATION


INTODUCTION: Fiber optics link can be used for transmission of digital as well as analog
signals. Basically a fiber optic link contains three main elements, a transmitter, an optical
fiber and a receiver. The transmitter module takes the input signal in electrical form and then
transforms it into optical ( light ) energy containing the same information. The optical fiber is
the medium which carries this energy to the receiver. At the receiver, light is converted back
into electrical form with the same pattern as originally fed to the transmitter.

Transmitter:
Fiber optic transmitter are typically composed of a buffer, driver and optical source.
The buffer provides both an electrical connection and isolation between the transmitter and
the electrical system supplying the data. The driver electronics provides electrical power to
the optical source in the fashion that duplicates the pattern of data being fed to the
transmitter.

Receiver:
The function of the receiver is to convert the optical energy into electrical form which
is then conditioned to reproduce the transmitted electrical signal in its original form.

Procedure:
 Connect power supply with proper polarity to kit DCT-03.
 Connect TD1 port to TX port of FO-transmission and TTL port of FO Rx to RD2.
 Connect RD1 port to TD2 port.
 Keep the switch setting S4 as shown in block diagram.
 Switch on power supply run a file transfer protocol using hyperlink on both the PC‟s.

******************

VII Semester Dept. of TCE, Dr. AIT Page 34

You might also like