Computer Networks Practical File: Hubhangi Bhatia 19570050 BSC Computer Science Hons Iii Semester
Computer Networks Practical File: Hubhangi Bhatia 19570050 BSC Computer Science Hons Iii Semester
Computer Networks Practical File: Hubhangi Bhatia 19570050 BSC Computer Science Hons Iii Semester
PRACTICAL FILE
SHUBHANGI BHATIA
19570050
BSc COMPUTER SCIENCE HONS
III SEMESTER
INDEX
2 WAP to simulate and implement stop and wait protocol for noisy 8
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;
}
#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;
}
frame data1;
void FromNetworkLayer(packet *);
void ToPhysicalLayer(frame *);
void ToNetworkLayer(packet *);
void FromPhysicalLayer(frame*);
void sender();
void receiver();
void WaitForEventSender(eventtype *);
void WaitForEventReceiver(eventtype *);
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 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);
}
}
}
#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;
}