Computer Networks Practical File: Hubhangi Bhatia 19570050 BSC Computer Science Hons Iii Semester

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

COMPUTER NETWORKS

PRACTICAL FILE

SHUBHANGI BHATIA
19570050
BSc COMPUTER SCIENCE HONS
III SEMESTER
INDEX

S. No PRACTICAL DETAIL PAGE No.

1 WAP to simulate Cyclic Redundance Check (CRC) error detection 3


algorithm for noisy channel.

2 WAP to simulate and implement stop and wait protocol for noisy 8
channel

3 WAP to simulate and implement go back and sliding window protocol. 15

4 WAP to simulate and implement selective repeat sliding window 17


protocol.

5 WAP to simulate and implement distance vector routing algorithm. 23

6 WAP to simulate and implement Dijkstra algorithm for shortest 26


path routing.
Question 1
WAP in C++ to simulate Cyclic Redundancy Check (CRC) error detection
algorithm for noisy channel.

#include<iostream>
using namespace std;
int length;
void reverse(int n[],int len)
{
int temp;
for(int i=0,j=len-1;i<len/2;i++,j--)
{
temp=n[j];
n[j]=n[i];
n[i]=temp;
}
return;
}
void DecToBin(int dec,int bin[])
{
int temp,i=0;
temp=dec;
while(temp!=0)
{
bin[i]=temp%2;
temp=temp/2;
i++;
}
reverse(bin,i);
length=i;
for(int a=0;a<i;a++)
cout<<bin[a];
cout<<endl;
return;
}
int xor_bin(int divt[],int divr[],int len_div,int len)
{
int j=0,k;
while(divt[j]!=1)
{
j++;
}
if((len-j)<len_div)
return 0;
for(int i=j,k=0;i<(len_div+j);i++,k++)
{
if(divt[i]==divr[k])
divt[i]=0;
else
divt[i]=1;
}
return 1;
}
void append(int n[],int app,int &norm)
{
length=(norm+app)-1;
for(int i=norm-1;i<length;i++)
{
n[i]=0;
}
cout<<"\n The number after appending is: ";
for(int i=0;i<length;i++)
{
cout<<n[i]<<" ";
}
cout<<endl;
norm=length;
}

void division(int mess[],int &len_mess,int a[],int len_a)


{
int flag;
append(mess,len_a,len_mess);
for(int i=0;i<len_mess-2;i++)
{
flag=xor_bin(mess,a,len_a,len_mess);
if(flag==0)
break;
}
cout<<"\n The new message now after dividing is: ";
for(int i=0;i<len_mess;i++)
cout<<mess[i]<<" ";
cout<<endl;
}
int main()
{
int gen,mess;
int
mess_pass[20],mess_temp[20],chc,i=0,length_mess,length_gen,rem[6],OrigMessLen
;
cout<<"\n Enter the message to be passed(in decimal): ";
cin>>mess;
cout<<"\n The message in binary code is: ";
DecToBin(mess,mess_pass);
length_mess=length;
cout<<"\n Enter the generator number i.e, the divisor(in decimal): ";
cin>>gen;
cout<<"\n The binary value of generator number is:\n";
int genr[6];
DecToBin(gen,genr);
length_gen=length;
for(int j=0;j<length_mess;j++)
mess_temp[j]=mess_pass[j];
OrigMessLen=length_mess;
division(mess_pass,length_mess,genr,length_gen);
i=0;
while(mess_pass[i]!=1)
{
i++;
if(i>=length_mess)
break;
}
int r=OrigMessLen;
if(i<length_mess)
{
cout<<"\n The Cyclic Redundancy Check remainder is: ";
for(int j=i;j<length_mess;j++)
{
cout<<mess_pass[j]<<" ";
mess_temp[r]=mess_pass[j];
r++;
}
cout<<endl;
}
cout<<"\n The message to be passed is: ";
for(i=0;i<r;i++)
cout<<mess_temp[i]<<" ";
cout<<endl;
return 0;
}
Question 2
WAP in C++ to simulate and implement stop and wait protocol for noisy
channel.

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
using namespace std;

#define t 5
#define max 1
#define total 5

int randn(int n)
{
return rand()%n+1;
}

typedef struct info1


{
int data;
}packet;

typedef struct info2


{
int kind;
int seq;
int ack;
packet info;
}frame;

typedef enum{framearr, err, timeout}eventtype;

frame data1;
void FromNetworkLayer(packet *);
void ToPhysicalLayer(frame *);
void ToNetworkLayer(packet *);
void FromPhysicalLayer(frame*);
void sender();
void receiver();
void WaitForEventSender(eventtype *);
void WaitForEventReceiver(eventtype *);

#define inc(k) if(k<max)k++;else k=0;

int i=1, disc=0;


char turn;
int main()
{
while(!disc)
{
sender();
receiver();
}
getchar();
}
void sender()
{
static int FrameToSend=0;
static frame f;
packet buf;
eventtype e;
static int flag=0;
if (flag==0)
{
FromNetworkLayer(&buf);
f.info=buf;
f.seq=FrameToSend;
cout<<"\n Sender information: \t"<<f.info.data<<"\n";
cout<<"\n Sequence number: \t"<<f.seq;
turn='r';
ToPhysicalLayer(&f);
flag=1;
}

WaitForEventSender(&e);
if(turn=='f')
{
if(e==framearr)
{
FromNetworkLayer(&buf);
inc(FrameToSend);
f.info=buf;
f.seq=FrameToSend;
cout<<"\n Sender information: \t"<<f.info.data<<"\n";
cout<<"\n Sequence number: \t"<<f.seq<<"\n";

getch();
turn='r';
ToPhysicalLayer(&f);
}
}
}
void FromNetworkLayer(packet *buf)
{
(*buf).data=i;
i++;
}

void ToPhysicalLayer(frame *f)


{
data1=*f;
}

void WaitForEventSender(eventtype *ev)


{
static int timer=0;
if(turn=='f')
{
timer++;
return ;
}
else
{
timer=0;
*ev=framearr;
}
}

void receiver()
{
static int frameexp=0;
frame f,r;
eventtype e;
WaitForEventReceiver(&e);
if(turn=='r')
{
if(e==framearr)
{
FromPhysicalLayer(&r);
if(r.seq==frameexp)
{
ToNetworkLayer(&r.info);
inc(frameexp);
}
else
cout<<"\n Receiver : Acknowledgement resent \n";
getch();
turn='f';
ToPhysicalLayer(&f);
}
}
}

void WaitForEventReceiver(eventtype *ev)


{
if(turn=='r')
{
*ev=framearr;
}
}

void FromPhysicalLayer(frame *buf)


{
*buf=data1;
}

void ToNetworkLayer(packet *buf)


{
cout<<"\n Receiver : packet received \t"<< i-1;
cout<<"\n Acknowledgement sent \t";
getch();
if(i>total)
{
disc=1;
cout<<"\ndiscontinue\n";
}
}
Question 3
WAP in C++ to simulate and implement go back n sliding window protocol.

#include<iostream>
using namespace std;

int main()
{
int win,i,f,frames[50];
cout<<"\n Enter window size: ";
cin>>win;
cout<<"\n Enter number of frames to transmit: ";
cin>>f;
cout<<"\n Enter frames: ";
for(i=1;i<=f;i++)
{
cout<<"\n Enter frame "<<i<<" : ";
cin>>frames[i];
}
cout<<"\n With sliding window protocol the frames will be sent in the following
manner (assuming no corruption of frames)\n\n";
cout<<"\n After sending "<<win<<" frames at each stage sender waits for
acknowledgement sent by the receiver\n\n";

for(i=1;i<=f;i++)
{
if(i%win==0)
{
cout<<frames[i]<<"\n";
cout<<"\n Acknowledgement of above frames sent is received by
sender\n\n";
}
else
cout<<frames[i]<<" ";
}
if(f%win!=0)
cout<<"\n Acknowledgement of above frames sent is received by sender\n";
return 0;
}
Question 4
WAP in C++ to simulate and implement selective repeat sliding window
protocol.

#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#define TOT_FRAMES 500
#define FRAMES_SEND 10
using namespace std;

class sel_repeat
{
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;
public:
void input();
void sender(int);
void receiver(int);
};
void sel_repeat::input()
{
int n, m, i, t=0;
cout<<"\n Enter the number of bits for sequence number: ";
cin>>n;
m=pow(2,n);
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<<"\n SENDER DETAILS: Frame "<<send[i]<<" is
successfully sent! ";
}
receiver(m);
}
void sel_repeat::receiver(int m)
{
time_t t;
int f, j, f1, 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)
{
for(int j=0;j<fr_send_at_instance;j++)
if(rcvd[j]==send[i])
{
cout<<"\n RECEIVER DETAILS :Frame
"<<rcvd[j]<<" recieved correctly! ";
rcvd[j]=arr[rw];
rw=(rw+1)%m;
break;
}
int j;
if(j==fr_send_at_instance)
cout<<"\n RECEIVER DETAILS: Duplicate frame
"<<send[i]<<" has been discarded.";
a1=rand()%5;
if(a1==3)
{
cout<<"\n (Acknowledgement "<<send[i]<<" lost!)";
cout<<"\n (SENDER TIMEOUT! PLEASE RESEND
THE FRAME. )";
rcvd_ack[i]='n';
}
else
{
cout<<"\n (Acknowledgement "<<send[i]<<"
recieved.)";
rcvd_ack[i]='p';
}
}
else
{
int ld=rand()%2;
if(ld==0)
{
cout<<"\n RECEIVER DETAILS: Frame
"<<send[i]<<" is damaged.";
cout<<"\n RECEIVER DETAILS: Negative
acknowledgement "<<send[i]<<" sent.";
}
else
{
cout<<"RECEIVER DETAILS: Frame "<<send[i]<<"
is lost!";
cout<<"\n (SENDER TIMEOUT! PLEASE RESEND
THE FRAME. )";
}
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<<"\n Do you wish to continue? (y/n)";
cin>>ch;
cout<<"\n";
if(ch=='y')
sender(m);
else
exit(0);
}

int main()
{
sel_repeat sr;
sr.input();
}
Question 5
WAP in C++ to simulate and implement distance vector routing algorithm.

#include<stdio.h>
#include<iostream>
using namespace std;
struct node
{
unsigned dist[6];
unsigned from[6];
}DVR[10];

int main()
{
cout<<"\n PROGRAM TO IMPLEMENT DISTANCE VECTOR ROUTING
ALGORITHM ";
int costmat[6][6];
int nodes;
cout<<"\n Enter the number of nodes: ";
cin>>nodes;
cout<<"\n Enter the cost matrix: ";
for(int i=0;i<nodes;i++)
{
for(int 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(int i = 0; i < nodes; i++)
{
for(int j= i+1; j < nodes; j++)
{
for(int 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(int i = 0; i < nodes; i++)
{
cout<<"\n\n For router: "<<i+1;
for(int 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;
}
Question 6
WAP in C++ to simulate and implement Dijkstra algorithm for shortest path
routing.

#include<iostream>
using namespace std;

class DijkstraRouting
{
int N;
int graph[10][10];
int dist[10];
bool visited[10];
int parent[10];
public:
void createGraph();
int minDistance();
void printPath(int j);
void dijkstra();
};

void DijkstraRouting::createGraph()
{
int i,j,max,u,v,w;
cout<<"\n Enter the number of nodes: ";
cin>>N;
for(i=0;i<=N;i++)
{
for(j=0;j<=N;j++)
{
graph[i][j]=0;
}
}
max=N*(N+1);
for(i=0;i<max;i++)
{
cout<<"\n Enter Edge and Weight : ";
cin>>u>>v>>w;
if(u==-1)
break;
else
{
graph[u][v]=w;
graph[v][u]=w;
}
}
}

int DijkstraRouting::minDistance()
{
int min = 10000, minDist;
for (int v = 0; v < N; v++)
{
if (visited[v] == false && dist[v] <= min)
{
min = dist[v];
minDist = v;
}
}
return minDist;
}

void DijkstraRouting::printPath(int j)
{
if (parent[j]==-1)
return;
printPath(parent[j]);
cout<<j<<" ";
}

void DijkstraRouting::dijkstra()
{
int src;
cout<<"\n Enter the source node: ";
cin>>src;
for (int i = 0; i < N; i++)
{
parent[0] = -1;
dist[i] = 10000;
visited[i] = false;
}
dist[src] = 0;
for (int count = 0; count < N-1; count++)
{
int u = minDistance();
visited[u] = true;
for (int v = 0; v < N; v++)
if (!visited[v] && graph[u][v] && dist[u] + graph[u][v] < dist[v])
{
parent[v] = u;
dist[v] = dist[u] + graph[u][v];
}
}
cout<<"\n Src->Dest\tDistance\tPath";
for (int i = 1; i < N; i++)
{
cout<<"\n "<<src<<"->"<<i<<"\t\t"<<dist[i]<<"\t\t"<<src<<" ";
printPath(i);
cout<<endl;
}
}
int main()
{
DijkstraRouting d;
d.createGraph();
d.dijkstra();
return 0;
}

You might also like