Codes

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

//BIT STUFFING #include<string.

h>
main()
#include<stdio.h> {
#include<math.h> int i=0,j=0,k=0,l=0;
#include<string.h> char msg[100],stmsg[100],dstmsg[100],stx,etx,dle;
main() clrscr();
{ printf("\nEnter the starting element of text:");
int i=0,j=0,k=0,cnt=0; stx=getche();
char ,stmsg[100],dstmsg[100],flag[10]="01111110"; printf("\nEnter the data link enable text:");
clrscr(); dle=getche();
printf("\nEnter the bit stream:"); printf("\nEnter the ending element of text:");
scanf("%s",msg); etx=getche();
/*stuffing*/ printf("\nEnter the mesage:");
for(i=0;flag[i]!='\0';i++) gets(msg);
{
stmsg[i]=flag[i]; /*Stuffing*/
} stmsg[j++]=dle;
for(j=0;msg[j]!='\0';j++) stmsg[j++]=stx;
{ for(i=0;msg[i]!='\0';i++)
if(msg[j]=='1') {
{ stmsg[j++]=msg[i];
++cnt; if(msg[i]==stx || msg[i]==etx || msg[i]==dle)
+]=msg[j]; {
if(cnt==5) stmsg[j++]=msg[i];
{ }
[i++]='0'; }
cnt=0; stmsg[j++]=dle;
} stmsg[j++]=etx;
} stmsg[j]='\0';
else printf("\nStuffed message is:");
{ puts(stmsg);
stmsg[i++]=msg[j];
cnt=0; /*destuffing*/
} for(k=2;k<j-2;k++)
} {
for(k=0;flag[k]!='\0';k++) dstmsg[l++]=stmsg[k];
{ if(stmsg[k]==stx || stmsg[k]==etx ||
stmsg[i++]=flag[k]; stmsg[k]==dle)
} {
stmsg[i]='\0'; k++;
printf("\nStuffed bit stream is : %s",stmsg); }
}
/*destuffing*/ dstmsg[l]='\0';
cnt=0; printf("\nDestuffed message is:");
k=0; puts(dstmsg);
for(j=8;j<i-8;j++) getch();
{ }
if(stmsg[j]=='1')
{ output:
++cnt;
dstmsg[k++]=stmsg[j]; Enter the starting element of text:a
if(cnt==5) Enter the data link enable text : b
{ Enter the ending element of text : c
j++; Enter the message : welcome
cnt=0;
} Stuffed message is: bawelccomebc
}
else Destuffed message is: welcome
{
dstmsg[k++]=stmsg[j];
cnt=0;
}
}
dstmsg[k]='\0';
printf("\nDestuffed message is : %s",dstmsg);
getch();
}

output:

Enter the bit stream:01101111110


Stuffed bit stream is : 0111111001101111101001111110
Destuffed message is : 01101111110 //Leaky Bucket Algo
//CHAR STUFFING
#include<stdio.h>
#include<stdio.h> #include<stdlib.h>
#include<math.h> int bucket_size;
void bucket_input (int pkt_sz, int op_rt)
{ struct NodeInfo nodeinfo[20];
if(pkt_sz>bucket_size)
printf("\n\n Bucket Overflow\n"); for (i = 1; i <= n; i++)
else nodeinfo[i] = (struct NodeInfo){0, 9999, 't'};
{
sleep(1); k = dst;
while(pkt_sz>op_rt) nodeinfo[dst] = (struct NodeInfo){0, 0, 'p'};
{
printf("\n %d bytes outputted",op_rt); do {
pkt_sz = pkt_sz - op_rt; for (i = 1; i <= n; i++)
sleep(1); if ((nodeinfo[i].lab == 't') && (net[k][i] != 0) && ((net[k][i] +
} nodeinfo[k].len) < nodeinfo[i].len))
if(pkt_sz>0) nodeinfo[i] = (struct NodeInfo){k, nodeinfo[k].len + net[k]
printf("\n Last %d bytes sent\n", pkt_sz); [i], 't'};
printf("\n Bucket output Successful \n");
} min = inf;
} for (i = 1; i <= n; i++)
int main() if ((nodeinfo[i].lab == 't') && (nodeinfo[i].len < min))
{ k = i, min = nodeinfo[i].len;
int i, op_rate, packet_size;
printf("\n Enter Bucket Size: "); nodeinfo[k].lab = 'p';
scanf("%d",&bucket_size); } while (k != src);
printf("\n Enter Output rate: ");
scanf("%d",&op_rate); printf("\nCost between source and destination is: %d",
for(i=1; i<=5; i++) nodeinfo[src].len);
{ printf("\nShortest path between source and destination is: ");
sleep(1);
packet_size = rand()%1000; int printNode = src; // introduce a separate variable for printing
printf("\n packet number [%d] \tPacket size=%d",i,packet_size);
bucket_input(packet_size, op_rate); do {
} printf("\n%d to %d", printNode, nodeinfo[printNode].pred);
return 0; printNode = nodeinfo[printNode].pred; // update the printing
} variable
} while (printNode != dst);
Output
Enter output rate 10 getch();
New packet size=41 return 0;
}

output:
//DIJIKSTRAS ALGO
Enter the no: of nodes:4
#include <stdio.h> Enter the cost matrix:
#include <conio.h>
Cost b/w nodes 1 to 2 is:1
struct NodeInfo {
int pred, len; Cost b/w nodes 1 to 3 is:2
char lab;
}; Cost b/w nodes 1 to 4 is:3

int main() { Cost b/w nodes 2 to 3 is:5


int n, i, j, k, min, inf = 9999, src, dst;
int net[20][20]; Cost b/w nodes 2 to 4 is:1

Cost b/w nodes 3 to 4 is:4

printf("Enter the number of nodes: "); Enter the src: node:1


scanf("%d", &n);
Enter the dst: node:4
printf("Enter the cost matrix:\n");
for (i = 1; i <= n; i++) { Cost b/w src: & dst: is : 2
for (j = 1; j <= n; j++) {
if (i < j) { Shortest path b/w src: & dst: is :1 to 2 2 to 4
printf("Cost between nodes %d to %d is: ", i, j);
scanf("%d", &net[i][j]);
net[j][i] = net[i][j];
}
} STOP AND WAIT PROTOCOL
}
#include <stdio.h>
printf("Enter the source node: "); #include <stdlib.h>
scanf("%d", &src); #define RTT 4
#define TIMEOUT 4
printf("Enter the destination node: "); #define TOT_FRAMES 7
scanf("%d", &dst);
enum {NO,YES} ACK; RTT/2 printf("RECEIVER:Frames Received, ACK Sent\n");
printf("-------------------------------------------\n");
int main() sleep(RTT/2); // wait for RTT/2
{ printf("SENDER:ACK received, sending next frames\n");
int wait_time,i=1; }
}
ACK=YES;
if(f%window_size!=0) // send the left over frames
for(;i<=TOT_FRAMES;) {
{ printf("\nSENDER:waiting for ACK...\n");
if (ACK==YES && i!=1) sleep(RTT/2);
{ // wait for RTT/2
printf("\nSENDER: ACK for Frame %d Received.\n",i-1); printf("\nRECEIVER:Frames Received, ACK Sent\n");
} printf("-------------------------------------------------\n");
sleep(RTT/2);
printf("\nSENDER: Frame %d sent, Waiting for ACK...\n",i); // wait for RTT/2
printf("SENDER:ACK received.");
ACK=NO; // after sending i_th frame set ACK=NO }
return 0;
wait_time= rand() % 4+1;

if(wait_time==TIMEOUT) Distance vector


{
printf("SENDER: ACK not received for Frame %d=>TIMEOUT Resending #include <stdio.h>
Frame...",i); int d[10][10], via[10][10];
} int main() {
else int i, j, k, n, g[10][10];
{ printf("\nEnter the no of nodes: ");
sleep(RTT/2); // wait for RTT/2 scanf("%d", &n);
printf("\nRECEIVER: Frame %d received,ACK sent\n",i); for (i = 0; i < n; i++) {
printf("--------------------------------------------"); printf("Enter the record for route %c\n", i + 97);
ACK=YES;// set ACK=YES for (j = 0; j < n; j++) {
sleep(RTT/2); // wait for RTT/2 printf("(%c : %c) :: ", i + 97, j + 97);
i++; // select next frame scanf("%d", &g[i][j]);
} if (g[i][j] != 999) {
} d[i][j] = 1;
return 0; }
} }
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
SLIDING WINDOW via[i][j] = i;
}
#include <stdio.h> }
#include <stdlib.h> for (k = 0; k < n; k++) {
#define RTT 5 for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
int main() if (d[i][k] == 1 && d[k][j] == 1 && g[i][k] + g[k][j] < g[i][j]) {
{ g[i][j] = g[i][k] + g[k][j];
int window_size,i,f,frames[50]; via[i][j] = k;
}
printf("Enter window size: "); }
scanf("%d",&window_size); // read window size }
}
printf("\nEnter number of frames to transmit: "); for (i = 0; i < n; i++) {
scanf("%d",&f); // read no. of frames printf("cost of route %c:\n", i + 97);
for (j = 0; j < n; j++) {
printf("\nEnter %d frames: ",f); printf("%c : %d via %c\n", j + 97, g[i][j], via[i][j] + 97);
}
for(i=1;i<=f;i++) }
scanf("%d",&frames[i]); //read frame values return 0;
}
printf("\nAfter sending %d frames at each stage sender waits for ACK }
",window_size);
printf("\nSending frames in the following manner....\n\n");

for(i=1;i<=f;i++)
{
if(i%window_size!=0) // collect the frames to fit in window //CRC
{
printf(" %d",frames[i]); #include<stdio.h>
} #include<math.h>
else #include<conio.h>
{ #include<ctype.h>
printf(" %d\n",frames[i]); // send the frames #include<string.h>
printf("SENDER:waiting for ACK...\n\n"); sleep(RTT/2); // wait for main()
char msg[100],gen[100],app[100],azero[100]; Checksum appended bit stream is : 01111110110111
int i,j,lmsg,lgen,lapp,lappr; Enter the received bit stream:01111110110111
clrscr();
printf("\nEnter the bit stream:"); Received bit stream is free of error:
scanf("%s",msg);
printf("\nEnter the generator:");
scanf("%s",gen);
lgen=strlen(gen);
lmsg=strlen(msg);
for(i=0;i<(lgen-1);i++)
azero[i]='0';
azero[i]='\0';
strcat(msg,azero);
strcpy(app,msg);
lapp=strlen(app);
for(i=0;i<=(lapp-lgen);i++)
{
if(app[i]=='1')
{
for(j=0;j<lgen;j++)
{
if(app[i+j]==gen[j])
app[i+j]='0';
else
app[i+j]='1';
}
}
}
printf("\nChecksum is : ");
for(i=lmsg;i<(lmsg+lgen-1);i++)
printf("%c",app[i]);
for(i=lmsg;i<(lmsg+lgen);i++)
msg[i]=app[i];
printf("\nChecksum appended bit stream is : %s",msg);
printf("\nEnter the received bit stream:");
scanf("%s",app);
lappr=strlen(app);
if(lappr!=lapp)
printf("\nReceived bit stream is in error");
else
{
for(i=0;i<=(lapp-lgen);i++)
{
if(app[i]=='1')
{
for(j=0;j<lgen;j++)
{
if(app[i+j]==gen[j])
app[i+j]='0';
else
app[i+j]='1';
}
}
}
j=0;
for(i=0;app[i]!='\0';i++)
{
if(app[i]=='1')
j++;
}
if(j!=0)
printf("\nReceived bt stream is
errorneous:");
else
printf("\nReceived bit stream is
free of error:");
}
getch();
}

output:

Enter the bit stream:0111111011

Enter the generator:10011

Checksum is : 0111

You might also like