COMPUTER NETWORKS LAB
INDEX
S. No Name of the Experiment
DATA LINK LAYER
ERROR DETECTION AND ERROR CORRECTION CODES
1. Hamming Code
2. Cyclic Redundancy Check(CRC)
3. Stop and Wait Protocol
4. Go back n Sliding Window Protocol
5. Selective Repeat Sliding Window Protocol
NETWORK LAYER
ROUTING ALGORITHMS
6. Distance Vector Routing Algorithm
7. Dijkstra algorithm for shortest path routing.
CONGESTION CONTROL ALGORITHM
8. Leaky bucket Congestion control algorithm
Prepared by SVK SIR-9177972217, RICS & VICS Page 1
1. Write a C program to implement Hamming code for Error Detection and Error Correction.
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.
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 2
PROGRAM:
SOURCE CODE:
#include <stdio.h>
#include
<math.h> int
input[32];
int code[32];
int ham_calc(int,int);
void main()
int n,i,p_n = 0,c_l,j,k;
printf("Please enter the length of the Data Word: ");
scanf("%d",&n);
printf("Please enter the Data Word:\n"); for(i=0;i<n;i+
+)
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++)
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 3
{
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 4
if(i==((int)pow(2,k)-1))
code[i]=0;
k++;
else
code[i]=input[j]; j+
+;
for(i=0;i<p_n;i++)
int position = (int)pow(2,i);
int value =
ham_calc(position,c_l);
code[position-1]=value;
printf("\nThe calculated Code Word is: ");
for(i=0;i<c_l;i++)
printf("%d",code[i]);
printf("\n");
printf("Please enter the received Code Word:\n");
for(i=0;i<c_l;i++)
scanf("%d",&code[i]);
int error_pos = 0;
for(i=0;i<p_n;i++)
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 5
{
int position = (int)pow(2,i);
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 6
int value =
ham_calc(position,c_l); if(value !
= 0)
error_pos+=position;
if(error_pos == 1)
printf("The received Code Word is correct.\n");
else
printf("Error at bit position: %d\n",error_pos);
}
int ham_calc(int position,int c_l)
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
}
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 7
return 1;
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 8
OUTPUT:
$ vi hamming.c
$ cc hamming.c
$ ./a.out
Please enter the length of the Data Word: 8
Please enter the Data Word:
The calculated Code Word is: 011110100011
Please enter the received Code Word:
Error at bit position: 6
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 9
2. Write a C program to implement Cyclic Redundancy Check (CRC) code for Error Detection.
AIM: To write a C program to implement Cyclic Redundancy Check (CRC) code for Error
Detection.
ALGORITHM:
Step 1: Start the program
Step 2: Accept the number of bits in the input data stream (t).
Step 3: Set the number of bits in the divisor (g).
Step 4: Accept the input bits and the divisor bits.
Step 5: Append (g-3) ‘0’ bits to the end of the input data bits.
Step 6: Perform binary division
Step 7: Remove the appended ‘0’ bits and append the remainder bits to the
original data bits.
Step 8: Again perform binary division.
Step 9: If the reminder in zero then there is no error in the transmitted bits
else the received bits are in error.
Step 10: Stop the program
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 10
PROGRAM:
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()
for(c = 1;c < N; c++)
cs[c] = (( cs[c] == g[c])?'0':'1');
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("\nEnter data : ");
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 11
scanf("%s",t);
printf("\n ");
printf("\nGeneratng polynomial : %s",g);
a=strlen(t);
for(e=a;e<a+N-1;e++)
t[e]='0';
printf("\n ");
printf("\nModified data is : %s",t);
printf("\n ");
crc();
printf("\nChecksum is :
%s",cs); for(e=a;e<a+N-1;e++)
t[e]=cs[e-a];
printf("\n ");
printf("\nFinal codeword is : %s",t); printf("\
n ");
printf("\nTest error detection 0(yes) 1(no)? : ");
scanf("%d",&e);
if(e==0)
do{
printf("\nEnter the position where error is to be inserted : ");
scanf("%d",&e);
}while(e==0 || e>a+N-1);
t[e-1]=(t[e-1]=='0')?'1':'0';
printf("\n ");
printf("\nErroneous data : %s\n",t);
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 12
crc();
for(e=0;(e<N-1) && (cs[e]!='1');e+
+); if(e<N-1)
printf("\nError detected\n\n");
else
printf("\nNo error detected\n\n");
printf("\n \n");
return 0;
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 13
OUTPUT:
$ vi crc.c
$ cc crc.c
$ ./a.out
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 14
3. Write a C program to simulate and implement stop and wait protocol for noisy channel.
AIM: To write a C program to simulate and implement stop and wait protocol for noisy channel.
ALGORITHM:
Step 1: Start the process
Step 2: Declare all necessary variables.
Step 3: The number of frames to be sent is generated using rand() function.
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.
Step 7: After all frames are sent stop the process.
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 15
PROGRAM:
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;
printf("\n number of frames is %d",noframes);
getch();
while(noframes>0)
printf("\n sending frame %d",i);
srand(x1++);
x=rand()%10;
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 16
if(x%2==0)
for(x2=1;x2<2;x2++)
printf("waiting for %d seconds \n",x2);
sleep(x2);
printf("\n sending frame %d",i);
srand(x1++);
x=rand()%10;
printf("\n ack for frames
%d",j); noframes-=1;
i++;
j++;
printf("\n end of stop and wait protocol");
getch();
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 17
OUTPUT:
$ vi stopwait.c
$ cc stopwait.c
$ ./a.out
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 18
4. Write a C program to simulate and implement Go back n sliding window protocol
AIM: To write a C program to simulate and implement Go back n sliding window protocol.
ALGORITHM:
Step 1: Start the process
Step 2: Declare all necessary variables.
Step 3: Define the window Size by getting input from the user.
Step 4: Sender transmits all frames present in the window.
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.
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 19
PROGRAM:
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int main()
int windowsize,sent=0,ack,i;
clrscr();
printf("enter window size \n");
scanf("%d",&windowsize);
while(1)
for( i = 0; i < windowsize; i++)
printf("Frame %d has been transmitted.\n",sent);
sent++;
if(sent == windowsize)
break;
printf("\nPlease enter the last Acknowledgement
received.\n");
scanf("%d",&ack);
if(ack == windowsize-
1)
printf("\n All frames sent successfully");
getch();
break;
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 20
}
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 21
else
sent = ack;
return 0;
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 22
OUTPUT:
$ vi goback.c
$ cc goback.c
$ ./a.out
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 23
5. Write a C program to simulate and implement Selective Repeat Sliding Window Protocol
AIM: To write a C program to simulate and implement Selective Repeat Sliding Window Protocol.
ALGORITHM:
Step 1: Start the process
Step 2: Declare all necessary variables.
Step 3: Define the functions recvfrm(void), resend(void), selective(void).
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
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 24
PROGRAM:
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();
printf("\nSelective repeat\n\n ");
selective();
getch();
void selective()
sender();
recvfrm();
resend();
printf("\nAll packets sent successfully");
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 25
}
int sender()
int i;
printf("\nEnter the no. of packets to be sent:");
scanf("%d",&n);
for(i=0;i<n;i++)
printf("\nEnter data for
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')
printf("\nThe packet number %d is not received\n",r);
void resend()
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 26
{
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 27
printf("\nresending packet %d",r);
sleep(2);
frm[r].ack='y';
printf("\nThe received packet data is %d",frm[r].data);
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 28
OUTPUT:
$ vi selectiverepeat.c
$ cc selectiverepeat.c
$ ./a.out
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 29
6. Write a C program to simulate and implement Distance Vector Routing algorithm.
AIM: To write a C program to simulate and implement Distance Vector Routing algorithm.
ALGORITHM:
Step 1: Start the program
Step 2: Assign the distance of the node to zero.
Step 3: Accept the input distance matrix from which represents the distance between each node in the
network.
Step 4: Store the distance between nodes in a suitable variable.
Step 5: Calculate the minimum distance between two nodes by iterating.
Step 6: If the distance between two nodes is larger than the calculated alternate available path, replace the
existing distance with the calculated distance.
Step 7: Print the shortest path calculated.
Step 8: Stop the program.
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 30
PROGRAM:
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;
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;
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 31
}
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");
getch();
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 32
OUTPUT:
$ vi distancevector.c
$ cc distancevector.c
$ ./a.out
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 33
7. Write a C program to Simulate and implement Dijkstra algorithm for shortest path
routing.
AIM: To write a C program to simulate and implement Dijkstra algorithm for shortest path
routing.
ALGORITHM:
Step 1: Start the process.
Step 2: Include all the header files.
Step 3: Declare all necessary variables and function for shortest path().
Step 4: Get the value for no. of vertices.
Step 5: Get the value for no. of edges.
Step 6: Get the initial vertex for which distance is to be calculated.
Step 7: Calculate the shortest path using minimum distance.
Step 8: Display the output.
Step 9: Stop the process
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 34
PROGRAM:
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
int shortest(int ,int);
int
cost[10][10],dist[20],i,j,n,k,m,S[20],v,totcost,pat
h[20],p;
main()
int c;
cout <<"enter no of vertices";
cin >> n;
cout <<"enter no of edges";
cin >>m;
cout <<"\nenter\nEDGE Cost\n"; for(k=1;k<=m;k+
+)
cin >> i >> j >>c;
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;
cout <<"enter initial vertex";
cin >>v;
cout << v<<"\n";
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 35
shortest(v,n);
int shortest(int v,int n)
int min; for(i=1;i<=n;i+
+)
{ 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++)
if(dist[j]<min && S[j]!=1)
min=dist[j];
k=j;
if(cost[v][k]<=dist[k])
p=1;
path[++p]=k;
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 36
for(j=1;j<=p;j++)
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] && S[j]!=1)
dist[j]=dist[k]+cost[k][j];
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 37
OUTPUT:
$ vi dijsktras.c
$ cc dijsktras.c
$ ./a.out
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 38
8. Write a C program for congestion control using Leaky bucket algorithm
AIM: To write a C program for congestion control using Leaky bucket algorithm.
ALGORITHM:
Step 1: Start the process.
Step 2: Set the bucket size or the buffer size.
Step 3: Set the output rate.
Step 4: Transmit the packets such that there is no overflow.
Step 5: Repeat the process of transmission until all packets are transmitted. Reject packets where its size
is greater than the bucket size.
Step 6: Stop the process.
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 39
PROGRAM:
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#define NOF_PACKETS 10
int rand(int a)
int rn = (random() % 10) % a;
return rn == 0 ? 1 : rn;
int main()
int packet_sz[NOF_PACKETS], i, clk, b_size, o_rate, p_sz_rm=0, p_sz, p_time, op;
for(i = 0; i<NOF_PACKETS; ++i)
packet_sz[i] = rand(6) * 10;
for(i = 0; i<NOF_PACKETS; +
+i)
printf("\npacket[%d]:%d bytes\t", i, packet_sz[i]);
printf("\nEnter the Output rate:");
scanf("%d", &o_rate);
printf("Enter the Bucket Size:");
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 40
scanf("%d", &b_size);
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 41
for(i = 0; i<NOF_PACKETS; ++i)
if( (packet_sz[i] + p_sz_rm) > b_size)
if(packet_sz[i] > b_size)/*compare the packet siz with bucket size*/
printf("\n\nIncoming packet size (%dbytes) is Greater than bucket capacity (%dbytes)-
PACKET REJECTED", packet_sz[i], b_size);
else
printf("\n\nBucket capacity exceeded-PACKETS REJECTED!!");
else
p_sz_rm += packet_sz[i];
printf("\n\nIncoming Packet size: %d", packet_sz[i]);
printf("\nBytes remaining to Transmit: %d", p_sz_rm);
p_time = rand(4) * 10;
printf("\nTime left for transmission: %d units", p_time);
for(clk = 10; clk <= p_time; clk += 10)
sleep(1);
if(p_sz_rm)
if(p_sz_rm <= o_rate)/*packet size remaining comparing with output rate*/
op = p_sz_rm, p_sz_rm = 0;
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 42
else
op = o_rate, p_sz_rm -= o_rate; printf("\
nPacket of size %d Transmitted", op);
printf("----Bytes Remaining to Transmit: %d", p_sz_rm);
else
printf("\nTime left for transmission: %d units", p_time-clk);
printf("\nNo packets to transmit!!");
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 43
OUTPUT:
$ vi leaky.c
$ cc leaky.c
$ ./a.out
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 44
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 45
Prepared by N. Sumalatha, Asst. Professor, KIST, Kautur, Nellore, A.P Page 46