Network Lab for Mca
Network Lab for Mca
INDEX
S. No Name of the Experiment
1. Hamming Code
NETWORK LAYER
ROUTING ALGORITHMS
AIM: To write a C program to implement Hamming code for Error Detection and Error
Correction.
ALGORITHM:
Step 1: Write the bit positions starting from 1 in binary form (1, 10, 11, 100, etc).
Step 2: All the bit positions that are a power of 2 are marked as parity bits (1, 2, 4, 8, etc).
Step 3: All the other bit positions are marked as data bits.
Step 4: Each data bit is included in a unique set of parity bits, as determined its bit position in binary
form:
a. Parity bit 1 covers all the bits positions whose binary representation includes a 1 in the least
significant position (1, 3, 5, 7, 9, 11, etc).
b. Parity bit 2 covers all the bits positions whose binary representation includes a 1 in the second
position from the least significant bit (2, 3, 6, 7, 10, 11, etc).
c. Parity bit 4 covers all the bits positions whose binary representation includes a 1 in the third position
from the least significant bit (4–7, 12–15, 20–23, etc).
d. Parity bit 8 covers all the bits positions whose binary representation includes a 1 in the fourth
position from the least significant bit bits (8–15, 24–31, 40–47, etc).
e. In general, each parity bit covers all bits where the bitwise AND of the parity position and the bit
position is non-zero.
Step 5: Since we check for even parity set a parity bit to 1 if the total number of ones in the positions it
checks is odd. Set a parity bit to 0 if the total number of ones in the positions it checks is even.
SOURCE CODE:
#include <stdio.h>
#include
<math.h> int
input[32];
int code[32];
int ham_calc(int,int);
void main()
scanf("%d",&n);
+)
scanf("%d",&input[i]);
i=0;
while(n>(int)pow(2,i)-(i+1))
p_n++;
i++;
c_l = p_n + n;
j=k=0;
for(i=0;i<c_l;i++)
code[i]=0;
k++;
else
code[i]=input[j]; j+
+;
for(i=0;i<p_n;i++)
int value =
ham_calc(position,c_l);
code[position-1]=value;
for(i=0;i<c_l;i++)
printf("%d",code[i]);
printf("\n");
for(i=0;i<c_l;i++)
scanf("%d",&code[i]);
int error_pos = 0;
for(i=0;i<p_n;i++)
ham_calc(position,c_l); if(value !
= 0)
error_pos+=position;
if(error_pos == 1)
else
int
count=0,i,j;
i=position-1;
while(i<c_l)
for(j=i;j<i+position;j++)
if(code[j] == 1)
count++;
i=i+2*position;
if(count%2 == 0)
return 0;
else
}
$ cc hamming.c
$ ./a.out
AIM: To write a C program to implement Cyclic Redundancy Check (CRC) code for Error
Detection.
ALGORITHM:
Step 2: Accept the number of bits in the input data stream (t).
Step 5: Append (g-3) ‘0’ bits to the end of the input data bits.
Step 7: Remove the appended ‘0’ bits and append the remainder bits to the
Step 9: If the reminder in zero then there is no error in the transmitted bits
SOURCE CODE:
#include<stdio.h>
#include<string.h>
#define N strlen(g)
char t[28],cs[28],g[]="10001000000100001";
int a,e,c;
void xor()
void crc()
for(e=0;e<N;e++)
cs[e]=t[e];
do{ if(cs[0]=='
1')
xor();
for(c=0;c<N-1;c++)
cs[c]=cs[c+1];
cs[c]=t[e++];
while(e<=a+N-1);
int main()
printf("\n ");
a=strlen(t);
for(e=a;e<a+N-1;e++)
t[e]='0';
printf("\n ");
printf("\n ");
crc();
printf("\nChecksum is :
%s",cs); for(e=a;e<a+N-1;e++)
t[e]=cs[e-a];
printf("\n ");
n ");
scanf("%d",&e);
if(e==0)
do{
scanf("%d",&e);
}while(e==0 || e>a+N-1);
t[e-1]=(t[e-1]=='0')?'1':'0';
printf("\n ");
+); if(e<N-1)
printf("\nError detected\n\n");
else
printf("\n \n");
return 0;
$ vi crc.c
$ cc crc.c
$ ./a.out
AIM: To write a C program to simulate and implement stop and wait protocol for noisy channel.
ALGORITHM:
Step 4: The sender sends one data packet at a time and sends next packet only after receiving
acknowledgement for previous.
Step 5: Using the sleep function acknowledgement is paused for few seconds.
Step 6: The stopped frames are retransmitted and acknowledgment is received for those frames.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
int i,j,noframes,x,x1=10,x2;
clrscr();
for(i=0;i<200;i++)
rand();
noframes=rand()/200;
i=1;
j=1;
noframes=noframes/8;
getch();
while(noframes>0)
srand(x1++);
x=rand()%10;
for(x2=1;x2<2;x2++)
sleep(x2);
srand(x1++);
x=rand()%10;
%d",j); noframes-=1;
i++;
j++;
getch();
$ vi stopwait.c
$ cc stopwait.c
$ ./a.out
AIM: To write a C program to simulate and implement Go back n sliding window protocol.
ALGORITHM:
Step 3: Define the window Size by getting input from the user.
Step 5: When acknowledgment is not received for a frame, then the sender retransmits the frames from
the last transmitted frame.
Step 6: After all frames are sent successfully stop the process.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int main()
int windowsize,sent=0,ack,i;
clrscr();
scanf("%d",&windowsize);
while(1)
sent++;
if(sent == windowsize)
break;
received.\n");
scanf("%d",&ack);
if(ack == windowsize-
1)
getch();
break;
sent = ack;
return 0;
$ vi goback.c
$ cc goback.c
$ ./a.out
AIM: To write a C program to simulate and implement Selective Repeat Sliding Window Protocol.
ALGORITHM:
Step 4: In the sender function enter the number of packets and data to be
sent.
Step 5: In recvfrm function get the packet number that is not received using rand() function.
Step 6: In the resend() function the packet for which acknowledgment is not received is resent.
Step 7: After all frames are sent successfully stop the process
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int n,r;
struct frame
char ack;
int data;
}frm[10];
int sender(void);
void recvfrm(void);
void resend(void);
void selective(void);
void main()
clrscr();
selective();
getch();
void selective()
sender();
recvfrm();
resend();
int sender()
int i;
scanf("%d",&n);
for(i=0;i<n;i++)
packets[%d]",i);
scanf("%d",&frm[i].data);
frm[i].ack='y';
return 0;
void recvfrm()
int i;
rand();
r=rand()%n;
frm[r].ack='n';
for(i=0;i<n;i++)
if(frm[i].ack=='n')
void resend()
sleep(2);
frm[r].ack='y';
$ vi selectiverepeat.c
$ cc selectiverepeat.c
$ ./a.out
AIM: To write a C program to simulate and implement Distance Vector Routing algorithm.
ALGORITHM:
Step 3: Accept the input distance matrix from which represents the distance between each node in the
network.
Step 6: If the distance between two nodes is larger than the calculated alternate available path, replace the
existing distance with the calculated distance.
SOURCE CODE:
#include<stdio.h>
struct node
unsigned dist[20];
unsigned from[20];
}rt[10];
void main()
int dmat[20][20];
int n,i,j,k,count=0;
scanf("%d",&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\n");
getch();
$ vi distancevector.c
$ cc distancevector.c
$ ./a.out
AIM: To write a C program to simulate and implement Dijkstra algorithm for shortest path
routing.
ALGORITHM:
Step 3: Declare all necessary variables and function for shortest path().
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
int
cost[10][10],dist[20],i,j,n,k,m,S[20],v,totcost,pat
h[20],p;
main()
int c;
cin >> n;
cin >>m;
+)
cost[i][j]=c;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]==0) cost[i]
[j]=31999;
cin >>v;
+)
{ S[i]=
0;
dist[i]=cost[v][i];
path[++p]=v;
S[v]=1;
dist[v]=0;
for(i=2;i<=n-1;i++)
k=-1;
min=31999;
for(j=1;j<=n;j++)
min=dist[j];
k=j;
if(cost[v][k]<=dist[k])
p=1;
path[++p]=k;
cout<<path[j];
cout <<"\n";
//cout <<k;
S[k]=1;
for(j=1;j<=n;j++)
if(cost[k][j]!=31999 &&
dist[j]=dist[k]+cost[k][j];
$ vi dijsktras.c
$ cc dijsktras.c
$ ./a.out
AIM: To write a C program for congestion control using Leaky bucket algorithm.
ALGORITHM:
Step 5: Repeat the process of transmission until all packets are transmitted. Reject packets where its size
is greater than the bucket size.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#define NOF_PACKETS 10
int rand(int a)
return rn == 0 ? 1 : rn;
int main()
for(i = 0; i<NOF_PACKETS; +
+i)
scanf("%d", &o_rate);
else
else
p_sz_rm += packet_sz[i];
sleep(1);
if(p_sz_rm)
op = p_sz_rm, p_sz_rm = 0;
else
$ vi leaky.c
$ cc leaky.c
$ ./a.out