CN Lab Manual CS
CN Lab Manual CS
SNO PROGRAM
1 Week 1-: Implement the data link layer framing methods such as character, character-
stuffing and bit stuffing.
2 WWeek 2 -: Write a program to compute CRC code for the polynomials CRC-12, CRC-16
and CRC CCIP
3 Week 3 - : Develop a simple data link layer that performs the flow control using the
sliding window protocol, and loss recovery using the Go-Back-N mechanism.
4 Week 4 - : Implement Dijsktra’s algorithm to compute the shortest path
through a network
5 Week 5- : Take an example subnet of hosts and obtain a broadcast tree for the subnet.
6 WWeek 6- : Implement distance vector routing algorithm for obtaining routing tables at
each node.
7 WWeek 7- : Implement data encryption and data decryption.
8 WWeek 8- : Write a program for congestion control using Leaky bucket algorithm.
SOURCE CODE:
// BIT Stuffing program
#include<stdio.h>
#include<string.h>
void main()
{
int a[20],b[30],i,j,k,count,n;
printf("Enter frame length:");
scanf("%d",&n);
printf("Enter input frame (0's & 1's only):");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
i=0; count=1; j=0;
while(i<n)
{
if(a[i]==1)
{
b[j]=a[i];
for(k=i+1;a[k]==1 && k<n && count<5;k++)
{
j+
+;
b[j]=a[k];
count++;
if(count==5)
{
j+
+;
b[j]=0;
}
i=k;
}
}
else
{
b[j]=a[i];
} i+
+;
j++;
}
printf("After stuffing the frame is:");
for(i=0;i<j;i++)
printf("%d",b[i]);
}
OUTPUT:
Enter frame length:5
Enter input frame (0's & 1's only):
1
1
1
1
1
After stuffing the frame is:111110
------------------
(program exited with code: 6)
Press return to continue
VIVA QUESTIONS:
1. What is bit stuffing?
2. What is the use of bit stuffing?
3. with bit stuffing the boundary b/w 2 frames can be unambiguously
recognized by ------------------------
4. -------------------- is analogous to character stuffing
5. Each frame begins and ends with a special bit pattern 01111110 called ---
------
6. The senders data link layer encounters ----------------no of 1’s consecutively
EXPERIMENT NO: 1. (b)
OUTPUT:
enter string
MLRITM
enter position
2
enter the character
frame after stuffing:
dlestxMdldleLRITMdleetx
------------------
(program exited with code: 0)
Press return to continue
VIVA QUESTIONS:
1. What is character stuffing?
2. What is the use of character stuffing?
3. is analogous to bit stuffing.
4. are the delimiters for character stuffing
5. Expand DLE STX
6. Expand DLE ETX
EXPERIMENT NO: 2.
SOURCE CODE:
//PROGRAM FOR CYCLIC REDUNDENCY CHECK
#include<stdio.h>
int gen[4],genl,frl,rem[4];
void main()
{
int i,j,fr[8],dupfr[11],recfr[11],tlen,flag;
frl=8; genl=4;
printf("enter frame:");
for(i=0;i<frl;i++)
{
scanf("%d",&fr[i]);
dupfr[i]=fr[i];
}
printf("enter generator:");
for(i=0;i<genl;i++)
scanf("%d",&gen[i]);
tlen=frl+genl-1;
for(i=frl;i<tlen;i++)
{
dupfr[i]=0;
}
remainder(dupfr);
for(i=0;i<frl;i++)
{
recfr[i]=fr[i];
}
for(i=frl,j=1;j<genl;i++,j++)
{
recfr[i]=rem[j];
}
remainder(recfr);
flag=0;
for(i=0;i<4;i++)
{
if(rem[i]!=0)
flag++;
}
if(flag==0)
{
printf("frame received correctly");
}
else
{
printf("the received frame is wrong");
}
}
remainder(int fr[])
{
int k,k1,i,j;
for(k=0;k<frl;k++)
{
if(fr[k]==1)
{
k1=k; for(i=0,j=k;i<genl;i+
+,j++)
{
rem[i]=fr[j]^gen[i];
}
for(i=0;i<genl;i++)
{
fr[k1]=rem[i];
k1++;
}
}
}
}
OUTPUT:
enter frame:
1
1
0
1
0
1
1
0
enter generator:
1
0
1
1
Frame received correctly.
VIVA QUESTIONS:
1. What is CRC?
2. What is the use of CRC?
3. Name the CRC standards
4. Define checksum?
5. Define generator polynomial?
6. Polynomial arithmetic is done by
XPERIMENT NO: 3
NAME OF THE EXPERIMENT: flow control using the sliding window protocol
AIM:Develop a simple data link layer that performs the flow control using the sliding window protocol, and
loss recovery using the Go-Back-N mechanism.
#include<stdio.h>
int main()
{
int w,i,f,frames[50];
printf("Enter window size: ");
scanf("%d",&w);
printf("\nEnter number of frames to transmit: ");
scanf("%d",&f);
printf("\nEnter %d frames: ",f);
for(i=1;i<=f;i++)
scanf("%d",&frames[i]);
printf("\nWith sliding window protocol the frames will be sent in the following manner (assuming no
corruption of frames)\n\n");
printf("After sending %d frames at each stage sender waits for acknowledgement sent by the receiver\n\
n",w);
for(i=1;i<=f;i++)
{
if(i%w==0)
{
printf("%d\n",frames[i]);
printf("Acknowledgement of above frames sent is received by sender\n\n");
}
else
printf("%d ",frames[i]);
}
if(f%w!=0)
printf("\nAcknowledgement of above frames sent is received by sender\n");
return 0;
}
Output:
With sliding window protocol the frames will be sent in the following manner (assuming no corruption of frames)
After sending 3 frames at each stage sender waits for acknowledgement sent by the receiver
12 5 89
Acknowledgement of above frames sent is received by sender
46
Acknowledgement of above frames sent is received by sender
VIVA QUESTIONS:
ALGORITHM/FLOWCHART:
Begin
Step1: Declare array path [5] [5], min, a [5][5], index, t[5];
Step2: Declare and initialize st=1,ed=5
Step 3: Declare variables i, j, stp, p, edp
Step 4: print “enter the cost “
Step 5: i=1
Step 6: Repeat step (7 to 11) until (i<=5)
Step 7: j=1
Step 8: repeat step (9 to 10) until (j<=5)
Step 9: Read a[i] [j]
Step 10: increment j
Step 11: increment i
Step 12: print “Enter the path”
Step 13: read p
Step 14: print “Enter possible paths”
Step 15: i=1
Step 16: repeat step(17 to 21) until (i<=p)
Step 17: j=1
Step 18: repeat step(19 to 20) until (i<=5)
Step 19: read path[i][j]
Step 20: increment j
Step 21: increment i
Step 22: j=1
Step 23: repeat step(24 to 34) until(i<=p)
Step 24: t[i]=0
Step 25: stp=st
Step 26: j=1
Step 27: repeat step(26 to 34) until(j<=5)
Step 28: edp=path[i][j+1]
Step 29: t[i]= [ti]+a[stp][edp]
Step 30: if (edp==ed) then
Step 31: break;
Step 32: else
Step 33: stp=edp
Step 34: end if
Step 35: min=t[st]
Step 36: index=st
Step 37: repeat step( 38 to 41) until (i<=p)
Step 38: min>t[i]
Step 39: min=t[i]
Step 40: index=i
Step 41: end if
Step 42: print” minimum cost” min
Step 43: print” minimum cost pth”
Step 44: repeat step(45 to 48) until (i<=5)
Step 45: print path[index][i]
Step 46: if(path[idex][i]==ed) then
Step 47: break
Step 48: end if
End
SOURCE CODE:
//*********************************
// .PROGRAM FOR FINDING SHORTEST //PATH FOR A GIVEN GRAPH
//*********************************
#include<stdio.h>
void main()
{
int path[5][5],i,j,min,a[5][5],p,st=1,ed=5,stp,edp,t[5],index;
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 the 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;
}
}
OUTPUT:
enter the cost matrix
12345
12345
12345
12345
12345
enter the paths
2
enter possible paths
12345
12345
minimum cost 14
VIVA QUESTIONS:
EXPERIMENT NO: 5
NAME OF THE EXPERIMENT: Broadcast Tree.
AIM: Implement broadcast tree for a given subnet of hosts
HARDWARE REQUIREMENTS: Intel based Desktop PC:- RAM of 512 MB
SOFTWARE REQUIREMENTS: Turbo C / Borland C.
THEORY:
This technique is widely used because it is simple and easy to understand. The idea of
this algorithm is to build a graph of the subnet with each node of the graph representing a router and
each arc of the graph representing a communication line. To choose a route between a given pair of
routers the algorithm just finds the broadcast between them on the graph.
ALGORITHM/FLOWCHART:
step 1: declare variable as int p,q,u,v,n;
step 2: Initialize min=99,mincost=0;
step 3: declare variable as int t[50][2],i,j;
step 4: declare variable as int parent[50],edge[50][50];
step 5: Begin
step 6: write "Enter the number of nodes"
step 7: read "n"
step 8: Initialize i=0
step 9: repeat step(10-12) until i<n
step10: increment i
step11: write"65+i"
step12: Initialize parent[i]=-1
step13:wite "\n"
step14: Initialize i=0
step15: repeat step(15-21) until i<n
step16: increment i
step17: write"65+i"
step18: Initialize j=0
step19: repeat until j<n
step20: increment j
step21: read edge[i][j]
step22: Initialize i=0
step23: repeat step(23-43) until i<n
step24: increment i
step25: Initialize j=0
step26: repeat until j<n
step27: increment j
step28: if'edge[i]j]!=99
step29: if'min>edge[i][j] repeat step (29-32)
step30: intialize min=edge[i][j]
step31: intialize u=i
step32: intialize v=j
step33: calling function p=find(u);
step34: calling function q=find(v);
step35: if'P!=q repeat steps(35-39)
step36: intialize t[i][0]=u
step37: intialize t[i][1]=v
step38: initialize mincost=mincost+edge[u][v]
step39: call function sunion(p,q)
step40: else repeat steps(40-42)
step41: Intialize t[i][0]=-1;
step42: Intialize t[i][1]=-1;
step43: intialize min=99;
step44; write"Minimum cost is %d\n Minimum spanning tree is",mincost
step45: Initialize i=0
step46: repeat until i<n
step47: increment i
step48: if't[i][0]!=-1 && t[i][1]!=-1'repeat step(48-50)
step49: write "%c %c %d", 65+t[i][0], 65+t[i][1], edge[t[i][0]][t[i][1]]
step50: write"\n"
step51: end
step52: called function sunion(int l,int m) repeat step(51-52)
step53: intialize parent[l]=m
step54: called function find(int l) repeat step(53-56)
step55: if parent([l]>0)
step56: initialize l=parent
step57: return l
SOURCE CODE:
// Write a ‘c’ program for Broadcast tree from subnet of host
#include<stdio.h>
int p,q,u,v,n;
int min=99,mincost=0;
int t[50][2],i,j;
int parent[50],edge[50][50];
main()
{
clrscr();
printf("\n Enter the number of nodes");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("%c\t",65+i);
parent[i]=-1;
}
printf("\n");
for(i=0;i<n;i++)
{
printf("%c",65+i);
for(j=0;j<n;j++)
scanf("%d",&edge[i][j]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(edge[i][j]!=99)
if(min>edge[i][j])
{
min=edge[i][j];
u=i;
v=j;
}
p=find(u);
q=find(v);
if(p!=q)
{ t[i]
[0]=u;
t[i][1]=v;
mincost=mincost+edge[u][v];
sunion(p,q);
}
else
{
t[i][0]=-1;
t[i][1]=-1;
}
min=99;
}
printf("Minimum cost is %d\n Minimum spanning tree is\n" ,mincost);
for(i=0;i<n;i++)
if(t[i][0]!=-1 && t[i][1]!=-1)
{
printf("%c %c %d", 65+t[i][0], 65+t[i][1],
edge[t[i][0]][t[i][1]]);
printf("\n");
}
}
sunion(int l,int m)
{
parent[l]=m;
}
find(int l)
{
if(parent[l]>0)
l=parent[l];
return l;
}
OUTPUT:
Enter the number of nodes3
A B C
A1 2 3 4
B1 2 3 4
C4 5 6 7
Minimum cost is 3
Minimum spanning tree is
CA3
VIVA QUESTIONS:
1. What is spanning tree
2. What is broad cast tree?
3. What are the advantages of broad cast tree?
4. Where we should use the broad cast tree
5. What is flooding?
6. What is the subnet?
EXPERIMENT NO: 6
NAME OF THE EXPERIMENT: Distance Vector routing.
AIM: Obtain Routing table at each node using distance vector routing algorithm for a
given subnet.
SOURCE CODE:
#include<stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
{
int dmat[20][20];
int n,i,j,k,count=0;
printf("\nEnter the number of nodes : ");
scanf("%d",&n);
printf("Enter the cost matrix :\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
scanf("%d",&dmat[i][j]);
dmat[i][i]=0;
rt[i].dist[j]=dmat[i][j];
rt[i].from[j]=j;
}
do
{
count=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0;k<n;k++)
if(rt[i].dist[j]>dmat[i][k]+rt[k].dist[j])
{
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
count++;
}
}while(count!=0);
for(i=0;i<n;i++)
{
printf("\nState value for router %d is \n",i+1);
for(j=0;j<n;j++)
{
printf("\nnode %d via %d Distance%d",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf("\n");
}
OUTPUT:
Enter the number of nodes : 2
Enter the cost matrix :
12
12
State value for router 1 is
node 1 via 1 Distance0
node 2 via 2 Distance2
State value for router 2 is
node 1 via 1 Distance1
node 2 via 2 Distance0
VIVA QUESTIONS:
1. What is routing
2. What is best algorithm among all routing algorithms?
3. What is static routing?
4. Difference between static and dynamic
5. How distance vector routing works
6. What is optimality principle?
EXPERIMENT NO: 7
NAME OF THE EXPERIMENT: data encryption and data decryption.
AIM: Take a 64 bit playing text and encrypt the same using DES algorithm.
HARDWARE REQUIREMENTS: Intel based Desktop PC:- RAM of 512 MB
SOFTWARE REQUIREMENTS: Turbo C / Borland C.
THEORY:
Data encryption standard was widely adopted by the industry in security products. Plain text is
encrypted in blocks of 64 bits yielding 64 bits of cipher text. The algorithm which is parameterized by a
56 bit key has 19 distinct stages. The first stage is a key independent transposition and the last stage is
exactly inverse of the transposition. The remaining stages are functionally identical but are
parameterized by different functions of the key. The algorithm has been designed to allow decryption
to be done with the same key as encryption
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
char pwd[20];
char alpha[26]="abcdefghijklmnopqrstuvwxyz";
int num[20],i,n,key;
//clrscr();
printf("\nEnter the password:");
scanf("%s",&pwd);
n=strlen(pwd);
for(i=0;i<n;i++)
num[i]=toascii(tolower(pwd[i]))-'a';
printf("\nEnter the key:");
scanf("%d",&key);
for(i=0;i<n;i++)
num[i]=(num[i]+key)%26;
for(i=0;i<n;i++)
pwd[i]=alpha[num[i]];
printf("\nThe key is:%d",key);
printf("\nEncrypted text is:%s",pwd);
for(i=0;i<n;i++)
{
num[i]=(num[i]-key)%26;
if(num[i]<0)
num[i]=26+num[i];
pwd[i]=alpha[num[i]];
}
printf("\nDecrypted text is:%s",pwd);
getch();
}
Output:
Enter the password:puji
VIVA QUESTIONS:
1. Expand DES
2. What is cipher text?
3. What is plain text?
4. Define public key?
5. Define encryption?
6. Substitutions are performed by boxes
EXPERIMENT NO: 8
NAME OF THE EXPERIMENT: congestion control using Leaky bucket algorithm
AIM: Implement a program for congestion control using Leaky bucket algorithm.
HARDWARE REQUIREMENTS: Intel based Desktop PC:- RAM of 512 MB
SOFTWARE REQUIREMENTS: Turbo C / Borland C.
Program
#include<stdio.h>
int main(){
int incoming, outgoing, buck_size, n, store = 0;
printf("Enter bucket size, outgoing rate and no of inputs: ");
scanf("%d %d %d", &buck_size, &outgoing, &n);
while (n != 0) {
printf("Enter the incoming packet size : ");
scanf("%d", &incoming);
printf("Incoming packet size %d\n", incoming);
if (incoming <= (buck_size - store)){
store += incoming;
printf("Bucket buffer size %d out of %d\n", store, buck_size);
} else {
printf("Dropped %d no of packets\n", incoming - (buck_size - store));
printf("Bucket buffer size %d out of %d\n", store, buck_size);
store = buck_size;
}
store = store - outgoing;
printf("After outgoing %d packets left out of %d in buffer\n", store, buck_size);
n--;
}
}
Output
Enter bucket size, outgoing rate and no of inputs: 50 40 3
VIVA QUESTIONS:
1. What is Congestion?
2. What is Congestion Control?
3. What are Congestion control algorithms?
4. Differentiate between Leaky bucket and Token bucket?
5. What is the purpose of leaky bucket algorithm?
EXPERIMENT NO: 9
AFTER SHUFFLING:
AFTER SORTING
EXPERIMENT NO: 10
Ed. Note: A “packet” is a single message from any network protocol (i.e., TCP, DNS, etc.)
Ed. Note 2: LAN traffic is in broadcast mode, meaning a single computer with Wireshark can
see traffic between two other computers. If you want to see traffic to an external site, you
need to capture the packets on the local computer.
Wireshark allows you to filter the log either before the capture starts or during analysis, so
you can narrow down and zero into what you are looking for in the network trace. For
example, you can set a filter to see TCP traffic between two IP addresses. You can set it only
to show you the packets sent from one computer. The filters in Wireshark are one of the
primary reasons it became the standard tool for packet analysis.
You can select one or more of the network interfaces using “shift left-
click.” Once you have the network interface selected, you can start the
capture, and there are several ways to do that.
Click the first button on the toolbar, titled “Start Capturing Packets.”
You can select the menu item Capture -> Start.
Or you could use the keystroke Control – E.
During the capture, Wireshark will show you the packets that it captures
in real-time.
Once you have captured all the packets you need, you use the same
buttons or menu options to stop the capture.
EXPERIMENT NO: 11