CCN Lab Manual Vtu
CCN Lab Manual Vtu
Prepared By
Mr.NARAYANA SWAMY.R
Assoc.Prof, Dept of TCE
TJIT, BANGALORE
CONTENTS
I. CCN Programming Experiments in C/C++ (3 lab sessions of 3 hrs each)
1. Simulate bit/character stuffing & de-stuffing using HDLC
2. Simulate the shortest path algorithm
3. Encryption and decryption of a given message
4. Find minimum spanning tree of a subnet
5. Compute polynomial code checksum for CRC-CCITT
II. CCN Experiments using Hardware (1 lab session of 3 Hrs each)
1. Asynchronous and Synchronous Communication using RS 232/ Optical
Fiber/ Twisted pair / RJ45
2. Using fork function creates TWO processes and communicates between them.
3. Communicate between TWO PCs, using simple socket function.
III. Demonstrate the operations of rlogin and telnet
IV. Demonstrate the operations of ftp, mailbox.
Character Count
This method uses a field in the header to specify the number of characters in the frame. When the data link
layer at the destination sees the character count, it knows how many characters follow, and hence where the
end of the frame is. The disadvantage is that if the count is garbled by a transmission error, the destination
will lose synchronization and will be unable to locate the start of the next frame. So, this method is rarely
used.
2.
Character stuffing
In the second method, each frame starts with the ASCII character sequence DLE STX and ends with the
sequence DLE ETX.(where DLE is Data Link Escape, STX is Start of TeXt and ETX is End of TeXt.) This
method overcomes the drawbacks of the character count method. If the destination ever loses
synchronization, it only has to look for DLE STX and DLE ETX characters. If however, binary data is
being transmitted then there exists a possibility of the characters DLE STX and DLE ETX occurring in the
data. Since this can interfere with the framing, a technique called character stuffing is used.
Solution: Use characterstuffing; within the frame, replace every occurrence of DLE with the two-character
sequence DLE DLE. The receiver reverses the processes, replacing every occurrence of DLE DLE with a
single DLEbefore this data is given to the network layer.
Example: If the frame contained ``A B DLE D E DLE'', the characters transmitted over the channel would
be ``DLE STX A B DLE DLE D E DLE DLE DLE ETX''.
Disadvantage: character is the smallest unit that can be operated on; not all architectures are byte oriented
and this is a major hurdle in transmitting arbitrary sized characters.
3.
Bit stuffing
The third method allows data frames to contain an arbitrary number of bits and allows character codes
with an arbitrary number of bits per character. Bit stuffing is used for various purposes, such as
a.
For bringing bit streams that do not necessarily have the same or rationally related bit rates
up to a common rate, or to fill buffers or frames. The location of the stuffing bits is
communicated to the receiving end of the data link, where these extra bits are removed to return
the bit streams to their original bit rates or form. Bit stuffing may be used to synchronize several
channels before multiplexing or to rate-match two single channels to each other.
Applications include Plesiochronous Digital Hierarchy and Synchronous Digital Hierarchy.
b. Another use of bit stuffing is for run length limited coding: to limit the number of consecutive
bits of the same value in the data to be transmitted. A bit of the opposite value is inserted after the
maximum allowed number of consecutive bits. Since this is a general rule the receiver doesn't need
extra information about the location of the stuffing bits in order to do the destuffing. This is done
to create additional signal transitions to ensure reliable transmission or to escape special reserved
code words such as frame sync sequences when the data happens to contain them.
Applications include Controller Area Network, HDLC, and Universal Serial Bus.
Bit stuffing does not ensure that the payload is intact (i.e. not corrupted by transmission errors); it is
merely a way of attempting to ensure that the transmission starts and ends at the correct places. Error
detection and correction techniques are used to check the frame for corruption after its delivery and, if
necessary, the frame will be resent.
Zero-bit insertion
Zero-bit insertion is a particular type of bit stuffing (in the latter sense) used in some data transmission
protocols. It was popularized by IBM's SDLC (later renamed HDLC), to ensure that the Frame Sync
Sequence (FSS) never appears in a data frame. An FSS is the method of frame synchronization used by
HDLC to indicate the beginning and/or end of a frame. The name relates to the insertion of only 0 bits.
No 1 bits are inserted to limit sequences of 0 bits.
The bit sequence "01111110" containing six adjacent 1 bits is commonly used as a "Flag byte" or FSS.
To ensure that this pattern never appears in normal data, a 0 bit is stuffed after every five 1 bits in the
data. This typically adds 1 stuffed bit to every 32 random payload bits, on average. Note that this
stuffed bit is added even if the following data bit is 0, which could not be mistaken for a sync sequence,
so that the receiver can unambiguously distinguish stuffed bits from normal bits. When the receiver
sees five consecutive 1s in the incoming data stream, followed by a zero bit, it automatically destuffs
the 0 bit. The boundary between two frames can be determined by locating the flag pattern.
4.
//**************************************
// Name: bit stuffing
// Description: performs bit stuffing/destuffing on an input data stream
//**************************************
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
int main()
{
char
char
char
char
char
*p,*q;
temp;
in[MAXSIZE];
stuff[MAXSIZE];
destuff[MAXSIZE];
int count=0;
printf("enter the input character string (0s & 1s only):\n");
scanf("%s",in);
p=in;
q=stuff;
while(*p!='\0')
{
if(*p=='0')
{
*q=*p;
q++;
p++;
}
else
{
while(*p=='1' && count!=5)
{
count++;
*q=*p;
q++;
p++;
}
if(count==5)
{
*q='0';
q++;
}
count=0;
}
*q='\0';
printf("\nthe stuffed character string is");
printf("\n%s",stuff);
p=stuff;
q=destuff;
while(*p!='\0')
{
if(*p=='0')
{
*q=*p;
q++;
p++;
}
else
{
while(*p=='1' && count!=5)
{
count++;
*q=*p;
q++;
p++;
}
if(count==5)
{
p++;
}
count=0;
}
}
*q='\0';
printf("\nthe destuffed character string is");
printf("\n%s\n",destuff);
return 0;
}
//**************************************
// Name: character stuffing
// Description:to perform character stuffing on input data stream(networking // link layer activity)
//**************************************
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
int main()
{
char in[MAXSIZE],stuff[MAXSIZE],destuff[MAXSIZE];
char *p,*q;
int i;
printf("enter the string to be character stuffed\n");
scanf("%s",in);
p=in;
q=stuff;
while(*p!='\0')
{
if(*p=='d')
{
*q=*p;
p++;
q++;
if(*p=='l')
{
*q=*p;
p++;
q++;
if(*p=='e')
{
*q=*p;
p++;
q++;
*(q++)='d';
*(q++)='l';
*(q++)='e';
}
}
}
else
{
*q=*p;
q++;
p++;
}
}
*q='\0';
printf("\nthe stuffed string is");
printf("\n%s",stuff);
p=stuff;
q=destuff;
while(*p!='\0')
{
if(*p=='d')
{
*q=*p;
p++;
q++;
if(*p=='l')
{
*q=*p;
p++;
q++;
if(*p=='e')
{
*q=*p;
p++;
q++;
for(i=1;i<=3;i++)
p++;
}
}
else
{
*q=*p;
q++;
p++;
}
}
*q='\0';
printf("\nthe destuffed string is");
printf("\n%s\n",destuff);
return 0;
}
//**************************************
// Name: Encryption and Decryption of string
//**************************************
#include <stdio.h>
#include <string.h>
void encrypt(char str[],int key)
{
unsigned int i;
for(i=0;i<strlen(str);++i)
{
str[i] = str[i] - key;
}
}
void decrypt(char str[],int key)
{
unsigned int i;
for(i=0;i<strlen(str);++i)
{
}
int main()
{
char str[] = "smurffANDkdo";
printf("Passwrod
= %s\n",str);
encrypt(str,0xFACA);
printf("Encrypted value = %s\n",str);
decrypt(str,0xFACA);
printf("Decrypted value = %s\n",str);
return 0;
}
BellmanFord algorithm solves the single-source problem if edge weights may be negative.
A* search algorithm solves for single pair shortest path using heuristics to try to speed up the search.
Johnson's algorithm solves all pairs shortest paths, and may be faster than FloydWarshall on sparse
graphs.
, and an undirected
and
)
The single-source shortest path problem, in which we have to find shortest paths from a source
vertex v to all other vertices in the graph.
The single-destination shortest path problem, in which we have to find shortest paths from all
vertices in the directed graph to a single destination vertex v. This can be reduced to the single-source
shortest path problem by reversing the arcs in the directed graph.
The all-pairs shortest path problem, in which we have to find shortest paths between every pair of
vertices v, v' in the graph.
These generalizations have significantly more efficient algorithms than the simplistic approach of running a
single-pair shortest path algorithm on all relevant pairs of vertices.
Dijkstra's Algorithm
Dijkstra's algorithm is a greedy algorithm that solves the shortest path problem
for a directed graph G.Dijkstra's algorithm solves the single-source shortest-path
problem when all edges have non-negative weights.
Dijkstra's Algorithm
DIJKSTRA(G, w, s)
1 INITIALIZE-SINGLE-SOURCE(G, s)
2 S
3 Q V[G]
4while Q
5
do u EXTRACT-MIN(Q)
6
S S {u}
7
for each vertex v Adj[u]
8
doRELAX(u, v, w)
Example
Distance
10
Distance From
Step2
Now consider vertex C
No. of Nodes
Distance
11
Distance From
Step3
Now consider vertex E
No. of Nodes
Distance
11
Distance From
Step4
Now consider vertex B
No. of Nodes
Distance
Distance From
Step5
Now consider vertex D
No. of Nodes
Distance
11
Distance From
10
11
14
A
C
//**************************************
16
#include<stdio.h>
#include<conio.h>
void main()
{
int path[5][5],i,j,min,a[5][5],p,st=1,ed=5,stp,edp,t[5],index;
clrscr();
printf("enter the cost matrix\n");
for(i=1;i<=5;i++)
for(j=1;j<=5;j++)
scanf("%d",&a[i][j]);
printf("enter number of paths\n");
scanf("%d",&p);
printf("enter possible paths\n");
for(i=1;i<=p;i++)
for(j=1;j<=5;j++)
scanf("%d",&path[i][j]);
for(i=1;i<=p;i++)
{
t[i]=0;
stp=st;
for(j=1;j<=5;j++)
{
edp=path[i][j+1];
t[i]=t[i]+a[stp][edp];
if(edp==ed)
break;
else
stp=edp;
}
min=t[st];
index=st;
for(i=1;i<=p;i++)
{
if(min>t[i])
{
min=t[i];
index=i;
}
}
printf("minimum cost %d",min);
printf("\n minimum cost path ");
for(i=1;i<=5;i++)
{
printf("--> %d",path[index][i]);
if(path[index][i]==ed)
break;
}
}
getch();
/*OUTPUT:
enter the cost matrix :
0 1 4 2 0
0 0 0 2 3
0 0 0 3 0
0 0 0 0 5
0 0 0 0 0
enter number of paths : 4
enter possible paths :
1 2 4 5 0
1 2 5 0 0
1 4 5 0 0
1 3 4 5 0
minimum cost : 4
minimum cost path :
1>2>5*/
Prim's Algorithm
Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for a connected weighted undirected
graph. It finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the
edges in the tree is minimized.
This algorithm is directly based on the MST (minimum spanning tree) property.
Example
Prim's Algorithm
MST-PRIM(G, w, r)
1.
for each u V [G]
2.
do key[u]
3.
[u] NIL
4.
key[r] 0
5.
Q V [G]
6.
while Q
7.
do u EXTRACT-MIN(Q)
8.
for each v Adj[u]
9.
do if v Q and w(u, v) < key[v]
10.
then [v] u
11.
key[v] w(u, v)
Example
0
0
1
3
0
2
1
0
3
6
0
Step2
No. of Nodes
Distance
Distance From
0
0
1
3
0
2
0
3
5
2
4
6
2
5
4
2
Step3
No. of Nodes
Distance
Distance From
0
0
1
0
2
0
3
5
2
4
3
1
5
4
2
Step4
No. of Nodes
Distance
Distance From
0
0
1
0
0
0
1
0
2
0
3
5
2
4
0
5
4
2
Step5
No. of Nodes
Distance
Distance From
2
0
3
3
2
4
0
5
0
2
for(i=0;i<keylen-1;i++)
printf("%c",rem[i]);
printf("\nFinal data is: ");
for(i=0;i<msglen;i++)
printf("%c",input[i]);
for(i=0;i<keylen-1;i++)
printf("%c",rem[i]);
getch();
}
Output: