Cn-Lab-Manual SREYAS
Cn-Lab-Manual SREYAS
SYLLABUS
Course Objectives
Course Outcomes
LIST OF EXPERIMENTS
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. Implement Dijkstra„s algorithm to compute the Shortest path thru a given graph.
6. Obtain Routing table at each node using distance vector routing algorithm for a given subnet.
EXPERIMENT 4 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
EX PER IME N T 7 Obtain Routing table at each node using distance vector routing
algorithm for a given subnet.
AIM: Implement the data link layer framing methods such as and bit stuffing.
THEORY:
The new technique allows data frames to contain an arbitrary number if bits and allows character codes with an
arbitrary no of bits per character. Each frame begins and ends with special bit pattern, 01111110, called a flag byte.
When ever the senders data link layer encounters five consecutive one‟s in the data, it automatically stuffs a 0 bit
into the out going bit stream. This bit stuffing is analogous to character stuffing, in which a DLE is stuffed into the
out going character stream before DLE in the data
ALGORITHM:
Begin
Step 1: Read frame length n
Step 2: Repeat step (3 to 4) until i<n(: Read values in to the input frame (0‟s and 1‟s) i.e.
Step 3: initialize I i=0;
Step 4: read a[i] and increment i
Step 5: Initialize i=0, j=0,count =0
Step 6: repeat step (7 to 22) until i<n
Step 7: If a[i] == 1 then
Step 8: b[j] = a[i]
Step 9: Repeat step (10 to 18) until (a[k] =1 and k<n and count <5)
Step 10: Initialize k=i+1;
Step 11: Increment j and b[j]= a[k];
Step 12: Increment count ;
Step 13: if count =5 then
Step 14: increment j,
Step 15: b[j] =0
Step 16: end if
Step 17: i=k;
Step 18: increment k
Step 19: else
Step 20: b[j] = a[i]
Step 21: end if
Step 22: increment I and j
Step 23: print the frame after bit stuffing
Step 24: repeat step (25 to 26) until i< j
Step 25: print b[i]
Step 26: increment i
End
SOURCE CODE:
#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++)
b[j]=a[i];
} i+
+;
j++;
}
printf("After stuffing the frame is:");
for(i=0;i<j;i++)
printf("%d",b[i]);
}
COMPILE THE PROGRAM:
[m0033@agni cnlabprograms]$ cc exp1a.c
OUTPUT
VIVA QUESTIONS:
AIM: Implement the data link layer framing methods such as character, character stuffing.
THEORY:
The framing method gets around the problem of resynchronization after an error by having each frame start with the
ASCII character sequence DLE STX and the sequence DLE ETX. If the destination ever losses the track of the
frame boundaries all it has to do is look for DLE STX or DLE ETX characters to figure out. The data link layer on
the receiving end removes the DLE before the data are given to the network layer. This technique is called character
stuffing
ALGORITHM:
Begin
Step 1: Initialize I and j as 0
Step 2: Declare n and pos as integer and a[20],b[50],ch as character
Step 3: read the string a
Step 4: find the length of the string n, i.e n-strlen(a)
Step 5: read the position, pos
Step 6: if pos > n then
Step 7: print invalid position and read again the position, pos
Step 8: end if
Step 9: read the character, ch
Step 10: Initialize the array b , b[0…5] as ‟d‟, ‟l‟, ‟e‟, ‟s‟ ,‟t‟ ,‟x‟
respectively
Step 11: j=6;
Step 12: Repeat step[(13to22) until i<n
Step 13: if i==pos-1 then
Step 14: initialize b array,b[j],b[j+1]…b[j+6] as„d‟, „l‟, „e‟ ,‟ch, ‟d‟, „l‟,„e‟
respectively
SOURCE CODE:
//PROGRAM FOR CHARACTER STUFFING
#include<stdio.h>
#include<string.h>
#include<process.h>
void main()
{
int i=0,j=0,n,pos;
char a[20],b[50],ch;
printf("enter string\n");
scanf("%s",&a);
n=strlen(a);
printf("enter position\n");
scanf("%d",&pos);
if(pos>n)
{
printf("invalid position, Enter again :");
scanf("%d",&pos);
}
printf("enter the character\n");
b[0]='d';
b[1]='l';
b[2]='e';
b[3]='s';
b[4]='t';
b[5]='x';
j=6;
while(i<n)
{
if(i==pos-1)
{
b[j]='d';
b[j+1]='l';
b[j+2]='e';
b[j+3]=ch;
b[j+4]='d';
b[j+5]='l';
b[j+6]='e';
j=j+7;
}
if(a[i]=='d' && a[i+1]=='l' && a[i+2]=='e')
{
b[j]='d';
b[j+1]='l';
b[j+2]='e';
j=j+3;
}
b[j]=a[i];
i++;
j++;
}
b[j]='d';
b[j+1]='l';
OUTPUT:
[m0033@agni cnlabprograms]$ ./a.out
enter string
archana
enter position
dlestxardlehdlechanadleetx
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
AIM: Implement on a data set of characters the three CRC polynomials – CRC 12, CRC 16 and CRC CCIP.
THEORY:
CRC method can detect a single burst of length n, since only one bit per column will be changed, a burst of length
n+1 will pass undetected, if the first bit is inverted, the last bit is inverted and all other bits are correct. If the block is
badly garbled by a long burst or by multiple shorter burst, the probability that any of the n columns will have the
correct parity that is 0.5. so the probability of a bad block being expected when it should not be 2 power(-n). This
scheme some times known as Cyclic Redundancy Code
ALGORITHM/FLOWCHART:
Begin
Step 1: Declare I,j,fr[8],dupfr[11],recfr[11],tlen,flag,gen[4],genl,frl,
rem[4] as integer
Step 2: initialize frl=8 and genl=4
Step 3: initialize i=0
Step 4: Repeat step(5to7) until i<frl
Step 5: read fr[i]
Step 6: dupfr[i]=fr[i]
Step 7: increment i
Step 8: initialize i=0
Step 9: repeat step(10to11) until i<genl
Step 10: read gen[i]
Step 11: increment i
Step 12: tlen=frl+genl-1
Step 13: initialize i=frl
Step 14: Repeat step(15to16) until i<tlen
Step 15: dupfr[i]=0
Step 16: increment i
SOURCE CODE:
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++)
{
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
enter generator:1
enter frame:1
enter generator:1
PROG -2
// Include headers
#include<stdio.h>
#include<string.h>
// length of the generator polynomial
#define N strlen(gen_poly)
// data to be transmitted and received
char data[28];
// CRC value
char check_value[28];
// generator polynomial
char gen_poly[10];
// variables
int data_length,i,j;
// function that performs XOR operation
void XOR(){
// if both bits are the same, the output is 0
// if the bits are different the output is 1
for(j = 1;j < N; j++)
check_value[j] = (( check_value[j] == gen_poly[j])?'0':'1');
}
// Function to check for errors on the receiver side
void receiver(){
// get the received data
printf("Enter the received data: ");
scanf("%s", data);
printf("\n-----------------------------\n");
printf("Data received: %s", data);
// Cyclic Redundancy Check
crc();
// Check if the remainder is zero to find the error
for(i=0;(i<N-1) && (check_value[i]!='1');i++);
if(i<N-1)
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
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
Solution:
In computer networks sliding window protocol is a method to transmit data on a network. Sliding
window protocol is applied on the Data Link Layer of OSI model. At data link layer data is in the
form of frames. In Networking, Window simply means a buffer which has data frames that needs
to be transmitted.
Both sender and receiver agrees on some window size. If window size=w then after sending w
frames sender waits for the acknowledgement (ack) of the first frame.
As soon as sender receives the acknowledgement of a frame it is replaced by the next frames to
be transmitted by the sender. If receiver sends a collective or cumulative acknowledgement to
sender then it understands that more than one frames are properly received, for eg:- if ack of
frame 3 is received it understands that frame 1 and frame 2 are received properly.
Image Source
In sliding window protocol the receiver has to have some memory to compensate any loss in
transmission or if the frames are received unordered.
W = Window Size
tx = Transmission time
tp = Propagation delay
It is of two types:-
1. Selective Repeat: Sender transmits only that frame which is erroneous or is lost.
2. Go back n: Sender transmits all frames present in the window that occurs after the error bit
including error bit also.
#include<stdio.h>
int main()
{
int w,i,f,frames[50];
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]);
if(f%w!=0)
printf("\nAcknowledgement of above frames sent is received by sender\n");
return 0;
}
Output :
AIM: Implement Dijkstra„s algorithm to compute the Shortest path thru a given graph.
THEORY:
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]
SOURCE CODE:
//*********************************
//5 .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
OUTPUT:
[m0033@agni cnlabprograms]$ ./a.out
01420
10370
43050
27506
00060
12345
12450
13450
14500
minimum cost 8
VIVA QUESTIONS:
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:
SOURCE CODE:
#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()
{
printf("\n Enter the number of nodes");
scanf("%d",&n);
for(i=0;i<n;i++)
A B C D
A 1 3 5 6
B 6 7 8 9
C 2 3 5 6
D 1 2 3 7
Minimum cost is 9
BA6
DA1
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
AIM: Obtain Routing table at each node using distance vector routing algorithm for a given subnet.
THEORY:
Distance Vector Routing Algorithms calculate a best route to reach a destination based solely on distance. E.g. RIP.
RIP calculates the reach ability based on hop count. It‟s different from link state algorithms which consider some
other factors like bandwidth and other metrics to reach a destination. Distance vector routing algorithms are not
preferable for complex networks and take longer to converge.
#include<stdlib.h>
#define NUL 1000
#define NODES 10
struct node
{
int t[NODES][3];
};
struct node n[NODES];
typedef struct node NOD;
int main()
{
void init(int,int);
void inp(int,int);
void caller(int,int);
void
op1(int,int,int);
void find(int,int);
int i,j,x,y,no;
clrscr();
do{
printf("\n Enter the no of nodes required:");
scanf("%d",&no);
}while(no>10||no<0);
for(i=0;i<no;i++)
OUTPUT
Enter the dists from the nodes 1 to other node...
Pls enter 999 if there is no direct
/* Enter dist to node 2=3
Enter dist to node 3=8
Enter dist to node 4=6
Enter dist to node 5=999
Enter the dists from the nodes 2 to other node...
Pls enter 999 if there is no direct
Enter dist to node 1=3
Enter dist to node 3=999
OUTPUT:
024
204
450
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?
AIM: Take a 64 bit playing text and encrypt the same using DES algorithm.
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
ALGORITHM/FLOWCHART:
Begin
Step1: Initialize as int i,ch,lp;
Step2: Initialize as char cipher[50],plain[50];
Step3: Initialize as char key[50];
Step4: while(1) repeat steps(4-36)
Step5: write "\n-----MENU -----\n"
Step6: write "\n1:Data Encryption\t\n\n2:Data Decryption\t\n\n3:Exit"
Step7: write ("\n\nEnter your choice:"
Step8: read"%d",&ch
Step9: stament switch(ch) repeat steps(9-35)
case 1:
step10: read "\nData Encryption"
step11:read ("\nEnter the plain text:"
step12: fflush(stdin)
step13 : gets(plain)
step14: write "\nEnter the encryption key:"
step15: gets(key)
step16: lp=strlen(key)
step17: Initialize i=0
SOURCE CODE:
/*Take a 64 bit playing text and encrypt the same using DES algorithm */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
int i,ch,lp;
char cipher[50],plain[50];
while(1)
scanf("%d",&ch);
switch(ch)
fflush(stdin);
scanf("%s",&plain);
scanf("%s",&key);
lp=strlen(key); for(i=0;plain[i]!='\
0';i++)
cipher[i]=plain[i]^lp;
cipher[i]='\0';
puts(cipher);
break;
for(i=0;cipher[i]!='\0';i++)
plain[i]=cipher[i]^lp; printf("\
puts(plain);
break;
OUTPUT:
[m0033@agni cnlabprograms]$ ./a.out
-----MENU-----
1:Data
Encryption
2:Data Decryption
3:Exit
Data Encryption
-----MENU-----
1:Data
Encryption
2:Data Decryption
3:Exit
Data decryption
VIVA QUESTIONS:
Sreyas Institute Of Engineering And Technology
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
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void main()
{ int
packets[8],i,j,clk,b_size,o_rate,i_rate,p_sz_rm=0,p_sz,p_time;
clrscr();
for(i=0;i<5;++i)
{ packets[i]=rand()%10;
if(packets[i]==0) --i;
}
printf("Enter output rate:");
scanf("%d",&o_rate);
printf("\nEnter bucket size:");
scanf("%d",&b_size);
for(i=0;i<5;++i)
{ if((packets[i]+p_sz_rm) > b_size)
{ if(packets[i]>b_size)
printf("\nIncoming packet size:%d greater than
bucket capacity\n",packets[i]);
else printf("Bucket size exceeded\n");
}
else
{
p_sz=packets[i];
p_sz_rm+=p_sz;
printf("\n
-\n");
printf("Incoming packet:%d",p_sz); printf("\
nTransmission left:%d\n",p_sz_rm);
p_time=rand()%10;
printf("Next packet will come at %d",p_time);
for(clk=0;clk<p_time&&p_sz_rm>0;++clk)
{
printf("\nTime left %d---No packets to
transmit!!\n",p_time-clk);
Output:
Enter output rate:2
Enter bucket size:10
Incoming packet:6
Transmission left:6
Next packet will come at 7
Time left 7 ------- No packets to transmit!!
Transmitted
Bytes remaining:4
Time left 6 ------- No packets to transmit!!
Transmitted
Bytes remaining:2
Time left 5 ------- No packets to transmit!!
Transmitted
Bytes remaining:0
Incoming packet:0
Transmission left:0
Next packet will come at 5
Incoming packet:2
Transmission left:2
Next packet will come at 5
Incoming packet:0
Transmission left:0
Next packet will come at 8
Incoming packet:6
Transmission left:6
Next packet will come at 6
Time left 6 ------- No packets to transmit!!
Transmitted
Bytes remaining:4
Time left 5 ------- No packets to transmit!!
Transmitted
Bytes remaining:2
Time left 4 ------- No packets to transmit!!
Transmitted
Bytes remaining:0
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct frame{
int fslno;
char finfo[20];
};
struct frame arr[10];
int n;
void sort()
{
int i,j,ex;
struct frame temp;
for(i=0;i<n;i++)
{
ex=0;
for(j=0;j<n-i-1;j++)
if(arr[j].fslno>arr[j+1].fslno)
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
ex++;
}
if(ex==0) break;
}
}
void main()
{
int i;
clrscr();
printf("\n Enter the number of frames \n");
scanf("%d",&n);
for(i=0;i<n;i++)
OUTPUT
Enter the number of frames:10
Enter the frame contents for sequence number 3
institute
Enter the frame contents for sequence number 25
Of
Enter the frame contents for sequence number 8
Technology
Enter the frame contents for sequence number 28
is
Enter the frame contents for sequence number 19
a
Enter the frame contents for sequence number 33
very
Enter the frame contents for sequence number 14
good
Enter the frame contents for sequence number 41
college
Enter the frame contents for sequence number 45
did
Enter the frame contents for sequence number 1
SREYAS
The frames in sequence
1 SREYAS
3 institute
8 technology
14 good