0% found this document useful (0 votes)
5 views11 pages

Bit Stuffing and Networking Algorithms

The document contains several C programs that demonstrate various networking and data transmission algorithms, including Bit Stuffing, Character Stuffing, Distance Vector Algorithm, Dijkstra's Algorithm, CRC CCITT, Stop & Wait and Sliding Window Protocol, and Leaky Bucket Algorithm. Each program includes user input for parameters and outputs the results of the respective algorithm. The content is technical and aimed at illustrating concepts in data communication and networking.

Uploaded by

KOUSHIK B S
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views11 pages

Bit Stuffing and Networking Algorithms

The document contains several C programs that demonstrate various networking and data transmission algorithms, including Bit Stuffing, Character Stuffing, Distance Vector Algorithm, Dijkstra's Algorithm, CRC CCITT, Stop & Wait and Sliding Window Protocol, and Leaky Bucket Algorithm. Each program includes user input for parameters and outputs the results of the respective algorithm. The content is technical and aimed at illustrating concepts in data communication and networking.

Uploaded by

KOUSHIK B S
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

BIT STUFFING

#include<string.h>
#include<stdio.h>
int main()
{
char a[20],fs[50]="",t[6],r[5];
int i,j,p=0,q=0;
printf("enter bit string : ");
scanf("%s",a);
strcat(fs,"01111110");
if(strlen(a)<5)
{
strcat(fs,a);
}
else
{
for(i=0;i<strlen(a)-4;i++)
{
for(j=i;j<i+5;j++)
{
t[p++]=a[j];
}
t[p]='\0';
if(strcmp(t,"11111")==0)
{
strcat(fs,"111110");
i=j-1;
}
else
{
r[0]=a[i];
r[1]='\0';
strcat(fs,r);
}
p=0;
}
for(q=i;q<strlen(a);q++)
{
t[p++]=a[q];
}
t[p]='\0';
strcat(fs,t);
}
strcat(fs,"01111110");
printf("After stuffing : %s",fs);
}

2. CHARACTER STUFFING

#include<stdio.h>
#include<string.h>
int main()
{
char a[30],fs[50]="",t[3],sd,ed,x[3],s[3],d[3],y[3];
int i,j,p=0,q=0;
printf("Enter characters to be stuffed : ");
scanf("%s",a);
printf("\nEnter a character that represents starting delimiter : ");
scanf(" %c",&sd);
printf("\nEnter a character that represents ending delimiter : ");
scanf(" %c",&ed);
x[0]=s[0]=s[1]=sd;
x[1]=s[2]='\0';
y[0]=d[0]=d[1]=ed;
d[2]=y[1]='\0';
strcat(fs,x);
for(i=0;i<strlen(a);i++)
{
t[0]=a[i];
t[1]='\0';
if(t[0]==sd)
strcat(fs,s);
else
if(t[0]==ed)
strcat(fs,d);
else
strcat(fs,t);
}
strcat(fs,y);
printf("\nAfter stuffing : %s",fs);
}

3. DISTANCE VECTOR ALGORITHM

#include<stdio.h>
#include<stdlib.h>
int main()
{
int d[10][10], t[10][10], a[10][10], i, j, n, k, count;
printf("Enter the number of nodes\n");
scanf("%d", &n);
printf("Enter the initial cost matrix\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
scanf("%d", &d[i][j]);
d[i][i] = 0;
t[i][j] = d[i][j];
a[i][j] = j;
}
}
do
{
count = 0;
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
for(k=1; k<=n; k++)
{
if(t[i][j]>(d[i][k]+t[k][j]))
{
t[i][j] = (t[i][k] + t[k][j]);
a[i][j] = k;
count++;
}
}
}
}
}while(count!=0);
for(i=1; i<=n; i++)
{
printf("\n\n For router %d\n", i);
for(j=1; j<=n; j++)
{
printf("\t\nto node %d, via %d, Distance -> %d ",j, a[i][j], t[i][j]);
}
}
printf("\n\n");
return 0;
}
4. DIJKSTRA’S ALGORITHM

#include<stdio.h>

#include<conio.h>

void dijkstra(int n, int v, int cost[10][10],int dist[10])


{
int count, u, i, w, visited[10], min;
for(i=0;i<n;i++)
{
visited[i]=0;
dist[i]=cost[v][i];
}
visited[v]=1;
dist[v]=1;
count=2;
while(count<=n)
{
min=999;
for(w=0;w<n;w++)
if((dist[w]<min) && (visited[w]!=1))
{
min=dist[w];
u=w;
}
visited[u]=1;
count++;
for(w=0;w<n;w++)
if((dist[u]+cost[u][w]<dist[w]) && (visited[w]!=1))
dist[w]=dist[u]+cost[u][w];
}
}
void main()
{
int n, v, cost[10][10], dist[10], i, j;
printf("Enter number of vertices:");
scanf("%d",&n);
printf("\nEnter cost matrix (for infinity, enter 999):\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&cost[i][j]);
printf("\nEnter source vertex:");
scanf("%d",&v);
dijkstra(n,v,cost,dist);
printf("\nShortest path from \n");
for(i=0;i<n;i++)
if(i!=v)
printf("\n%d -> %d = %d", v, i, dist[i]);
}

5. CRC CCITT

#include<stdio.h>

#include<string.h>
#define N strlen(g)
char t[128], cs[128], g[]="10001000000100001";
int a, e, c, b;
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);
}
void main()
{
printf("\nEnter data t: ");
scanf("%s", t);
printf("\nPredefined Generator Polynomial is : %s", g);
a = strlen(t);
for(e=a; e<a+N-1; e++)
t[e] = '0';
printf("\nModified t[u] is : %s", t);
crc();
printf("\nChecksum is : %s", cs);
for(e=a; e<a+N-1; e++)
t[e] = cs[e-a];
printf("\nFinal Codeword is : %s", t);
printf("\n\nTest Error detection 1(yes) 0(no) ? : ");
scanf("%d", &b);
if(b==1)
{
printf("\nEnter position where error is to inserted : ");
scanf("%d", &e);
}
crc();
t[e] = (t[e]=='0')?'1':'0';
printf("Errorneous data : %s\n", t);
for(e=0; (e<N-1)&&(cs[e]!='1'); e++);
if(e<N-1)
printf("Error detected.");
else
printf("No Error Detected.");
}
6. STOP & WAIT AND SLIDING WINDOW PROTOCOL

#include<stdio.h>

void sender(void);
void reciever(void);
int frame=0,ack=0,frame_no=1;
int lost_frame=1,lost_ack=2;
int seq_no=0,ack_no=0,timer=0,ready=1,max_frames=3;
void main()
{
// printf("Enter [Link] frames\n");
// scanf("%d",&max_frames);
while(frame_no <= max_frames)
{
sender();
sleep(2);
if(frame_no==lost_frame)
{
frame=0;
lost_frame=0;
printf("\nFrame%d has been lost\n",frame_no);
}
reciever();
sleep(2);
if(frame_no==lost_ack && ack==1)
{
ack=0;
lost_ack=0;
printf("\nAck for frame%d has been lost\n",frame_no);
}
if(ack==0)
{
sleep(4);
printf("\nTimer has been expired\n");
timer=1; //time is expired
}
}
}
void sender(void)
{
if(ready)
{
printf("\nFrame%d is sent with seq no %d\n",frame_no,seq_no);
frame=1;
ready=0;
timer=0; // indicates timer is started
}
else
{
if(timer==1) // indicates timer is expired
{
printf("\nFrame%d is resent with seq no %d\n",frame_no,seq_no);
frame=1;
timer=0; // indicates timer is started
}
if(ack==1)
{
printf("\nAck. for frame%d is recieved\n",frame_no);
seq_no=!seq_no;
ack=0;
frame_no=frame_no+1;
if(frame_no<=max_frames)
{
printf("\nFrame%d is sent with seq no %d\n",frame_no,seq_no);
frame=1;
ready=0;
timer=0; // indicates timer is started
}
}
}
}
void reciever(void)
{
if(frame==1)
{
frame=0; // frame has reached reciever
if(ack_no==seq_no)
{
ack_no=!ack_no;
printf("\nFrame%d is recieved and ack with [Link]. %d sent\n",frame_no,ack_no);
ack=1;
}
else
{
printf("\nFrame%d is duplicate and discarded. Ack with [Link]. %d has been
sent\n",frame_no,ack_no);
ack=1;
}
}
}

7. LEAKY BUCKET ALGORITHM

#include<stdio.h>

#include<strings.h>
#include<stdio.h>
int min(int x,int y)
{
if(x<y)
return x;
else
return y;
}
int main()
{
int drop=0,mini,nsec,cap,count=0,i,inp[25],process;
printf("Enter The Bucket Size\n");
scanf("%d",&cap);
printf("Enter The Operation Rate\n");
scanf("%d",&process);
printf("Enter The No. Of Seconds You Want To Stimulate\n");
scanf("%d",&nsec);
for(i=0;i<nsec;i++)
{
printf("Enter The Size Of The Packet Entering At %d sec\n",i+1);
scanf("%d",&inp[i]);
}
printf("\nSecond|Packet Recieved|Packet Sent|Packet Left|Packet Dropped|\n");
printf(" ------------------------------------------------------------- \n");
for(i=0;i<nsec;i++)
{
count+=inp[i];
if(count>cap)
{
drop=count-cap;
count=cap;
}
printf("%d",i+1);
printf("\t%d",inp[i]);
mini=min(count,process);
printf("\t\t%d",mini);
count=count-mini;
printf("\t\t%d",count);
printf("\t\t%d\n",drop);
drop=0;
}
for(;count!=0;i++)
{
if(count>cap)
{
drop=count-cap;
count=cap;
}
printf("%d",i+1);
printf("\t0");
mini=min(count,process);
printf("\t\t%d",mini);
count=count-mini;
printf("\t\t%d",count);
printf("\t\t%d\n",drop);
}
}

You might also like