You are on page 1of 10

CCN PART-B Programs

Bit Stuffing

1. #include <iostream>
2. #include<conio.h>
3. #include<stdio.h>
4. using namespace std;
5. int main()
6. {
7. char ch,a[20];
8. int count=0, i=0,j=0;
9. cout << " enter data" << endl;
10. while((ch=getche())!='\r')
11. {
12. if(ch=='1')
13. ++count;
14. else
15. count=0;
16. a[i++]=ch;
17. if(count==5)
18. {
19. a[i++]='0';
20. count=0;
21. }
22. }
23. cout<< "\n stuffed data is:";
24. for(j=0;j<i;++j)
25. {
26. cout<<a[j];
27. }
28. getch();
29. }

Character Stuffing Program


#include <iostream>
#include <stdio.h>
#include<string.h>
using namespace std;
int main()
{
int i=0,j=0,n,pos;
char a[20],b[50],ch;
cout<<"enter the string \n";
cin>>a;
n=strlen(a);
b[0]='d';
b[1]='l';
b[2]='e';
b[3]='s';
b[4]='t';
b[5]='x';
j=6;
while(i<n)
{
if(a[i]=='d'&&a[i+1]=='l'&&a[i+2]=='e')
{
b[j]='d';
b[j+1]='1';
b[j+2]='e';
j=j+3;
}
b[j]=a[i];
i++;
j++;
}
b[j]='d';
b[j+1]='l';
b[j+2]='e';
b[j+3]='e';
b[j+4]='t';
b[j+5]='x';
b[j+6]='\0';
cout<<"\n frame after stuffing: \n";
cout<<b;
}

CRC Program

#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<string.h>
using namespace std;
char t[28],cs[28],g[30];
int a,i,j,N;
void axor()
{
for(j=1; j<N; j++)
cs[j] = ((cs[j]==g[j])?'0':'1');
}
void crc()
{
for(i=0;i<N;i++)
cs[i]=t[i];
do
{
if(cs[0]=='1')
axor();
for(j=0;j<N-1;j++)
cs[j]=cs[j+1];
cs[j]=t[i++];
}while(i<=a+N-1);
}
int main()
{
cout<<"\n message to be send is :";
cin>>t;
cout<<"\n-------------------";
cout<<"\nGenerating polynomial : ";
scanf("%s",&g);
a=strlen(t);
N=strlen(g);
for(i=a;i<(a+N-1);i++)
t[i]='0';
cout<<"\n after appending zero's to message :";
cout<<t;
cout<<"\n-------------------------------------";
crc();
cout<<"\n checksum is :"<<cs;
for(i=a;i<(a+N-1);i++)
t[i]=cs[i-a];
cout<<"\n-----------------------------------";
cout<<"\n final codeword(message + checksum)to be transmitted is :"<<t;
cout<<"\n-----------------------------";
cout<<"\n Enter received message";
cin>>t;
crc();
cout<<"\n REMAINDER : %s"<<cs;
for(i=0;(i<N-1)&&(cs[i]!='1');i++);
if(i<N-1)
cout<<"\n\n error detected\n\n";
else
cout<<"\n\n No error detected\n\n";
getch();
}

Stop and wait protocol Program

#include <iostream>
#include <time.h>
#include <stdio.h>
#include <windows.h>
#include <conio.h>
#define RTT 4
#define TIMEOUT 4
#define TOT_FRAMES 7
enum {NO,YES} ACK;
using namespace std;
int main()
{
int wait_time,i=1;
ACK=YES;
for(;i<=TOT_FRAMES;)
{
if (ACK==YES && i!=1)
{
cout<<"\n SENDER: ACK for Frame " << i-1 << " Received \n ";
}
cout<<"\n SENDER: Frame "<< i << " sent, Waiting for ACK...\n";
ACK=NO;
wait_time= rand() % 4+1;
if (wait_time==TIMEOUT)
{
cout<<"\n SENDER: ACK not received for Frame "<< i <<" => TIMEOUT
Resending Frame... \n ";
}
else
{
Sleep(RTT/2);
cout<< "\n RECEIVER: Frame " << i <<" received, ACK sent \n ";
cout<< "\n --------------------------------------------\n ";
ACK=YES;
Sleep(RTT/2);
i++;
}
}
return 0;
}

Sliding window protocol

#include <iostream>
#include <stdio.h>
#include<conio.h>
#include <time.h>
#include <windows.h>
#define RTT 5
using namespace std;
int main()
{
int window_size,i,f,frames[50];
cout<<" \n Enter window size: ";
cin>>window_size;
cout<<"\n Enter number of frames to transmit: ";
cin>>f;
cout<<"\n Enter " << f << " frames \n";
for(i=1;i<=f;i++)
{
cin>>frames[i];
}
cout<<"\n after sending " << window_size << " frames at each stage sender
waits for ACK ";
cout<<"\n sending frames in the following manner .......\n\n";
for (i=1;i<=f;i++)
{
if(i%window_size!=0)
{
cout<<"\n"<<frames[i];
}
else
{
cout<<"\n"<<frames[i];
cout<<"\n SENDER: waiting for ACK.....\n\n";
Sleep(RTT/2);
cout<<"\n Reciver: frames Received, ACK sent \n";
cout<<"\n -------------------------------\n";
Sleep(RTT/2);
cout<<"\n SENDER: ACK received, sending next frames \n";
}
}
if(f%window_size!=0)
{
cout<<"\n SENDER: waiting for ACK........ \n";
Sleep(RTT/2);
cout<<"\n Receiver: Frames received, ACK sent\n";
cout<<"\n ---------------------------------------\n";
Sleep(RTT/2);
cout<<"\n SENDER: ACK received ";
}
getch();
}

Leaky Bucket Algorithm: Program

#include <iostream>
#include <stdio.h>
#include <time.h>
#include <windows.h>
#define bucketsize 1000
#define n 5
using namespace std;

void bucketoutput (int *bucket,int op)


{
if(*bucket > 0 && *bucket > op)
{
*bucket= *bucket-op;
//cout<<"\n"<< op <<" - outputed, remaining is " << *bucket;
}
else if( *bucket > 0)
{
cout<< "\n remaining data output = "<< *bucket;
*bucket=0;
}
}
int main()
{
int op,newpack,oldpack=0,wt,i,j,bucket=0;
cout<< "enter output rate: ";
cin>>op;
for(i=1;i<=n;i++)
{
newpack=rand()%500;
//cout<< "\n\n new packet size = " <<newpack;
newpack= oldpack+newpack;
wt=rand()%5;
if(newpack<bucketsize)
bucket=newpack;
else
{
cout<< "\n" << newpack << " = the newpacket and old pack is greater than
bucketsize reject";
bucket=oldpack;
}
cout<< "\n the data in bucket = "<< bucket;
cout<< "\n the next packet will arrive after = "<<wt<< "sec";
for(j=0;j<wt;j++)
{
bucketoutput(&bucket,op);
Sleep(1);
}
oldpack=bucket;
}
while(bucket>0)
bucketoutput(&bucket,op);
return 0;
}

Dijkstras Algorithm: Program

#include <iostream>
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define infinity 999
using namespace std;
void dijkstras(int cost [10][10], int n, int src, int distance[10])
{
int visited[10],min,u,i,j;
for(i=1;i<=n;i++)
{
distance[i]=cost[src][i];
visited[i]=0;
}
visited[src]=1;
for(i=1;i<=n;i++)
{
min=infinity;
for(j=1;j<=n;j++)
if(visited[j]==0 && distance[j]<min)
{
min=distance[j];
u=j;
}
visited[u]=1;
for(j=1;j<=n;j++)
if(visited[j]==0 && (distance[u]+cost[u][j]<distance[j]))
distance[j]=distance[u]+cost[u][j];
}
}
int main()
{
int cost[10][10],distance[10],n,i,j,sum,dist,src;
cout<<" \n enter the no of nodes:";
cin>>n;
cout<<"\n enter the cost matrix 0 self loop and 999 no edge connection : \n";
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
cin>>cost[i][j];
cout<<"\n enter the source node";
cin>>src;
cout<<"\enter the destination node";
cin>>dist;
dijkstras(cost,n,src,distance);
cout<<"\n shortest distance from"<<src<<"to"<<dist<<"is"<<distance[dist];
getch();
}

RSA Algorithm: Program

#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;

int gcd(int a, int b)


{
while (a!=b)
{
if (a>b)
a=a-b;
else
b=b-a;
}
return (a);
}

int main()
{
double p = 3, q = 5, m;
double n = p*q;
double e = 3; // choose e such that gcd(e,phi)=1
double phi = (p-1)*(q-1);

while (e < phi)


{
if (gcd(e, phi)==1)
break;
else
e++;
}
int k = 5;// choose k such that d*e mod(phi)=1
double d = (1 + (k*phi))/e;// choose d such that d*e mod(phi)=1
cout<<"\n \n Input Message data = ";
cin>> m;
double c= fmod((pow(m,e)),n);
cout<<"\n \n Cipher data ="<<c<<"\n";
m = fmod((pow(c,e)),n);
cout<< "\n \n original data = "<< m<<" \n";
return 0;
}

You might also like