CN Lab
CN Lab
Implement the data link layer framing methods such as Bit Stuffing.
Theory
Security and Error detection are the most prominent features that are to be provided by any
application which transfers data from one end to the other end. One of such a mechanism in
tracking errors which may add up to the original data during transfer is known as Stuffing. It is of
two types namely Bit Stuffing and the other Character Stuffing. Coming to the Bit Stuffing,
01111110 is appended within the original data while transfer of it. The following program
describes how it is stuffed at the sender end and de-stuffed at the reciever end.
Program:
#include
main()
{
int a[15];
int i,j,k,n,c=0,pos=0;
clrscr();
printf("\n Enter the number of bits");
scanf("%d",&n);
printf("\n Enter the bits");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]==1)
{
c++;
if(c==5)
{
pos=i+1;
c=0;
for(j=n;j>=pos;j--)
{
k=j+1;
a[k]=a[j];
}
a[pos]=0;
n=n+1;
}
}
else
c=0;
}
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
printf(" 01111110 ");
getch();
}
Output:
Aim:
Implement the data link layer framing methods such as Character Stuffing and also De-stuff it
Theory
Coming to the Character Stuffing, DLESTX and DLEETX are used to denote start and end of character
data with some constraints imposed on repetition of charaters as shown in the program below clearly.
Program:
#include
#include
#include
void charc(void);
void main()
{
int choice;
while(1)
{
printf("\n\n\n1.character stuffing");
printf("\n\n2.exit");
printf("\n\n\nenter choice");
scanf("%d",&choice);
printf("%d",choice);
if(choice>2)
printf("\n\n invalid option....please renter");
switch(choice)
{
case 1:
charc();
break;
case 2:
exit(0);
}
}
}
void charc(void)
{
char c[50],d[50],t[50];
int i,m,j;
clrscr();
printf("enter the number of characters\n");
scanf("%d",&m);
printf("\n enter the characters\n");
for(i=0;i<m+1;i++)
{
scanf("%c",&c[i]);
}
printf("\n original data\n");
for(i=0;i<m+1;i++)
printf("%c",c[i]);
d[0]='d';
d[1]='l';
d[2]='e';
d[3]='s';
d[4]='t';
d[5]='x';
for(i=0,j=6;i<m+1;i++,j++)
{
if((c[i]=='d'&&c[i+1]=='l'&& c[i+2]=='e'))
{
d[j]='d';
j++;
d[j]='l';
j++;
d[j]='e';
j++;
m=m+3;
}
d[j]=c[i];
}
m=m+6;
m++;
d[m]='d';
m++;
d[m]='l';
m++;
d[m]='e';
m++;
d[m]='e';
m++;
d[m]='t';
m++;
d[m]='x';
m++;
printf("\n\n transmitted data: \n");
for(i=0;i<m;i++)
{
printf("%c",d[i]);
}
for(i=6,j=0;i<m-6;i++,j++)
{
if(d[i]=='d'&&d[i+1]=='l'&&d[i+2]=='e'&&d[i+3]=='d'&&d[i+4]=='l'&&d[i+5]=='e')
i=i+3;
t[j]=d[i];
}
printf("\n\nreceived data:");
for(i=0;i<j;i++)
{printf("%c",t[i]);
}
}
Output:
Aim:
Implement on a data set of characters the CRC polynomials.
Theory
CRC means Cyclic Redundancy Check. It is the most famous and traditionally successful mechanism used
in error detection through the parity bits installed within the data and obtaining checksum which acts as
the verifier to check whether the data retreived at the reciever end is genuine or not. Various operations
are involved in implementing CRC on a data set through CRC generating polynomials. In the program, I
have also provided the user to opt for Error detection whereby he can proceed for it. Understand the
program below as it is much simpler than pretented to be so.
Program:
#include
#include
#include
#define N strlen(g)
void xor() {
for(c=1;c}
void crc() {
for(e=0;e do {
if(cs[0]=='1') xor();
for(c=0;c cs[c]=t[e++];
}while(e<=a+N-1);
}
void main() {
clrscr();
printf("\nEnter poly : "); scanf("%s",t);
printf("\nGenerating Polynomial is : %s",g);
a=strlen(t);
for(e=a;e
printf("\nModified t[u] is : %s",t);
crc();
printf("\nChecksum is : %s",cs);
for(e=a;e printf("\nFinal Codeword is : %s",t);
printf("\nTest Error detection 0(yes) 1(no) ? : ");
scanf("%d",&e);
if(e==0) {
printf("Enter position where error is to inserted : ");
scanf("%d",&e);
t[e]=(t[e]=='0')?'1':'0';
printf("Errorneous data : %s\n",t);
}
crc();
for (e=0;(e<n-1)&&(cs[e]!='1');e++);
if(e
else printf("No Error Detected.");
getch();
}
Output:
Aim:
Implement Dijkstra's algorithm to compute the Shortest path through a graph.
Theory
Dijkstra's algorithm is a non-adaptive routing algorithm which is very widely used to route packets from
source to detination through various routers available during the transmission. It is implemented at the
network layer of the architecture where data packets are sent through routers which maitain routing
tables that help to denote the exact location to where the destined packets need to be delivered. Major
advantage in using Dijkstra's algorithm is that it forwards the data packets from source to destination
through the most optimized path in terms of both the distance and cost observed. It prompts the user to
enter the number of nodes and the source and destination nodes among them. In addition, the
algorithm written below also asks for the neighbours to each node with the distances to reach to them
from each node is also prompted. All this data is stored and used further to calculate and estimate the
best path possible for data packets to reach their destination from source. Program below explains it in
a much better way.
Program:
#include
#include
int n,s,nb,nbs[15],snbs[15],delay[15][15],i,j,temp[15],ze=0;
void min();
void main()
{
clrscr();
printf("Enter the no.of nodes:");
scanf("%d",&n);
printf("\nEnter the source node:");
scanf("%d",&s);
printf("\nEnter the no.of Neighbours to %d:",s);
scanf("%d",&nb);
printf("\nEnter the Neighbours:");
for(i=1;i<=nb;i++)
scanf("%d",&nbs[i]);
printf("\nEnter the timedelay form source to nbs:");
for(i=1;i<=nb;i++)
scanf("%d",&snbs[i]);
for(i=1;i<=nb;i++)
{
printf("\nEnter the timedelay of %d: ",nbs[i]);
for(j=1;j<=n;j++)
scanf("%d",&delay[i][j]);
}
for(i=1;i<=nb;i++)
{
printf("\nThe timedelays of %d: ",nbs[i]);
for(j=1;j<=n;j++)
printf("%3d",delay[i][j]);
}
min();
getch();
}
void min()
{
int sum,k,y=1,store=1;
printf("\n\t\t\tnew- rout");
printf("\n\t\t\ttime-");
printf("\n\t\t\tdelay");
printf("\n");
for(i=1;i<=n;i++)
{
sum=0;
k=1;
for(j=1;j<=nb;j++)
{
temp[k++]=delay[j][i];
}
sum=temp[1]+snbs[1];
for(y=2;y<=nb;y++)
{
if(sum>temp[y]+snbs[y])
{
sum=temp[y]+snbs[y];
store=y;
}
}
if(s==i)
printf("\n\t%d+\t%d =\t%d --",ze,ze,ze);
else
printf("\n\t%d +\t%d =\t%d\t%d",temp[store],snbs[store],sum,nbs[store]);
}
}
Output:
Aim:
Take an example subnet graph with weights indicating delay between nodes. Now obtain Routing
table at each node using Distance Vector Routing.
Theory
Distance Vector routing (DVR) algorithm is unlike Dijkstra's algorithm which is a non-adaptive routing
algorithm and means that it is purely static, that is pre-destined and fixed, not flexible in networks
where congestions are more prone to occur. DVR is an adaptive routing algorithm in which the
information from neighbours is maitained well by each and every node and this helps us to determine
the simplest path possible in a changing network. Though, one of the node may fail, still, the destined
node is reachable through other possible intermediate nodes that are found out by the DVR algorithm.
The perfectly executing program below shows it live below.
Program:
#include
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("\nEnter 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("\n\nState value for router %d is \n",i+1);
for(j=0;j<n;j++)
{
printf("\t\nnode %d via %d Distance%d",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf("\n\n");
}
Aim:
Take an example subnet of hosts. Obtain broadcast tree for it.
Theory
IP addressing is the allocation of unique ID to each and every system connected in a network to maintan
communication among them through out the affixed network. There are 5 classes of IP Addresses
namely A through E with the range varying from one class to the other class.
A subnet is a network allocation to similar systems or same hierarchial systems present in a allocated
network like an organisation. Each and every system can be reachd through a client-server computing
environment where the server acts as the Master and the clients acts as the Slaves to form a Master-
Slave computing environment. Below programs show the calculation of network addresses with subnet
predefinition and subnet generation.
Program:
a)Network Address:
#include
#include
void main()
{
unsigned int compad[4];
unsigned int mask[4];
unsigned int netadr[4];
int i;
clrscr();
printf("Enter the ip address:\n");
scanf("%u%*c%u%*c%u%*c%u%*c",&compad[3],&compad[2],&compad[1],&compad[0]);
printf("Enter the subnet address:\n");
scanf("%u%*c%u%*c%u%*c%u%*c",&mask[3],&mask[2],&mask[1],&mask[0]);
for(i=0;i<4;i++)
{
netadr[i]= compad[i]&mask[i];
}
printf("\nNetwork address is:\n");
printf("%u.%u.%u.%u",netadr[3],netadr[2],netadr[1],netadr[0]);
printf("\nsubnet address is:\n");
printf("%u.%u.%u.%u",mask[3],mask[2],mask[1],mask[0]);
printf("\nip address is:\n");
printf("%u.%u.%u.%u",compad[3],compad[2],compad[1],compad[0]);
getch();
}
Output:
b)Network address with automatic subnet address generation:
#include
#include
void main()
{
unsigned int compad[4];
unsigned int mask[4];
unsigned int netadr[4];
unsigned long int ma=0;
int i,pre;
clrscr();
printf("Enter the ip address:\n");
scanf("%u%*c%u%*c%u%*c%u%*c",&compad[3],&compad[2],&compad[1],&compad[0]);
printf("Enter the prefix:\n");
scanf("%ul",&pre);
for(i=(32-pre);i<32;i++)
ma=ma|(1<<i);
for(i=0;i<4;i++)
{
mask[i]=ma%256;
ma=ma/256;
}
for(i=0;i<4;i++)
{
netadr[i]= compad[i]&mask[i];
}
printf("\nNetwork address is:\n");
printf("%u.%u.%u.%u",netadr[3],netadr[2],netadr[1],netadr[0]);
printf("\nsubnet address is:\n");
printf("%u.%u.%u.%u",mask[3],mask[2],mask[1],mask[0]);
printf("\nip address is:\n");
printf("%u.%u.%u.%u",compad[3],compad[2],compad[1],compad[0]);
getch();
}
Output:
Conclusion:
I personally found these programs very useful during my preparation for examinations. Not only from
the examination point of view, I have used the above concepts in my academic as well as professional
projects which also include concepts regarding Operating Systems, Compiler Design, Web
Technologies and UML diagrams. A combination of all these concepts in terms of programs as depicted
above would really mean a lot for every Computer Science backgroun student.
Programs Page
S.No
No
PART – A: Computer
Networks
Implement the data link layer framing methods such as character stuffing
1
and bit stuffing.
2 Implement on a data set of characters the three CRC polynomials – CRC
12, CRC 16 and CRC CCIP.
Implement Dijkstra’s algorithm to compute the Shortest path thru a
3
graph.
Take an example subnet graph with weights indicating delay between
4 nodes. Now obtain Routing table art each node distance vector routing
algorithm.
5 Take an example subnet of hosts. Obtain broadcast tree for it.
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct frame
{
char s[50],len[10],flag[10];
};
void main()
{
int i,j=0,l1,l2;
struct frame b;
clrscr();
printf("enter the string\n");
gets(b.s);
l1=strlen(b.s);
l2=l1/8;
printf("length=%d\tbytes=%d",l1,l2);
printf("\nflag\t\tdata\t\t\tendflag\n");
strcpy(b.flag,"01111110");
printf("%s",b.flag);
for(i=0;b.s[i]!='\0';i++)
if(b.s[i]=='0')
{
printf("%2c",b.s[i]);
}
else
{
printf("%2c",b.s[i]);
j++;
if(j==5)
{
printf(" 0");
j=0;
}
}
printf("\t\t%s",b.flag);
getch();
}
INPUT/OUTPUT:
1)Enter the bit stream(0,1) of the frame 0111000011101
After the bit stuffing the frame is 01111110011100001110101111110
2)Enter the bit stream(0,1) of the frame 01111101111110
After the bit stuffing the frame is 01111110011111001111101001111110
CONCLUSION:
The output is correct and the program is error free
2) Name of the Experiment: Implementation on CRC polynomials.
CRC Encoding algorithm
AIM: To write a program to implement CRC Encoding algorithm
DESCRIPTION: (CYCLIC REDUNDANCY CHECK) This program is used for encoding the
given bits using the generated string. The encoded information is transmitted to the other
end.For encoding we perform the exclusive operation.
ALGORITHM:
1)Start
2)Read the frame string to cg
3)Read the generated string to cg
4)for the entire string length of cg
a) Concatenate the string 0 to cp
5)print the cp
6)for the entire string length of cp a)calculate p[i]as cp[i]-‘0’
7)for the entire string length of cg a)calculate g[i]ascg[i]-‘0’
8)Initialize i and j to zero
9)while(1)
a)Initialize j to zero b)While p[j] is not equal to 1
i)Increment j
ii)if j is grater than strlen(cp)-(strlen(cg)-1) then do 1)goto step a
iii)Initialize k to zero
iv)for i=j to j+strlen(cg) step i and step k 1)p[i]=p[i]^g[k]
10)for i=strlen(cp)-1 to strlen(cp)-(strlen(cg)-1) step down i a)cp=p[i]-‘0’
11)Print cp which has the data to be transmitted 12)Stop
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int lt,lp;
int I,j,k;
char pol[10],temp[20],data[20],data1[20];
clrscr();
printf(“Enter the polynomial”);
gets(pol);
printf(“\n Enter the data to send”);
gets(data);
strcpy(data1,data);
strcpy(temp,data);
for(i=strlen(pol)-1;i>0;i--)
strcat(temp,"0");
printf("\n if sender:\n");
printf("\n after adding the zeros:%s\n",temp);
lp=strlen(pol);
lt=strlen(temp);
while(lt>=lp)
{
for(i=0;i<lp;i++)
temp[i]=temp[i]==pol[i]?'0':'1';
while(temp[0]=='0')
{
for(i=0;i<lt;i++)
temp[i]=temp[i+1];
lt=strlen(temp);
}
}
printf("\n remainder=");
for(k=lp-1-lt;k;k--)
{
printf("0");
strcat(data,"0");
}
strcat(data,temp);
printf("%s",temp);
printf("\n\n:transmited data=%s",data);
printf("\n\n if receiver");
printf("\n\n remainder=");
for(i=1;i<lp;i++)
printf("0");
printf("\n\n output data=%s",data);
getch();
}
10. Stop.
INPUT/OUTPUT:
1. crc received string : 11010110111110 Enter the polynomial string 10011
No errors.
The data received is: 1101011011
2. crc received string is : 1110001010 Enter the polynomial string 11010
ERROR.
3. crc received string : 101010110 Enter the polynomial string 10001
No errors
The data received is : 111000010.
CONCLUSION:
Hence the program is error free.
3) Name of the Experiment: Implementation on Dijkstra’s algorithm.
AIM: Implement Dijkstra’s algorithm to compute the shortest path
thru a graph.
DESCRIPTION: This program is used for finding the shortest path between nodes. Path is
found based on the cost of the path existing between the nodes. Among different paths it
selects the minimum path between source and destination.
ALGORITHM:
1. Start.
2. Read the number of nodes to n.
3. Read all the distances to matrix ‘mat’.
4. Read the source to variable ‘sor’.
5. vnod[v++]=sor
6. Read the destination to des
7. a. cou is incremented .
b.assign visit to zero.
c.assign 1000 to small.
d.for j from 0 to n incrementing j in steps of 1 and assigning visit to zero repeat
the following steps.
If mat[sor][j]<small && mat [sor][j]!=0then do the following: for k from 0 to v-1 do
if j=vnod[k] assign 1 to visit if visit!=1 do
small=mat[sor][j] assign j to sor vnod[v++]=sor
if sor=des then break
e.if sor is not equal to des or cou=n then goto step a otherwise goto step
9. Print the path-vnod[0]+65.
10. for i=1 to v print the vnod[1]+65.
11. Stop.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#define INFINITY 1000
int min,i=0,j,k=0,n,no=0,dist[10][10],e1,e2,w=0,path[10];
void shortpath(int,int);
void main()
{
int so=0,de=0;
clrscr();
printf("\n Enter the no.of nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
dist[i][j]=0;
}
while(1)
{
printf("\n Enter the edge to and from and the weight:");
scanf("%d%d%d",&e1,&e2,&w);
if((e1==0)&&(e2==0))
break;
else
{
dist[e1][e2]=w;
dist[e2][e1]=w;
}
}
fflush(stdin);
printf("\n enter the source and destination nodes:");
scanf("%d%d",&so,&de);
shortpath(so,de);
printf("path is:");
for(i=0;i<no;i++)
printf("%3d",path[i]);
getch();
}
void shortpath(int s,int t)
{
struct state
{
int pre;
int length;
enum{perm,tent}label;
}state[10];
struct state *p;
for(p=&state[0];p<&state[n];p++)
{
p->pre=-1;
p->length=INFINITY;
p->label=tent;
}
state[t].length=0;
state[t].label=perm;
k=t;
do
{
for(i=0;i<n;i++)
{
if(dist[k][i]!=0&&state[i].label==tent)
{
if((state[k].length+dist[k][i])<(state[i].length))
{
state[i].pre=k;
state[i].length=state[k].length+dist[k][i];
}
}
}
k=0;
min=INFINITY;
for(i=0;i<n;i++)
{
if((state[i].label==tent)&&(state[i].length<min))
{
min=state[i].length;
k=i;
}
}
state[k].label=perm;
}
while(k!=s);
k=s;
do
{
path[no++]=k;
k=state[k].pre;
}
while(k>=0);
return;
}
for(i=0;i<noadj;i++)
{
printf(“\n Enter time delay to adj node %d and node name of adj node
%d:”,i+1,i+1); /* reading time delay and node name*/
scanf(“%d%c”,&edel[i],&adver[i]);
}
for(i=0;i<noadj;i++)
{
printf(“\nEnter the time delay from %c to all other nodes:”,adver[i]);
for(j=0;j<nv;j++)
scanf(“%d”,&tdel[i][j]);
}
printf(“\n Destination Node \t Adjacent Node \t Shortest Delay”);
for(i=0;i<nv;i++)
{
min=1000;
ch=0;
for(j=0;j<noadj;j++)
if(min>tdel[j][i]+edel[j])) /* finding the shortest path */
{
min=tdel[j][i]+edel[j];
ch=adver[j];
}
if(i!=sv-1)
printf(“\n %d \t \t %d”,i+1,ch,min); else
printf(\n %d-0 »,i+1) ;
}
getch();
}
OUTPUT:
1) Enter no. of vertices 17 Enter source vertex 1a
Enter full table for source vertex 1a
1a 0 0
1b 1b 1
1c 1c 1
2a 1c 2
2b 1c 3
2c 1c 4
2d 1c 3
3a 1c 2
3b 1c 3
4a 1c 4
4b 1c 5
4c 1c 5
5a 1c 5
5b 1c 5
5c 1c 4
5d 1c 5
5e 1c 6
HIERARCHICAL TABLE
1a 0 0
1b 1b 1
1c 1c 1
2 1c 2
3 1c 2
4 1c 4
5 1c 5