0% found this document useful (0 votes)
121 views

Data Communication and Networking Practicals

The document contains C++ code for implementing different error detection techniques like parity check, longitudinal redundancy check (LRC), and cyclic redundancy check (CRC) at the sender and receiver sides for ensuring reliability of data transmission. It includes functions for calculating parity/LRC/CRC bits and appending or checking them at the respective ends. Main functions are used to read data from a file, generate error checking bits, write to an output file, and detect errors at the receiver side by validating the checking bits.

Uploaded by

programmer
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views

Data Communication and Networking Practicals

The document contains C++ code for implementing different error detection techniques like parity check, longitudinal redundancy check (LRC), and cyclic redundancy check (CRC) at the sender and receiver sides for ensuring reliability of data transmission. It includes functions for calculating parity/LRC/CRC bits and appending or checking them at the respective ends. Main functions are used to read data from a file, generate error checking bits, write to an output file, and detect errors at the receiver side by validating the checking bits.

Uploaded by

programmer
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

16ce068 patel jainil

1)parity at sender side


#include<iostream>
#include<fstream>
using namespace std;
string lrc(string a)
{
int count=0;
string parity="0";

for(int i=0;i<a.size();i++)
{
if(a[i]=='1')
{
count++;
}
}
if(count%2!=0)
{
parity="1";
}
else{
parity="0";
}
a=a+parity;
return a;
}
int main()
{
string aa;
ifstream rfile;
rfile.open("D:\input1.txt");
rfile >> aa;
int size=aa.size();
string neww;
neww=neww+lrc(aa);
cout<<neww;
ofstream myfile;
myfile.open ("d:\output1.txt");
myfile << neww;
myfile.close();
}
16ce068 patel jainil

2)parity at receiver side


#include<iostream>
#include<fstream>
using namespace std;
string lrc(string a)
{
int count=0;
string parity="0";

for(int i=0;i<a.size();i++)
{
if(a[i]=='1')
{
count++;
}
}
if(count%2!=0)
{
parity="1";
}
else{
parity="0";
}
a=parity;
return a;
}
int main()
{
string aa;
ifstream rfile;
rfile.open("D:\output1.txt");
rfile >> aa;
string k;
string kk;
int flag=0;
if(lrc(aa)=="0")
{
cout<<"file is perfect";
}
else{
cout<<"file is damaged and droped"; } }
16ce068 patel jainil

3)lrc at sender side


#include<iostream>
#include<fstream>
using namespace std;
string lrc(string a)
{
int count=0;
string parity="0";

for(int i=0;i<a.size();i++)
{
if(a[i]=='1')
{ count++; }
}
if(count%2!=0)
{ parity="1"; }
else { parity="0"; }
a=a+parity;
return a;
}
int main()
{
string aa;
ifstream rfile;
rfile.open("D:\input1.txt");
rfile >> aa;
int size=aa.size();
string k;
string neww;
for(int i=0;i<aa.size();i=i+8)
{
k=aa.substr(i,8);
neww=neww+lrc(k);
}
cout<<neww;
ofstream myfile;
myfile.open ("d:\output1.txt");
myfile << neww;
myfile.close();
}
16ce068 patel jainil

4) lrc at receiver side


#include<iostream>
#include<fstream>
using namespace std;
string lrc(string a)
{
int count=0;
string parity="0";

for(int i=0;i<a.size();i++)
{
if(a[i]=='1')
{ count++; }
}
if(count%2!=0)
{ parity="1"; }
else
{ parity="0"; }
a=a+parity;
return a;
}
int main()
{
string aa;
ifstream rfile;
rfile.open("D:\output1.txt");
rfile >> aa;
string k;
string kk;
int flag=0;
for(int i=0;i<aa.size()-9;i=i+9)
{
k=aa.substr(i,8);
kk=aa.substr(i,9);
if((kk)!=lrc(k))
{ cout<<"error detected at"<<(i/9)<<" th byte \n";
flag=1; }
}
if(flag==0)
{ cout<<"no error detected \n"; } }
16ce068 patel jainil

5)vrc at sender side


#include<iostream>
#include<fstream>
using namespace std;
string vrccheck(string a)
{
int count=0;
string parity="0";
for(int i=0;i<a.size();i++)
{
if(a[i]=='1')
{ count++; }
}
if(count%2!=0)
{ parity="1"; }
else{ parity="0"; }
return parity;
}
int main()
{
string vrc;
string aa;
ifstream rfile;
rfile.open("D:\input1.txt");
rfile >> aa;
string k;
string kk;
for(int j=0;j<8;j++){
for(int i=j;i<aa.size();i=i+8)
{
k=aa.substr(i,1);
kk=k+kk;
}
vrc=vrc+vrccheck(kk);
kk="";
}
cout<<aa+vrc;
ofstream myfile;
myfile.open ("d:\output1.txt");
myfile << aa+vrc;
myfile.close();
}
16ce068 patel jainil

6)vrc at receiver side


#include<iostream>
#include<fstream>
using namespace std;
string vrccheck(string a)
{
int count=0;
string parity="0";
for(int i=0;i<a.size();i++)
{
if(a[i]=='1')
{
count++;
}
}
if(count%2!=0)
{
parity="1";
}
else{
parity="0";
}
return parity;
}
int main()
{
string vrc;

string aa;
ifstream rfile;
rfile.open("D:\output1.txt");
rfile >> aa;
string k;
string kk;
for(int j=0;j<8;j++){
for(int i=j;i<aa.size()-8;i=i+8)
{
k=aa.substr(i,1);
kk=k+kk;

}
vrc=vrc+vrccheck(kk);
16ce068 patel jainil

if(aa.substr(aa.length()-8+j,1)!=vrccheck(kk))
{
cout<<aa.substr(aa.length()-8+j,1)<<" "<<vrccheck(kk);
cout<<"error detected at level "<<j<<"\n";
}
kk="";
}
string check=aa.substr(aa.length()-8,8);
if(vrc==check)
{
cout<<"file is perfect"<<"\n";
}
else
{
cout<<"error in file drop the whole file"<<"\n";
}
}
16ce068 patel jainil

7)crc at sender side


#include<iostream>
#include<fstream>
using namespace std;
string vrccheck(string a)
{
int count=0;
string parity="0";
for(int i=0;i<a.size();i++)
{
if(a[i]=='1')
{
count++;
}
}
if(count%2!=0)
{
parity="1";
}
else{
parity="0";
}
return parity;
}
string lrc(string a)
{
int count=0;
string parity="0";

for(int i=0;i<a.size();i++)
{
if(a[i]=='1')
{
count++;
}
}
if(count%2!=0)
{
parity="1";
}
else{
16ce068 patel jainil

parity="0";
}
a=a+parity;
return a;
}
int main()
{
string aa;
ifstream rfile;
rfile.open("D:\input1.txt");
rfile >> aa;
int size=aa.size();
string k;
string neww;
for(int i=0;i<aa.size();i=i+8)
{
k=aa.substr(i,8);
neww=neww+lrc(k);
}
string vrc;

string vk;
string vkk;
for(int j=0;j<9;j++){
for(int i=j;i<neww.size();i=i+9)
{
vk=neww.substr(i,1);
vkk=vk+vkk;
}
vrc=vrc+vrccheck(vkk);
vkk="";
}
cout<<neww+vrc;
ofstream myfile;
myfile.open ("d:\output1.txt");
myfile << neww+vrc;
myfile.close();
}
16ce068 patel jainil

8)crc at receiver side


#include<iostream>
#include<fstream>
using namespace std;
string vrccheck(string a)
{
int count=0;
string parity="0";
for(int i=0;i<a.size();i++)
{
if(a[i]=='1')
{
count++;
}
}
if(count%2!=0)
{
parity="1";
}
else{
parity="0";
}
return parity;
}
string lrc(string a)
{
int count=0;
string parity="0";

for(int i=0;i<a.size();i++)
{
if(a[i]=='1')
{
count++;
}
}
if(count%2!=0)
{
parity="1";
}
else{
16ce068 patel jainil

parity="0";
}
a=a+parity;
return a;
}
int lrcclient()
{

string aa;
ifstream rfile;
rfile.open("D:\output1.txt");
rfile >> aa;
string k;
string kk;
int flag=0;
for(int i=0;i<aa.size()-9;i=i+9)
{
k=aa.substr(i,8);
kk=aa.substr(i,9);
if((kk)!=lrc(k))
{
return i/9;
}
}
return 0;

}
int main()
{

string vrc;
int level=0;
string aa;
ifstream rfile;
rfile.open("D:\output1.txt");
rfile >> aa;
string k;
string kk;
for(int j=0;j<9;j++){
for(int i=j;i<aa.size()-9;i=i+9)
{
16ce068 patel jainil

k=aa.substr(i,1);
kk=k+kk;
}
vrc=vrc+vrccheck(kk);
if(aa.substr(aa.length()-9+j,1)!=vrccheck(kk))
{
cout<<aa.substr(aa.length()-9+j,1)<<" "<<vrccheck(kk);
level=j;
goto x;
}
kk="";
}
x:
cout<<"error found at"<<(((lrcclient())*8)+(level+1));

You might also like