0% found this document useful (0 votes)
7 views6 pages

Badrichit

The document discusses different data communication techniques including bit stuffing, character stuffing, cyclic redundancy check (CRC), and stop-and-wait protocol. It provides code examples for implementing each technique. Bit stuffing and character stuffing are used to ensure sufficient transition from 0 to 1 to allow synchronization. CRC is used to detect errors in transmission by generating and appending a checksum. Stop-and-wait protocol uses acknowledgments and timeouts to ensure reliable frame transmission.

Uploaded by

Chetan Bhajantri
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)
7 views6 pages

Badrichit

The document discusses different data communication techniques including bit stuffing, character stuffing, cyclic redundancy check (CRC), and stop-and-wait protocol. It provides code examples for implementing each technique. Bit stuffing and character stuffing are used to ensure sufficient transition from 0 to 1 to allow synchronization. CRC is used to detect errors in transmission by generating and appending a checksum. Stop-and-wait protocol uses acknowledgments and timeouts to ensure reliable frame transmission.

Uploaded by

Chetan Bhajantri
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
You are on page 1/ 6

BYTESTUFFING j++;

#include<stdio.h> cnt=0;
#include<math.h> }
#include<string.h> }
main() else
{ {
int i=0,j=0,k=0,cnt=0; dstmsg[k++]=stmsg[j];
char cnt=0;
msg[100],stmsg[100],dstmsg[100],flag[10]="01111 }
110"; }
clrscr(); dstmsg[k]='\0';
printf("\nEnter the bit stream:"); printf("\nDestuffed message is :
scanf("%s",msg); %s",dstmsg);
/stuffing/ getch();
for(i=0;flag[i]!='\0';i++) }
{ CHARACTER STUFFING
stmsg[i]=flag[i];
} #include<stdio.h>

#include<math.h>
for(j=0;msg[j]!='\0';j++)
{ #include<string.h>
if(msg[j]=='1')
{ main()
++cnt; {
stmsg[i++]=msg[j];
if(cnt==5) int i=0,j=0,k=0,l=0;
{
char
stmsg[i++]='0';
msg[100],stmsg[100],dstmsg[100],stx,etx,
cnt=0;
dle;
}
} printf("\nEnter the starting element of
else text:");
{
stmsg[i++]=msg[j]; stx=getche();
cnt=0;
printf("\nEnter the data link enable
}
text:");
}
for(k=0;flag[k]!='\0';k++) dle=getche();
{
stmsg[i++]=flag[k]; printf("\nEnter the ending element of
} text:");
etx=getche();
stmsg[i]='\0';
printf("\nStuffed bit stream is : printf("\nEnter the mesage:");
%s",stmsg);
/destuffing/ gets(msg);
cnt=0;
/*Stuffing*/
k=0;
for(j=8;j<i-8;j++) stmsg[j++]=dle;
{
if(stmsg[j]=='1') stmsg[j++]=stx;
{
for(i=0;msg[i]!='\0';i++)
++cnt;
dstmsg[k++]=stmsg[j]; {

if(cnt==5) stmsg[j++]=msg[i];
{
if(msg[i]==stx || msg[i]==etx || char
msg[i]==dle) msg[100],gen[100],app[100],azero[100];
{ int i,j,lmsg,lgen,lapp,lappr;
stmsg[j++]=msg[i]; printf("\nEnter the bit stream:");
} scanf("%s",msg);
} printf("\nEnter the generator:");
stmsg[j++]=dle; scanf("%s",gen);
stmsg[j++]=etx;
lgen=strlen(gen);
stmsg[j]='\0';
lmsg=strlen(msg);
printf("\nStuffed message is:");
for(i=0;i<(lgen-1);i++)
puts(stmsg);
azero[i]='0';
/*destuffing*/
azero[i]='\0';
for(k=2;k<j-2;k++)
strcat(msg,azero);
{
strcpy(app,msg);
dstmsg[l++]=stmsg[k];
lapp=strlen(app);
if(stmsg[k]==stx || stmsg[k]==etx
|| stmsg[k]==dle) for(i=0;i<=(lapp-lgen);i++)

{
{ if(app[i]=='1')
k++; {
} for(j=0;j<lgen;j++)
} {
dstmsg[l]='\0';

printf("\nDestuffed message is:"); if(app[i+j]==gen[j])

puts(dstmsg); app[i+j]='0';

getch(); else

} app[i+j]='1';

CRC }

#include<stdio.h> }

#include<math.h> }

#include<conio.h> printf("\nChecksum is : ");

#include<ctype.h> for(i=lmsg;i<(lmsg+lgen-1);i++)

#include<string.h> printf("%c",app[i]);

main() for(i=lmsg;i<(lmsg+lgen);i++)
{ msg[i]=app[i];
printf("\nChecksum appended bit stream else
is : %s",msg);
printf("\nReceived bit
printf("\nEnter the received bit stream:"); stream is free of error:");

scanf("%s",app); }

lappr=strlen(app); getch();

if(lappr!=lapp) }

printf("\nReceived bit stream is STOP AND WAIT PROTOCOL


in error");
#include<stdio.h>
else
#include<stdlib.h>
{
#include<windows.h>
for(i=0;i<=(lapp-lgen);i++)
#define RTT 4
{
#define TIMEOUT 4
if(app[i]=='1')
#define TOT_FRAMES 7
{
enum{NO,YES} ACK;

int main()
for(j=0;j<lgen;j++)
{
{
int wait_time,i=1;
if(app[i+j]==gen[j])
ACK==YES;
app[i+j]='0'; for(i=1 ;i<=TOT_FRAMES;i++)
else {

if(ACK==YES && i!=1)


app[i+j]='1';
{
}
printf("\n SENDER:ACK
} for Frame%d Received\n",i-1);
} }
j=0; printf("\nSENDER :Frames %d
sent waiting for ACK\n",i);
for(i=0;app[i]!='\0';i++)
ACK=NO;
{
wait_time=rand()%4+1;
if(app[i]=='1')
if(wait_time==TIMEOUT)
j++;
{
}
printf("SENDER: ACK not
if(j!=0)
received for frame%d ---> TIMEOUT Resending
printf("\nReceived bit Frane, ",i);
stream is errorneous:");
}
else for(j=1;j<=n;j++)

{ {

Sleep(RTT/2); if(i<j)

printf("\n RECEIVER : {
Frame%d received, ACK sent\n",i);
printf("\nCost
printf("----------------------- b/w nodes %d to %d is:",i,j);
--------");

ACK=YES; scanf("%d",&net[i][j]);

Sleep(RTT/2);
net[j][i]=net[i][j];
}
net[i][i]=0;
}
net[j][j]=0;
return 0;
}
}
}

}
SHORTEST PATH
printf("\nEnter the src: node:");
#include<stdio.h>
scanf("%d",&src);
#include<math.h>
printf("\nEnter the dst: node:");
#include<ctype.h>
scanf("%d",&dst);
#include<string.h>
for(i=1;i<=n;i++)
#include<conio.h>
{
main()
nodeinfo[i].pred=0;
{
nodeinfo[i].len=9999;
struct
nodeinfo[i].lab='t';
{
}
int pred,len;
k=dst;
char lab;
nodeinfo[dst].len=0;
}nodeinfo[20];
nodeinfo[dst].lab='p';
int n,i,j,k,min,inf=9999,src,dst;
do
int net[20][20];
{
printf("\nEnter the no: of nodes:");
for(i=1;i<=n;i++)
scanf("%d",&n);
{
printf("\nEnter the cost matrix:");
if( (nodeinfo[i].lab=='t')
for(i=1;i<=n;i++)
&& (net[k][i]!=0) )
{
{
if( BELLMAN FORD'S ALGORITHM
(net[k][i]+nodeinfo[k].len)<nodeinfo[i].len )
#include<stdio.h>
{
struct node

{
nodeinfo[i].len=nodeinfo[k].len+net[k][i];
unsigned dist[20];
nodeinfo[i].pred=k; unsigned from[20];
} }rt[10];
} int main()
} {
min=inf; int costmat[20][20];
for(i=1;i<=n;i++) int nodes,i,j,k,count=0;
{ printf("\nEnter the number of nodes : ");
if( (nodeinfo[i].lab=='t') scanf("%d",&nodes);//Enter the nodes
&& (nodeinfo[i].len<min) )
printf("\nEnter the cost matrix :\n");
{
for(i=0;i<nodes;i++)
k=i;
{

min=nodeinfo[i].len; for(j=0;j<nodes;j++)

} {

} scanf("%d",&costmat[i][j]);

nodeinfo[k].lab='p'; costmat[i][i]=0;

}while(k!=src); rt[i].dist[j]=costmat[i][j];//initialise the


distance equal to cost matrix
printf("\nCost b/w src: & dst: is :
%d",nodeinfo[src].len); rt[i].from[j]=j;

printf("\nShortest path b/w src: & dst: is : }


");
}
do
do
{
{
printf("\n%d to
count=0;
%d",k,nodeinfo[k].pred);
for(i=0;i<nodes;i++)//We choose arbitary
k=nodeinfo[k].pred;
vertex k and we calculate the direct distance from
}while(k!=dst); the node i to k using the cost matrix

getch(); //and add the distance from k to node j

} for(j=0;j<nodes;j++)

DISTANCE VECTOR ROUTING for(k=0;k<nodes;k++)


if(rt[i].dist[j]>costmat[i][k]+rt[k].dist[j])

{//We calculate the minimum distance

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<nodes;i++)

printf("\n\n For router %d\n",i+1);

for(j=0;j<nodes;j++)

printf("\t\nnode %d via %d Distance %d


",j+1,rt[i].from[j]+1,rt[i].dist[j]);

printf("\n\n");

getch();

You might also like