Professional Documents
Culture Documents
NC Da3
NC Da3
Course Course
Class Group Course Title Class Id Slot
Code Type
General Network and Embedded
CSE1004 VL2021220502954 L51+L52
(Semester) Communication Lab
TABLE OF CONTENTS
SN QUESTION PG NO
O
01 Implementation of Go Back N 03
Sliding Window Protocol
02 Implementation of Check Sum 06
03 Implementation of CRC 14
2
20BCE0793 MEGHA BHATTACHARYA LAB 03
Program Code:
//GO BACK N
#include<stdio.h>
int main()
{
printf ("NAME: MEGHA BHATTACHARYA\n");
printf ("REG NO: 20BCE0793\n");
printf ("SUB: CSE 1004 - NETWORKING AND COMMUNICATION\n");
printf ("LAB ASSESSMENT: 03\n");
printf ("GO BACK N SLIDING WINDOW PROTOCOL\n\n\n\n");
int windowsize,sent=0,ack,i;
printf("Enter window size\n");
scanf("%d",&windowsize);
while(1)
{
for( i = 0; i < windowsize; i++)
{
printf("Frame %d has been transmitted.\n",sent);
sent++;
if(sent == windowsize)
break;
}
printf("\nPlease enter the last Acknowledgement received.\
n");
scanf("%d",&ack);
3
20BCE0793 MEGHA BHATTACHARYA LAB 03
if(ack == windowsize)
break;
else
sent = ack;
}
return 0;
}
Output:
PTO
4
20BCE0793 MEGHA BHATTACHARYA LAB 03
5
20BCE0793 MEGHA BHATTACHARYA LAB 03
Observation:
In the above we can see that, in Go-Back-N ARQ, N is the sender's window
size. If the acknow ledgment of a frame is not received within a time
period, then all the frames available in the current window will be
retra nsmitted. Like in the case there were 8 frames and we told that the last
acknowledgement is of 6th frame. So it retransmitted the next two frames
that is frame 7 and frame 8.
Algorithm:
a. Start
b. Divide the message into the binary string of given block size
c. All the binary strings are added together to get the sum
d. The one’s complement of the binary string representing the sum is the
required checksum value
e. Check if the value of the received message is equal to zero
f. The checksum of the received message can be calculated similarly to the
checksum calculated in the above process
g. If the checksum value is zero, the message is transmitted properly with no
errors otherwise some error has occurred during the transmission
h. End
Program Code:
//CHECKSUM
#include <iostream>
#include <string>
using namespace std;
// Function to find the One's complement of given string
6
20BCE0793 MEGHA BHATTACHARYA LAB 03
string ones_comp(string s)
{
for (int i = 0; i < s.size(); i++)
{
if (s[i] == '0')
{
s[i] = '1';
}
else
{
s[i] = '0';
}
}
return s;
}
// Function to return the checksum value of
// the give string when divided in K size blocks
string checkSum(string inp, int blocksize)
{
int n = inp.size();
if (n % blocksize != 0)
{
int pad_size = blocksize - (n % blocksize);
for (int i = 0; i < pad_size; i++)
{
inp = '0' + inp;
}
}
string result = "";
for (int i = 0; i < blocksize; i++)
{
result += inp[i];
}
for (int i = blocksize; i < n; i += blocksize)
{
string nextblock = "";
for (int j = i; j < i + blocksize; j++)
{
nextblock += inp[j];
}
string add = "";
int sum = 0, carry = 0;
for (int k = blocksize - 1; k >= 0; k--)
7
20BCE0793 MEGHA BHATTACHARYA LAB 03
{
sum += (nextblock[k] - '0') + (result[k] - '0');
carry = sum / 2;
if (sum == 0)
{
add = '0' + add;
sum = carry;
}
else if (sum == 1)
{
add = '1' + add;
sum = carry;
}
else if (sum == 2)
{
add = '0' + add;
sum = carry;
}
else
{
add = '1' + add;
sum = carry;
}
}
string final = "";
if (carry == 1)
{
for (int l = add.size() - 1; l >= 0; l--)
{
if (carry == 0)
{
final = add[l] + final;
}
else if (((add[l] - '0') + carry) % 2 == 0)
{
final = "0" + final;
carry = 1;
}
else
{
final = "1" + final;
carry = 0;
}
8
20BCE0793 MEGHA BHATTACHARYA LAB 03
}
result = final;
}
else
{
result = add;
}
}
cout << "Sum is: " << result << endl;
string chres = ones_comp(result);
cout << "Check_Sum is: " << chres << endl;
return ones_comp(result);
}
int count0(string inp)
{
int res = 0;
for (int i = 0; i < inp.size(); ++i)
{
if (inp[i] == '0')
{
res++;
}
}
return res;
}
bool checkboth_checksum(string sent_message, string rec_message, int
block_size)
{
cout << "\nSender side calculation:" << endl;
string sender_checksum = checkSum(sent_message, block_size);
cout << "\nReciever side calculation:" << endl;
string receiver_checksum = checkSum(rec_message, block_size);
int gotsize = count0(receiver_checksum);
if (gotsize == block_size)
{
return true;
}
else
{
return false;
}
}
// Driver Code
9
20BCE0793 MEGHA BHATTACHARYA LAB 03
int main()
{
cout << "Enter the original data:" << endl;
string sent_message;
cin >> sent_message;
cout << "Enter the recieved message:" << endl;
string recv_message;
cin >> recv_message;
int block_size = 8;
if (checkboth_checksum(sent_message, recv_message, block_size))
{
cout << "No Error";
}
else
{
cout << "Error";
}
return 0;
}
10
20BCE0793 MEGHA BHATTACHARYA LAB 03
11
20BCE0793 MEGHA BHATTACHARYA LAB 03
12
20BCE0793 MEGHA BHATTACHARYA LAB 03
Output:
13
20BCE0793 MEGHA BHATTACHARYA LAB 03
Observation:
The Sender followed the given steps:
The block unit is divided into k sections, and each of n bits.
All the k- sections are added together by using one's complement to get
the sum.
The sum is complemented and it becomes the checksum field.
The original data and checksum field are sent across the network.
d. In receiver side
14
20BCE0793 MEGHA BHATTACHARYA LAB 03
In each step, a copy of the divisor (or data) is XORed with the k bits of
the dividend (or key).
e. The result of the XOR operation (remainder) is (n-1) bits, which is used
for the next step after 1 extra bit is pulled down to make it n bits long.
f. When there are no bits left to pull down, we have a result. The (n-1)-bit
remainder which is appended at the sender side.
g. End
Program Code:
//CRC
#include <iostream>
#include <math.h>
#include <cstring>
using namespace std;
char exor(char a, char b) // perform exor operation
{
if (a == b)
return '0';
else
return '1';
}
15
20BCE0793 MEGHA BHATTACHARYA LAB 03
16
20BCE0793 MEGHA BHATTACHARYA LAB 03
cout << "Our remainder is: " << rem2 << endl;
cout << "Data is a accepted" << endl;
}
int main()
{
17
20BCE0793 MEGHA BHATTACHARYA LAB 03
18
20BCE0793 MEGHA BHATTACHARYA LAB 03
Output:
19
20BCE0793 MEGHA BHATTACHARYA LAB 03
Observation:
In sender side, the binary data is first augmented by adding k-1 zeros in the end
of the data. Then we use modulo-2 binary division to divide binary data by the
key and store remainder of division. Append the remainder at the end of the
data to form the encoded data and send the same.
In receiver side, in each step, a copy of the divisor (or data) is XORed with the
k bits of the dividend (or key). The result of the XOR operation (remainder) is
(n-1) bits, which is used for the next step after 1 extra bit is pulled down to
make it n bits long. When there are no bits left to pull down, we have a result.
The (n-1)-bit remainder which is appended at the sender side.
In this way we have control errors by Cyclic Redundancy Check.
_____________________________________________
THE END
THANK YOU
_____________________________________________
20
20BCE0793 MEGHA BHATTACHARYA LAB 03
DONE BY:
Student Details:
Student Name: MEGHA BHATTACHARYA
Roll/Reg No: 20BCE0793
Email: megha.bhattacharya2020@vitstudent.ac.in
Mobile: 6382933694
21