FALL SEMESTER 2023-2024
NAME: Chethan N V
REG.NO: 21BCE0427
Course Title: Computer Networks
Course Code: BCSE308P
Slot: L55+L56
EXPERIMENT NO : LAB ASSIGNMENT – 2
FACULTY NAME: Arivoli A
Question 1:
Aim: To Detect the error using the Checksum method.
Theory: A checksum is a value that is calculated from the data being
transmitted. The checksum is then sent along with the data. When the
data is received, the checksum is recalculated and compared to the
checksum that was sent. If the two checksums do not match, then an
error has occurred in the transmission.
Algorithm:
Step 1: Start
Step 2: Read two binary strings
Step 3: Add the two binary strings converting to integer
performing normal binary addition
Step 4: Compute One’s Complement for the result obtained
Step 5: Print the sum obtained, And the checksum obtained by
One’s Complement.
Step 6: Print the Data to be Transmitted that would be
combination of two string with the checksum value.
Step 7: Input the Receiver end
Step 8: Add all the bits dividing them equally into 8 bits each
Step 9: compute the One’s complement for the result obtained
Step 10: If the result is all 0’s then data is received successfully
else corrupted.
Step 11: Stop
Program:
#include <bits/stdc++.h>
#include<string>
using namespace std;
string addBinary(string A, string B)
{
if (A.length() > B.length())
return addBinary(B, A);
int diff = B.length() - A.length();
string padding;
for (int i = 0; i < diff; i++)
padding.push_back('0');
A = padding + A;
string res;
char carry = '0';
for (int i = A.length() - 1; i >= 0; i--) {
if (A[i] == '1' && B[i] == '1') {
if (carry == '1')
res.push_back('1'), carry = '1';
else
res.push_back('0'), carry = '1';
}
else if (A[i] == '0' && B[i] == '0') {
if (carry == '1')
res.push_back('1'), carry = '0';
else
res.push_back('0'), carry = '0';
}
else if (A[i] != B[i]) {
if (carry == '1')
res.push_back('0'), carry = '1';
else
res.push_back('1'), carry = '0';
}
}
if (carry == '1')
res.push_back(carry);
reverse(res.begin(), res.end());
int index = 0;
while (index + 1 < res.length() && res[index] == '0')
index++;
return (res.substr(index));
}
char flip(char c) {return (c == '0')? '1': '0';}
string onescomplement(string bin)
{
int n = bin.length();
int i;
string ones, twos;
ones = twos = "";
for (i = 0; i < n; i++)
ones += flip(bin[i]);
return ones;
}
int main()
{
cout<<"Enter the first data (maximum 16 bit): ";
string a,b;
cin>>a;
cout<<"Enter the second data (maximum 16 bit): ";
cin>>b;
string result=addBinary(a,b);
cout<<"Sum: "<<result<<endl;
string result1=onescomplement(result);
cout<<"Checksum: "<<result1<<endl;
cout<<"Data to be Transmitted: "<<a<<" "<<b<<"
"<<result1<<endl<<endl;
cout<<"Enter the receiver data: ";
string rec;
cin>>rec;
int i = 0;
int l=0;
string part[3];
while (i < rec.length())
{
part[l] = rec.substr(i, 8);
l++;
i += 8;
}
part[1]=addBinary(part[0],part[1]);
part[2]=addBinary(part[1],part[2]);
cout<<"Sum: "<<part[2]<<endl;
string result2=onescomplement(part[2]);
cout<<"Complement: "<<result2<<endl;
int value = 0;
for (int i = 0; i < result2.length(); i++)
{
value = value * 2 + (result2[i] - '0');
}
if(value==0)
cout<<"The data has received correctly!!!";
else
cout<<"The data is corrupted!!!";
return 0;
}
Sample Input and Output:
Output:
Question 2:
Aim: To perform Error Detection and Error Correction using
Hamming code Technique.
Theory:
Hamming code is both an error detection and error correction code. It is
a linear code that can detect up to two single-bit errors and correct
single-bit errors. Hamming codes are named after their inventor,
Richard W. Hamming. Hamming codes work by adding redundant bits
to the data being transmitted. These redundant bits are used to check the
data for errors. If an error is detected, the redundant bits can be used to
correct the error.
Algorithm:
Step 1: Start
Step 2: Input the length of the data.
Step 3: Input the data.
Step 4: Calculate the number of parity bits required.
Step 5: Place the parity bits in the correct position in the binary
code representing ‘X’.
Step 6: Calculate all the parity bits required using the hamming
code method.
Step 7: Place the parity bits in the binary code.
Step 8: Stop.
Program:
#include <bits/stdc++.h>
#include<string>
using namespace std;
int main()
{
cout<<"Sender Side: "<<endl;
cout<<"Enter the length of the data: ";
int n;
cin>>n;
cout<<"Enter the data: ";
string data;
cin>>data;
if (data.length() != n)
{
cout << "The string is not of the correct length."
<< endl;
return 0;
}
int i;
for(i=0;i<n;i++)
{
if(pow(2,i)>=n+i+1)
break;
}
int j=i;
cout<<"Number of parity bits is: "<<j<<endl;
cout<<"Parity bits are ";
for(i=0;i<j;i++)
{
if(i!=j-1)
cout<<"P"<<pow(2,i)<<" = X, ";
else
cout<<"P"<<pow(2,i)<<" = X";
}
cout<<"(Here X could be 0 or 1)"<<endl;
cout<<"Code word at Sender Side is: ";
string f="";
i=1;
int kk=0;
int flag=0;
while(i<=n+j)
{
for(int k=0;k<j;k++)
{
if(i==pow(2,k))
{
f="X"+f;
flag=1;
}
}
if(flag==0)
{
f=data[kk]+f;
kk++;
}
i++;
flag=0;
}
cout<<f;
int count=0;
string p1,p2,p8,p4;
for(i=0;i<data.length();i=i+2)
{
if(data[i]=='1')
count++;
}
if(count%2==0)
p1="0";
else
p1="1";
int temp=0;
count=0;
for(int i=1;i<=data.length();i++)
{
if(temp!=2)
if(data[i]=='1')
{
count++;
}
else
{
temp=0;
i=i+2;
}
}
if(count%2==0)
p2="0";
else
p2="1";
temp=0;
count=0;
for(int i=3;i<=data.length();i++)
{
if(temp!=4)
if(data[i]=='1')
{
count++;
}
else
{
temp=0;
i=i+4;
}
}
if(count%2==0)
p4="0";
else
p4="1";
temp=0;
count=0;
for(int i=8;i<=data.length();i++)
{
if(temp!=8)
if(data[i]=='1')
{
count++;
}
else
{
temp=0;
i=i+8;
}
}
if(count%2==0)
p8="0";
else
p8="1";
temp=0;
f="";
i=1;
kk=0;
while(i<=n+j)
{
if(i==pow(2,0))
{
f=p1+f;
flag=1;
}
if(i==pow(2,1))
{
f=p2+f;
flag=1;
}
if(i==pow(2,2))
{
f=p4+f;
flag=1;
}
if(i==pow(2,3))
{
f=p8+f;
flag=1;
}
if(flag==0)
{
f=data[kk]+f;
kk++;
}
i++;
flag=0;
}
cout<<endl<<"P1 = "<<p1<<"\nP2 = "<<p2<<"\nP4 =
"<<p4<<"\nP8 = "<<p8;
cout<<"\nCode word at Sender Side is: ";
cout<<f;
}
Sample Input and Output:
Sender Side:
Enter the length of the data: 7
Enter the data: 1001001
Number of parity bits is: 4
Parity bits are P1 = X, P2 =X, P4 = X and P8= X (Here X could be 0 or 1)
Code word at Sender Site is: 1 0 X 0 1 0 0 X 1 X X (Here X could be 0 or 1)
Output:
Question 3:
Aim: To perform Error Detection using Cycle Redundancy
Check(CRC) Method.
Theory:
CRC works by adding a checksum to the data being transmitted. The
checksum is calculated using a polynomial function. When the data is
received, the checksum is recalculated and compared to the checksum that
was sent. If the two checksums do not match, then an error has occurred
in the transmission.
Algorithm:
Step 1: Start
Step 2: Input the length of data
Step 3: Input the data
Step 4: input the length of the generator.
Step 5: Input the generator’s data.
Step 6: Calculate how many 0’s to be inserted.
Step 7: Append 0’s to the main data and perform binary division
with generator’s data.
Step 8: the remainder obtained is replaced with the appended 0’s.
Step 9: Perform binary division again, if the output obtained is 0
as remained then the code has no errors.
Step 10: Stop.
Program:
#include <bits/stdc++.h>
#include<string>
using namespace std;
int main()
{
cout<<"Enter the length of the data: ";
int n1;
cin>>n1;
cout<<"Enter the data: ";
string data1;
cin>>data1;
if (data1.length() != n1)
{
cout << "The string is not of the correct length."
<< endl;
return 0;
}
cout<<"Enter the length of the Generator (Divisor): ";
int n2;
cin>>n2;
cout<<"Enter the Generator (Divisor) : ";
string data2;
cin>>data2;
string data3=data1;
if (data2.length() != n2)
{
cout << "The string is not of the correct length."
<< endl;
return 0;
}
int count =0;
for(int i=0;i<data2.length();i++)
{
if(data2[i]=='1')
count ++;
}
cout<<"Message after appending 0's : "<<count<<endl;
for(int i=0;i<count;i++)
{
data1=data1+"0";
}
cout<<"Message after appending 0's: "<<data1<<endl;
int dividend = 0;
for (int i = 0; i < data1.length(); i++) {
dividend = dividend * 2 + (data1[i] - '0');
}
int divisor = 0;
for (int i = 0; i < data2.length(); i++) {
divisor = divisor * 2 + (data2[i] - '0');
}
int quotient = 0;
int remainder = dividend;
while (remainder >= divisor) {
quotient++;
remainder -= divisor;
}
string crc = "";
while (remainder > 0) {
int bit = remainder & 1;
crc += bit + '0';
remainder >>= 1;
}
if(crc.length()<3)
{
while(crc.length()!=3)
crc="0"+crc;
}
cout << "CRC bits: " << crc << endl;
data3=data3+crc;
cout<<"Transmitted Data: "<<data3<<endl;
cout<<"Receiver Side: "<<endl;
dividend = 0;
for (int i = 0; i < data3.length(); i++) {
dividend = dividend * 2 + (data3[i] - '0');
}
divisor = 0;
for (int i = 0; i < data2.length(); i++) {
divisor = divisor * 2 + (data2[i] - '0');
}
quotient = 0;
remainder = dividend;
while (remainder >= divisor) {
quotient++;
remainder -= divisor;
}
crc="000";
cout<<"Remainder: "<<crc<<endl;
cout<<"Since remainder is 0, hence the message has
received correctly!!!";
}
Sample input and output:
Output: