1. Networks Laboratory – 10CSL77 PART - B
Implement the following in C/C++:
1. Write a program for error detecting code using CRC CCIT(16 bits).
#include<stdio.h>
#include<string.h>
#define N strlen(g)
char t[128],cs[128],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()
{
int flag=0;
system("clear");
printf("Enter polynomial:n");
scanf("%s",&t);
printf("Generated divisor is :%sn",g);
a=strlen(t);
for(e=a;e<a+N-1;e++)
t[e]='0';
printf("Modified polynomial is %sn",t);
crc( );
printf("Checksum is :%sn",cs);
for(e=a;e<a+N-1;e++)
t[e]=cs[e-a];
printf("Final codeword is %sn",t);
printf("Test error detection?n1(yes) 0(no):n");
scanf("%d",&flag);
if(flag==0)
return;
if(flag==1)
{
printf("Enter position where error is to be inserted:n ");
scanf("%d",&e);
if(e<a+N-1){
t[e]=(t[e]=='0')?'1':'0';
1 | P a g e A n a n d a K u m a r H N
2. Networks Laboratory – 10CSL77 PART - B
printf("err data : %sn",t);
}
else
printf("Position where error should be created is above the length of Codeword...Hencen");
}
crc();
for(e=0;(e<N-1)&&(cs[e]!='1');e++);
if(e<N-1)
printf("Error detectedn");
else
printf("No error detected n");
return;
}
OUTPUT:
2. Write a program for “distance vector algorithm” to find suitable path for
transmission.
#include<stdio.h>
struct node
{
int dist[20];
int from[20];
}rt[10];
int main()
{
int distance_matrix[20][20];
int n,i,j,k,count=0,src,dest;
printf("nEnter the number of nodes : ");
scanf("%d",&n);
printf("nEnter the cost/distance matrix :n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
scanf("%d",&distance_matrix[i][j]);
distance_matrix[i][i]=0;
rt[i].dist[j]=distance_matrix[i][j];
rt[i].from[j]=j;
}
do
{
count=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
2 | P a g e A n a n d a K u m a r H N
3. Networks Laboratory – 10CSL77 PART - B
for(k=0;k<n;k++)
if((rt[i].dist[j])>(distance_matrix[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---------------------------------");
printf("nRouting table for router %d :nDesttNextHopttDistn",i+1);
printf("----------------------------------n");
for(j=0;j<n;j++)
printf("%dt%dtt%dn",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
printf("nEnter source and destination : ");
scanf("%d%d",&src,&dest);
src--; dest--;
if(rt[src].dist[dest]!=999)
printf("Shortest path : n Via router : %dn Shortest distance : %d n",rt[src].from[dest]+1,rt[src].dist[dest]);
else
printf("no connection exists between %d----->%d",src+1,dest+1);
return 0;
}
6. Write a program for congestion control using leaky bucket algorithm.
#include<stdio.h>
#include<math.h>
int random(int a)
{ int rn=(rand()%10)%a;
return rn==0?1:rn;
}
int main()
{
int packet_sz[5],i,clk,b_size,o_rate,p_sz_rm=0,p_sz,p_time;
for(i=0;i<5;++i)
packet_sz[i]=random(6)*10;
for(i=0;i<5;++i)
printf("packet[%d]:%d bytesn",i,packet_sz[i]);
printf("nEnter the Output rate:");
scanf("%d",&o_rate);
printf("Enter the Bucket Size:");
scanf("%d",&b_size);
for(i=0; i<5; ++i)
{
if((packet_sz[i]+p_sz_rm) > b_size)
if(packet_sz[i] > b_size)
printf("nnIncomming packet size (%d) is Greater than bucket capacity-PACKET REJECTED",packet_sz[i]);
else
3 | P a g e A n a n d a K u m a r H N
4. Networks Laboratory – 10CSL77 PART - B
printf("nnPacket[%d] size %d + Packet remaining %d exceeds Bucket capacity - PACKET
REJECTED!!",i+1,packet_sz[i],p_sz_rm);
else
{
p_sz_rm+=packet_sz[i];
printf("nnIncomming Packet size: %d",packet_sz[i]);
printf("nBytes remaining to Transmit: %d",p_sz_rm);
p_time = random(4)*10;
printf("nTime left for transmission: %d units",p_time);
for(clk=10; clk<=p_time; clk+=10)
{
if(p_sz_rm)
{
if(p_sz_rm <= o_rate)
{
printf("n Packet of size %d Transmitted",p_sz_rm);
p_sz_rm=0;
}
else
{
printf("n Packet of size %d Transmitted",o_rate);
p_sz_rm -= o_rate;
printf("nBytes Remaining after Transmission:%d",p_sz_rm);
}
}
else
printf("n No packets to transmit!!");
printf(" Time Left:%d",p_time-clk);
}
}
}
}
4 | P a g e A n a n d a K u m a r H N