DEEN DAYAL UPADHYAYA COLLEGE
UNIVERSITY OF DELHI
PRACTICAL QUESTIONS SUBMISSION
NAME: Suyash Mehra
ROLL NO.: 21HCS4185
COURSE: B.Sc. (Hons) Computer Science
SEMESTER: III
SUBJECT: COMPUTER NETWORKS
UNIQUE PAPER CODE: 32341303
1. Simulate Cyclic Redundancy Check (CRC) error detection
algorithm for noisy channel.
ANS:
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
void calc(int* temp,int* gen,int size)
{
for(int i=0;i<size;i++)
{
if(temp[i]==gen[i])
temp[i]=0;
else
temp[i]=1;
}
}
int main()
{
int *msg, *key;
int msize,ksize,i;
cout<<"\n Eneter the size of key:";
cin>>ksize;
key=new int[ksize];
cout<<"\n Enter key:";
for(i=0;i<ksize;i++)
cin>>key[i];
cout<<"\n Enter the size of message:";
cin>>msize;
msg=new int[msize+ksize-1];
cout<<"\n Enter message:";
for(i=0;i<msize;i++)
cin>>msg[i];
for(i=msize;i<msize+ksize-1;i++)
msg[i]=0;
int *temp=new int[ksize];
int *zkey=new int[ksize];
for(i=0;i<ksize;i++)
{
temp[i]=msg[i]; zkey[i]=0;
}
for(i=ksize-1;i<msize+ksize-1;i++)
{
temp[ksize-1]=msg[i];
if(temp[0]==0)
calc(temp,zkey,ksize);
else
calc(temp,key,ksize);
for(int j=1;j<ksize;j++)
{
temp[j-1]=temp[j];
}
}
cout<<"\n CRC is:";
for(i=0;i<ksize-1;i++)
cout<<temp[i];
for(int i=msize,j=0;i<ksize+msize-1,j<ksize-1;i++,j++)
{
msg[i]=temp[j];
}
int n=rand()%(ksize+msize+5);
msg[n]=!msg[n];
cout<<"\n"<<n<<"\n";
for(i=0;i<ksize;i++)
{
temp[i]=msg[i]; zkey[i]=0;
}
for(i=ksize-1;i<msize+ksize-1;i++)
{
temp[ksize-1]=msg[i];
if(temp[0]==0)
calc(temp,zkey,ksize);
else
calc(temp,key,ksize);
for(int j=1;j<ksize;j++)
{
temp[j-1]=temp[j]; }
}
for(int i=0;i<ksize-1;i++)
{
cout<<temp[i];
}
int flag=1;
for(int i=0;i<ksize-1;i++)
{
if(temp[i]==1)
flag=0;
}
if(flag==0)
cout<<"\nError";
else
cout<<"\nNo error";
getch();
return 0;
}
Output:
2. Simulate and implement stop and wait protocol for noisy channel.
ANS:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
using namespace std;
#define time 5
#define max_seq 1
#define tot_pack 5
int randn(int n)
{
return rand()%n + 1;
}
typedef struct
{
int data;
}packet;
typedef struct
{
int kind;
int seq;
int ack;
packet info;
}frame;
typedef enum{ frame_arrival,error,time_out}event_type;
frame data1;
//creating prototype
void from_network_layer(packet *);
void to_physical_layer(frame *);
void to_network_layer(packet *);
void from_physical_layer(frame*);
void sender();
void receiver();
void wait_for_event_sender(event_type *);
void wait_for_event_receiver(event_type *);
//end
#define inc(k) if(k<max_seq)k++;else k=0;
int i=1;
char turn;
int disc=0;
int main()
{
while(!disc)
{ sender();
// delay(400);
receiver();
}
getchar();
}
void sender()
{
static int frame_to_send=0;
static frame s;
packet buffer;
event_type event;
static int flag=0; //first place
if (flag==0)
{
from_network_layer(&buffer);
s.info=buffer;
s.seq=frame_to_send;
cout<<"\nsender information \t"<<s.info.data<<"\n";
cout<<"\nsequence no. \t"<<s.seq;
turn='r';
to_physical_layer(&s);
flag=1;
}
wait_for_event_sender(&event);
if(turn=='s')
{
if(event==frame_arrival)
{
from_network_layer(&buffer);
inc(frame_to_send);
s.info=buffer;
s.seq=frame_to_send;
cout<<"\nsender information \t"<<s.info.data<<"\n";
cout<<"\nsequence no. \t"<<s.seq<<"\n";
getch();
turn='r';
to_physical_layer(&s);
}
} //end of sender function
void from_network_layer(packet *buffer)
{
(*buffer).data=i;
i++;
} //end of from network layer function
void to_physical_layer(frame *s)
{
data1=*s;
} //end of to physical layer function
void wait_for_event_sender(event_type *e)
{
static int timer=0;
if(turn=='s')
{ timer++;
//timer=0;
return ;
}
else //event is frame arrival
{
timer=0;
*e=frame_arrival;
}
} //end of wait for event function
void receiver()
{
static int frame_expected=0;
frame s,r;
event_type event;
wait_for_event_receiver(&event);
if(turn=='r')
{ if(event==frame_arrival)
{
from_physical_layer(&r);
if(r.seq==frame_expected)
{
to_network_layer(&r.info);
inc (frame_expected);
}
else
cout<<"\nReceiver :Acknowledgement resent \n";
getch();
turn='s';
to_physical_layer(&s);
}
}
} //end of receiver function
void wait_for_event_receiver(event_type *e)
{
if(turn=='r')
{
*e=frame_arrival;
}
}
void from_physical_layer(frame *buffer)
{
*buffer=data1;
}
void to_network_layer(packet *buffer)
{
cout<<"\nReceiver : packet received \t"<< i-1;
cout<<"\n Acknowledgement sent \t";
getch();
if(i>tot_pack)
{ disc=1;
cout<<"\ndiscontinue\n";
}
}
Output:
3. Simulate and implement go back n sliding window protocol.
ANS:
#include<bits/stdc++.h>
#include<ctime>
#define ll long long int
using namespace std;
void transmission(ll & i, ll & N, ll & tf, ll & tt) {
while (i <= tf) {
int z = 0;
for (int k = i; k < i + N && k <= tf; k++) {
cout << "Sending Frame " << k << "..." << endl;
tt++;
}
for (int k = i; k < i + N && k <= tf; k++) {
int f = rand() % 2;
if (!f) {
cout << "Acknowledgment for Frame " << k << "..." << endl;
z++;
} else {
cout << "Timeout!! Frame Number : " << k << " Not Received" <<
endl;
cout << "Retransmitting Window..." << endl;
break;
}
}
cout << "\n";
i = i + z;
}
}
int main() {
ll tf, N, tt = 0;
srand(time(NULL));
cout << "Enter the Total number of frames : ";
cin >> tf;
cout << "Enter the Window Size : ";
cin >> N;
ll i = 1;
transmission(i, N, tf, tt);
cout << "Total number of frames which were sent and resent are : " <<
tt <<
endl;
return 0;
}
Output:
4. Simulate and implement selective repeat sliding window protocol.
ANS:
#include<iostream>
using namespace std;
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#define TOT_FRAMES 500
#define FRAMES_SEND 10
class sel_repeat
private:
int fr_send_at_instance;
int arr[TOT_FRAMES];
int send[FRAMES_SEND];
int rcvd[FRAMES_SEND];
char rcvd_ack[FRAMES_SEND];
int sw;
int rw; //tells expected frame
public:
void input();
void sender(int);
void receiver(int);
};
void sel_repeat::input()
int n; //no. of bits for the frame
int m; //no. of frames from n bits
int i;
cout<<"Enter the no. of bits for the sequence no. : ";
cin>>n;
m=pow(2,n);
int t=0;
fr_send_at_instance=(m/2);
for(i=0;i<TOT_FRAMES;i++)
arr[i]=t;
t=(t+1)%m;
for(i=0;i<fr_send_at_instance;i++)
send[i]=arr[i];
rcvd[i]=arr[i];
rcvd_ack[i]='n';
rw=sw=fr_send_at_instance;
sender(m);
void sel_repeat::sender(int m)
for(int i=0;i<fr_send_at_instance;i++)
if(rcvd_ack[i]=='n')
cout<<"SENDER : Frame "<<send[i]<<" is sent\n";
receiver(m);
void sel_repeat::receiver(int m)
time_t t;
int f;
int j;
int f1;
int a1;
char ch;
srand((unsigned)time(&t));
for(int i=0;i<fr_send_at_instance;i++)
if(rcvd_ack[i]=='n')
f=rand()%10;
//if f=5 frame is discarded for some reason
//else frame is correctly recieved
if(f!=5)
for(int j=0;j<fr_send_at_instance;j++)
if(rcvd[j]==send[i])
cout<<"reciever:Frame"<<rcvd[j]<<"recieved correctly\n";
rcvd[j]=arr[rw];
rw=(rw+1)%m;
break;
}
int j;
if(j==fr_send_at_instance)
cout<<"reciever:Duplicate frame"<<send[i]<<"discarded\n";
a1=rand()%5;
//if al==3 then ack is lost
//else recieved
if(a1==3)
cout<<"(acknowledgement "<<send[i]<<" lost)\n";
cout<<"(sender timeouts-->Resend the frame)\n";
rcvd_ack[i]='n';
else
{
cout<<"(acknowledgement "<<send[i]<<" recieved)\n";
rcvd_ack[i]='p';
else
{int ld=rand()%2;
//if =0 then frame damaged
//else frame lost
if(ld==0)
cout<<"RECEIVER : Frame "<<send[i]<<" is damaged\n";
cout<<"RECEIVER : Negative Acknowledgement "<<send[i]<<"
sent\n";
else
cout<<"RECEIVER : Frame "<<send[i]<<" is lost\n";
cout<<"(SENDER TIMEOUTS-->RESEND THE FRAME)\n";
}
rcvd_ack[i]='n';
for(int j=0;j<fr_send_at_instance;j++)
if(rcvd_ack[j]=='n')
break;
int i=0;
for(int k=j;k<fr_send_at_instance;k++)
send[i]=send[k];
if(rcvd_ack[k]=='n')
rcvd_ack[i]='n';
else
rcvd_ack[i]='p';
i++;
if(i!=fr_send_at_instance)
for(int k=i;k<fr_send_at_instance;k++)
send[k]=arr[sw];
sw=(sw+1)%m;
rcvd_ack[k]='n';
cout<<"Want to continue";
cin>>ch;
cout<<"\n";
if(ch=='y')
sender(m);
else
exit(0);
int main()
sel_repeat sr;
sr.input();
}
Output:
5. Simulate and implement distance vector routing algorithm.
ANS:
#include<stdio.h>
#include<iostream>
using namespace std;
struct node
{
unsigned dist[6];
unsigned from[6];
}DVR[10];
int main()
{
cout<<"\n\n PROGRAM TO IMPLEMENT DISTANCE VECTOR
ROUTING ALGORITHM ";
int costmat[6][6];
int nodes, i, j, k;
cout<<"\n\n Enter the number of nodes : ";
cin>>nodes;
cout<<"\n Enter the cost matrix : \n" ;
for(i = 0; i < nodes; i++)
{
for(j = 0; j < nodes; j++)
{
cin>>costmat[i][j];
costmat[i][i] = 0;
DVR[i].dist[j] = costmat[i][j];
DVR[i].from[j] = j;
}
}
for(i = 0; i < nodes; i++)
for(j = i+1; j < nodes; j++)
for(k = 0; k < nodes; k++)
if(DVR[i].dist[j] > costmat[i][k] + DVR[k].dist[j])
{
DVR[i].dist[j] = DVR[i].dist[k] + DVR[k].dist[j];
DVR[j].dist[i] = DVR[i].dist[j];
DVR[i].from[j] = k;
DVR[j].from[i] = k;
}
for(i = 0; i < nodes; i++)
{
cout<<"\n\n For router: "<<i+1;
for(j = 0; j < nodes; j++)
cout<<"\t\n node "<<j+1<<" via "<<DVR[i].from[j]+1<<"
Distance "<<DVR[i].dist[j];
}
cout<<" \n\n ";
return 0;
}
Output:
6. Simulate and implement Dijkstra algorithm for shortest path
routing.
ANS:
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
class DVR{
private:
int** routingTable;
int size;
public:
DVR(int size){
// Setting up values and giving memory to class members
this->size = size;
this->routingTable = new int*[size];
for (int i = 0; i < this->size; i++){
this->routingTable[i] = new int[size];
}
for (int i = 0; i < this->size; i++){
for (int j = 0; j < this->size; j++){
if (i == j){
this->routingTable[i][j] = 0;
continue;
}
if (i > j){
this->routingTable[i][j] = this->routingTable[j][i];
continue;
}
cout << "What is the distance between node " << i+1 << " "
<< j+1 << endl;
cout << "Enter (-1) if no routes exist ";
cin >> this->routingTable[i][j];
}
}
}
bool routeExist(int start, int end){
return (this->routingTable[start][end] != -1);
}
int lenRoute(int start, int end){
return this->routingTable[start][end];
}
void printRoutingTable(){
cout << "\t";
for (int i = 0; i < this->size; i++) cout << i << "\t";
cout << endl << " ";
for (int i = 0; i < this->size; i++) cout << "--------";
cout << endl;
for (int i = 0; i < this->size; i++){
cout << i << "| \t";
for (int j = 0; j < this->size; j++){
cout << this->routingTable[i][j] << "\t";
}
cout << endl;
}
cout << endl;
}
int findRouteLength(int start, int end, vector<int> checked, int
curLen = 0, int maxPass = 2147483647){
if (maxPass == 0) return -1;
if (routeExist(start , end)){
for (int j = 0; j < checked.size(); j++){
cout << checked[j] << "->";
}
cout << end << "\t\t" << "Length " << curLen +
lenRoute(start, end) << endl;
return curLen + lenRoute(start, end);
}
int** lengths = new int*[this->size];
for (int i = 0; i < this->size; i++){
lengths[i] = new int[2];
}
int lengths_size = 0;
vector<int> values;
for (int i = 0; i < this->size; i++){
bool already_checked = false;
if (i == start || i == end) already_checked = true;
for (int j = 0; j < checked.size(); j++){
if (checked[j] == i) already_checked = true;
}
if (already_checked) continue;
else if (routeExist(start, i)){
int j;
for (j = 0; j < lengths_size; j++){
if (lengths[j][1] > lenRoute(start, i)) break;
}
lengths[j][0] = i;
lengths[j][1] = lenRoute(start, i);
lengths_size++;
}
}
for (int i = 0; i < lengths_size; i++){
checked.push_back(lengths[i][0]);
values.push_back(findRouteLength(lengths[i][0] , end,
checked ,
curLen + lengths[i][1] , --maxPass));
checked.pop_back();
}
if (lengths_size == 0) return -1;
return *min_element(values.begin(), values.end());
}
void destroyRoute(int start, int end){
this->routingTable[start][end] = this->routingTable[end][start] =
-1;
}
};
int main(){
DVR A(5);
try{
vector<int> checked;
int start, end;
bool run = true;
while (run){
cout << "Do you want continue (1 = yes/ 0 = no) ";
cin >> run;
if (!run) break;
cout << "From where do you want to start ? ";
cin >> start;
cout << "Where do you want to end ? ";
cin >> end;
checked.push_back(start);
A.findRouteLength(start, end, checked);
checked.pop_back();
}
} catch (const char* exp){
cout << "Exception Occurred : " << exp << endl;
}
return 0;
}
Output:
7. Implementation of Hamming code in c++.
ANS:
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
class Receiver;
class Sender
{
int parity_type, codewordlen, msglen, message[100], parity_bit=0;
int codeword[100], p=0, m=1, i, j, k , l;
int paritycount, onecount;
public:
//int i,parity_type,message_size,msg[100],parity_bit=0;
void getmessage();
void parity_count();
void codeword_array();
void final_codeword_print();
void even_odd();
friend Receiver;
};
class Receiver
{
int random1, random2;
int x,y,z, paritycount, onecount, decimal_array[100],
decimal2_array[100];
int received[100];
public:
void one_bit_random(Sender);
void one_biterror_codeword(Sender);
void one_biterror_detection(Sender);
void two_bit_random(Sender);
void two_biterror_codeword(Sender);
void two_biterror_detection(Sender);
};
void Receiver :: one_bit_random(Sender s)
{
random1= rand()%s.codewordlen;
for(x=0; x<=s.parity_bit; x++)
{
if(random1==pow(2,x))
{
random1=random1+1;
}
}
for(x=1; x<=s.codewordlen; x++)
{
received[x]=s.codeword[x];
}
if(received[random1]==0)
{
received[random1]=1;
}
else
{
received[random1]=0;
}
cout<<"\n\n Error Created at position: "<<random1<<"\n\n Where
Codeword Size is: "<<s.codewordlen;
void Receiver :: one_biterror_codeword(Sender s)
{
cout<<"\n\n codeword with one bit error in message";
for(int x=1; x<=s.codewordlen; x++)
{
cout<<" "<<received[x];
}
void Receiver :: one_biterror_detection(Sender s)
{
for(x=1; x<=s.parity_bit; x++)
{
onecount=0;
paritycount=pow(2,x-1);
for(y=paritycount-1; y<=s.codewordlen; y=y+paritycount+z-1)
{
for(z=1 ;z<=paritycount; z++)
{
if(received[y+z]==1)
onecount++;
}
}
if(s.parity_type==1)
{
if(onecount%2==0)
decimal_array[x]=1;
else
decimal_array[x]=0;
}
else
{
if(onecount%2==0)
decimal_array[x]=0;
else
decimal_array[x]=1;
}
}
int error_decimal=0;
for(y=s.parity_bit; y>0; y--)
{
error_decimal=error_decimal+pow(2,y-1)*decimal_array[y];
}
cout<<"\n\n Error is at postition: ";
cout<<error_decimal;
if(received[error_decimal]==0)
{
received[error_decimal]=1;
}
else
{
received[error_decimal]=0;
}
cout<<"\n\n Sender's Message with parity bits:";
for (y = 1; y <=s.codewordlen ; y++)
{
cout<<" "<<received[y];
}
cout<<"\n\n Sender's Message without parity bits:";
int p=0;
for (y = 1; y <= s.codewordlen; y++)
{
if (y==pow(2, p))
{
cout<<"";
p++;
}
else
{
cout<<" "<<received[y];
}
}
}
void Receiver :: two_bit_random(Sender s)
{
random1= rand()%s.codewordlen;
for(x=0; x<=s.parity_bit; x++)
{
if(random1==pow(2,x))
{
random1=random1+1;
}
}
for(x=1; x<=s.codewordlen; x++)
{
received[x]=s.codeword[x];
}
if(received[random1]==0)
{
received[random1]=1;
}
else
{
received[random1]=0;
}
cout<<"\n\n 1st Error Created at position: "<<random1<<"\n\n Where
Codeword Size is: "<<s.codewordlen;
random2= random1+1;
if(received[random2]==0)
{
received[random2]=1;
}
else
{
received[random2]=0;
}
cout<<"\n\n 2nd Error Created at position: "<<random2<<"\n\n
Where Codeword Size is: "<<s.codewordlen;
}
void Receiver :: two_biterror_codeword(Sender s)
{
cout<<"\n\n Codword with two bits error:\n";
for(x=1; x<=s.codewordlen; x++)
{
cout<<" "<<received[x];
}
void Receiver :: two_biterror_detection(Sender s)
{
int twoE;
for(x=1; x<=s.parity_bit; x++)
{
onecount=0;
paritycount=pow(2,x-1);
for(y=paritycount-1; y<=s.codewordlen; y=y+paritycount+z-1)
{
for(z=1 ;z<=paritycount; z++)
{
if(received[y+z]==1)
onecount++;
}
}
if(s.parity_type==1)
{
if(onecount%2!=0)
decimal_array[x]=1;
else
decimal_array[x]=0;
}
else
{
if(onecount%2!=0)
decimal_array[x]=0;
else
decimal_array[x]=1;
}
}
int error_decimal=0;
for(y=s.parity_bit; y>0; y--)
{
error_decimal=error_decimal+pow(2,y-1)*decimal_array[y];
}
cout<<"\n\n 1st Error is at position: ";
cout<<error_decimal;
if(received[error_decimal]==0)
{
received[error_decimal]=1;
}
else
{
received[error_decimal]=0;
}
//AGAIN!!!
for(x=1; x<=s.parity_bit; x++)
{
onecount=0;
paritycount=pow(2,x-1);
for(y=paritycount-1; y<=s.codewordlen; y=y+paritycount+z-1)
{
for(z=1 ;z<=paritycount; z++)
{
if(received[y+z]==1)
onecount++;
}
}
if(s.parity_type==1)
{
if(onecount%2==0)
decimal2_array[x]=1;
else
decimal2_array[x]=0;
}
else
{
if(onecount%2==0)
decimal2_array[x]=0;
else
decimal2_array[x]=1;
}
}
int error_decimal2=0;
for(y=s.parity_bit; y>0; y--)
{
error_decimal2=error_decimal2+pow(2,y-1)*decimal2_array[y];
}
cout<<"\n\n 2nd Error is at position: ";
cout<<error_decimal2;
if(received[error_decimal2]==0)
{
received[error_decimal2]=1;
}
else
{
received[error_decimal2]=0;
}
cout<<"\n\n Sender's Message with parity bits:";
for (y = 1; y <=s.codewordlen ; y++)
{
cout<<" "<<received[y];
}
cout<<"\n\n Sender's Message Finally Received without parity bits:";
int p=0;
for (y = 1; y <= s.codewordlen; y++)
{
if (y==pow(2, p))
{
cout<<"";
p++;
}
else
{
cout<<" "<<received[y];
}
}
void Sender :: even_odd()
{
for(j=1; j<=parity_bit; j++)
{
onecount=0;
paritycount=pow(2,j-1);
for(i=paritycount-1; i<=codewordlen; i=i+paritycount+k-1)
{
for(k=1 ;k<=paritycount; k++)
{
if(codeword[i+k]==1)
onecount++;
}
}
if(parity_type==1)
{
if(onecount%2==0)
codeword[paritycount]=1;
else
codeword[paritycount]=0;
}
else
{
if(onecount%2==0)
codeword[paritycount]=0;
else
codeword[paritycount]=1;
}
}
void Sender :: final_codeword_print()
{
cout<<"\n\n Codeword in transmitter with final even odd parity
count:\n";
for(i=1; i<=codewordlen; i++)
{
cout<<" "<<codeword[i];
}
}
void Sender :: codeword_array() /// parity bit + data + making box ///
{
codewordlen=msglen+parity_bit;
for (i = 1; i <= codewordlen; i++)
{
if (i==pow(2, p))
{
codeword[i] = 0;
p++;
}
else
{
codeword[i] = message[m];
m++;
}
}
cout<<"\n\n Codeword to be sent in transmitter:\n";
for (i = 1; i <= codewordlen; i++)
{
cout<<" "<<codeword[i];
}
void Sender :: parity_count()
{
for(i=0; i<=msglen; i++)
if(msglen+i > pow (2, i))
{
parity_bit++;
}
void Sender :: getmessage()
{
cout<<"\n\n Enter Sender's message size: ";
cin>>msglen;
cout<<"\n\n Enter message's bit one by one:";
for(i=1; i<=msglen; i++)
{
cin>>message[i];
}
cout<<"\n\n Even(0) or Odd(1) parity??: ";
cin>>parity_type;
cout<<"\n\n Your Original Message is: ";
for(i=1; i<=msglen; i++)
{
cout<<" "<<message[i];
}
}
int main()
{
Sender tahir;
Receiver MRU;
tahir.getmessage();
tahir.parity_count();
tahir.codeword_array();
tahir.even_odd();
tahir.final_codeword_print();
int choice;
cout<<"\n\n How many bit error you want to test? 1 or 2??\n";
cin>>choice;
if(choice==1)
{
MRU.one_bit_random(tahir);
MRU.one_biterror_codeword(tahir);
MRU.one_biterror_detection(tahir);
}
if(choice==2)
{
MRU.two_bit_random(tahir);
MRU.two_biterror_codeword(tahir);
MRU.two_biterror_detection(tahir);
}
cout<<"\n\n\n\n\n";
return 0;;
Output: