Professional Documents
Culture Documents
Computer Networks Practical
Computer Networks Practical
PRACTICAL
Prepared By:
Kaushik Mazumder – ECB19025
Experiment 1
Theory: Data conversion has always been a widely used application and one
among them can be “conversion of a string to its binary equivalent”. The idea
is to first calculate the length of the string as n and then run a loop n times. In each
iteration store ASCII value of character in variable val and then convert it into
binary number and store result in array finally print the array in reverse order.
#include <bits/stdc++.h>
using namespace std;
cout << endl << "Enter the binary to be decoded to string : \n";
cin >> k;
cout << binToStr(k);
return 0;
}
Output:
Experiment 2:
Program:
#include <bits/stdc++.h>
using namespace std;
// utility function
string strToBinary(string s)
{
int n = s.length();
string bin2 = "";
for (int i = 0; i < n; i++)
{
//Receiver
void simple_tokenizer(string s)
{
stringstream ss (s);
string word;
string str = "";
while (ss>> word) {
if(word != "0" && word != "11")
{//cout<<"\n" << word ;
int w = std::bitset<7>(word).to_ulong();
char r = static_cast<char>(w);
str += r;
}
}
cout<<"\n"<< "Recieved: "<<str;
}
void Reciever(string x)
{
//token =
simple_tokenizer(x);
//cout<<"\n"<<token
}
// driver code
int main()
{
string s = "I am a student of TU";
string x = strToBinary(s);
cout<<"\n"<<x;
Reciever(x);
return 0;
}
Output:
Experiment 3:
Program:
#include <bits/stdc++.h>
using namespace std;
// utility function
string strToBinary(string s)
int n = s.length();
string bin2 = "";
// ASCII value
int val = int(s[i]);
if(val % 2 == 0)
bin.push_back('0');
else
bin.push_back('1');
val /= 2;
}
bin.append("0"); //For 8 bit frame
reverse(bin.begin(), bin.end());
if(bin.length()<8)
{
bin = "0" + bin;
bin2.append(bin);
return bin2;
//Reciever
string Reciever(string s)
int n = s.length();
string k = "";
for(int i = 0; i<n ;i+=8)
{
string str = "";
str += s[j];
}
//cout<<"\n "<<str;
int w = std::bitset<7>(str).to_ulong();
char r = static_cast<char>(w);
k += r;
cout<<"\n"<<k;
return k;
// driver code
int main()
{
Output:
Experiment 6 :
Theory: Cyclic Redundancy Check is the most powerful and easy to implement
technique. Unlike checksum scheme, which is based on addition, CRC is based on
binary division.
In CRC, a sequence of redundant bits, called cyclic redundancy check bits, are
appended to the end of data unit so that the resulting data unit becomes exactly
divisible by a second, predetermined binary number. At the destination, the
incoming data unit is divided by the same number. If at this step there is no
remainder, the data unit is assumed to be correct and is therefore accepted. A
remainder indicates that the data unit has been damaged in transit and therefore
must be rejected.
Program:
#include <stdio.h>
#include <iostream>
{ char validInput[64] ;
};
{ cout<< fa_HDLC.validInput[i]<<endl ;
int main ()
char input ;
int flag = 0 ; // Integer for detecting two flags, (flag =1) first flag detected, (flag= 2)
end flag found.
{ input = getchar() ;
if ( input == '0' )
if ( input == '1' )
+;
if ( input == '1' )
+;
if ( input == '1' )
+;
if ( input == '1' )
+;
if ( input == '1' )
+;
if ( input == '1' )
+;
if ( input == '0' )
if(flag == 0)
{
j=0;
flag++ ;
counter++ ;
}// End if
}// End if
else
return 0;
Output:
Experiment 4:
Program :
#include<bits/stdc++.h>
using namespace std;
// Finds XOR of 'a' and 'b' (both of same length)
string xor1(string a, string b)
{
if (a[i] == b[i])
result += "0";
else
result += "1";
}
return
result;
if (tmp[0] == '1')
else
if (tmp[0] == '1')
else
return tmp;
int main()
Output :
Experiment 5:
#include <iostream>
#include <string.h>
#define MAX 100
#define PARITY 100
int main() {
int i,j,seg,flag=0;
char arr[MAX],vrc[PARITY],lrc[PARITY];
for(i=2;i<strlen(arr);i++){
if(strlen(arr)%i==0){
flag=1;
break;
}else{
flag=0;
}
}
if(flag==1){
seg=strlen(arr)/i;
}else{
seg=strlen(arr);
}
for(i=0;i<strlen(arr);i++){
if( i%(strlen(arr)/seg) == 0 ){
vrc[i/(strlen(arr)/seg)]=arr[i];
continue;
}
if(vrc[i/(strlen(arr)/seg)]==arr[i]){
vrc[i/(strlen(arr)/seg)]='0';
}
else{
vrc[i/(strlen(arr)/seg)]='1';
}
}
for(i=0;i<strlen(arr);i++){
if( i<(strlen(arr)/seg) ){
lrc[i]=arr[i];
continue;
}
if(lrc[i%(strlen(arr)/seg)]==arr[i]){
lrc[i%(strlen(arr)/seg)]='0';
}
else{
lrc[i%(strlen(arr)/seg)]='1';
}
}
for(i=0;i<(strlen(arr)/seg);i++){
cout<<"-----";
}
cout<<endl;
for(i=0;i<(strlen(arr)/seg);i++){
cout<<" "<<lrc[i]<<" ";
}
cout<<endl;
return 0;
}
Output:
Experiment 8:
Theory : The Bellman-Ford algorithm [FORD62] can be stated as follows: Find the
shortest paths from a given source node subject to the constraint that the paths
contain at most one link, then find the shortest paths with a constraint of paths of at
most two links, and so on. Given a graph and a source vertex source in graph, find
shortest paths from source to all vertices in the given graph. The graph may
contain negative weight edges.
#include <bits/stdc++.h>
struct Edge {
intsrc, dest, weight;
};
struct Graph {
int V, E;
printArr(dist, V);
return;
}
Output:
Experiment 7:
Theory: In checksum error detection scheme, the data is divided into k segments
each of m bits. In the sender’s end the segments are added using 1’s complement
arithmetic to get the sum. The sum is complemented to get the checksum. The
checksum segment is sent along with the data segments. At the receiver’s end, all
received segments are added using 1’s complement arithmetic to get the sum. The
sum is complemented. If the result is zero, the received data is accepted; otherwise
discarded.
The checksum detects all errors involving an odd number of bits. It also detects
most errors involving even number of bits.
(a) Sender’s end for the calculation of the checksum, (b) Receiving end for
checking the checksum
Program:
#include<iostream>
#include<string.h>
int main()
char a[20],b[20];
char sum[20],complement[20];
int i;
cin>>a;
cin>>b;
if(strlen(a)==strlen(b))
char carry='0';
int length=strlen(a);
for(i=length-1;i>=0;i--)
sum[i]='0';
carry='0';
sum[i]='1';
carry='0';
sum[i]='1';
carry='0';
sum[i]='0';
carry='1';
sum[i]='1';
carry='0';
sum[i]='0';
carry='1';
sum[i]='0';
carry='1';
sum[i]='1';
carry='1';
else
break;
cout<<"\nSum="<<carry<<sum;
for(i=0;i<length;i++)
if(sum[i]=='0')
complement[i]='1';
else
complement[i]='0';
if(carry=='1')
carry='0';
else
carry='1';
cout<<"\nChecksum="<<carry<<complement;
else
return 0;
Output: