COMPUTER NETWORKS
LAB FILE
SUBMITI'ED TO: SUBMITTED BY:
ASSISTANT PROFESSOR PULKIT TANWAR
R.K. YADAV DTU/2K14/lT/OSO
Scanned by CamScanner
S.NO NAME OF EXPERIMENT DATE
ff”
1. FINDCLASS.NETWORKID,HOSTID OF 7 )b
islvrax'w ADDRESS 3’ 8" -°
‘ 2. ‘ SIMULATE STOP AND WAIT ~
ALGORITHM 7,2 — 8 ’ 20} L
3. SIMULATE suome WINDOW r (,
PROTOCOL OJ ’ C1 ’ 20'
4. SIMULATE GO BACK N PROTOCOL
ll , <1 ’ Jul 4:
s. SIMUIATE DISTANCE VECTOR
ALGORITHM
1‘1 ’9 ’20” /
V: 0
6. IMPLEMENT DIJIKSTRA’S SHORTEST
_ PATH ALGORITHM ‘3“ \!\\\}.
- _ - 34 o «2014 /
u “
7. IMPLEMENT BELLMAN FORD _
ALGORITHM -
H —] oh’golé- y
8. IMPLEMENT FLOYD WARSHAL FOR - '
SINGLE SOURCE SHORTEST PATH 3 I _ \ D *20) '4‘ V
Scanned by CamScanner
Experiment: 1
Aim :- To find out the class to which an IP address belongs.
#include<iostream>
#include<coni0.h>
using namespace std;
int main()
int ipl, ip2, ip3, ip4;
cout<<"Enler an IP address: ";
ci11>> ipl‘,
cout<<'.';
cin>> ip2;
cout<< ‘.';
cin>> ip3;
cout<< '.';
cin>> ip4;
if( ip1>=0 && ip1<128)
{
cout<< "CLASS A'_'<<end1;
cout<<"NID = “<<ip1<<end1;;
cout<<"HID = "<<ip2<<'.'<<ip3<<‘.'<<ip4;
if( ip1>=128 && ip1<192)
{
cout<< "CLASS B"<<endl;;
cout<<"NID = "<<ip1<<‘.'<<ip2<<endl;;
cout<<“HID = "<<ip3<<‘.‘<<ip4;
if( ip1>=192 && ip1<224 )
{
cout<< "CLASS C"<<end1;;
cout<<"NID = "<<ip1<<'.'<<ip2<<‘.'<<ip3<<endl;;
cout<<"I-IID = "<<ip4; /”
} /
if( ip1>=224 && ip1<240)
cout<< "CLASS D"<<endl;;
cout<<"Mu1ticast Address";
}
if( ip1>=240 && ip1<256> .
Scanned by CamScanner
1
cout<< "CLASS E"<<endl;;
cout<<"Rcscrvcd Address";
getcho;
return 0;
}
OUTPUT
00880:: 0.74, Cpu speed: max109% cycles, Frameskip 0, Program:
6 :\TUBB(]CB\B IN>TC
Enter an IP address: 129
Scanned by CamScanner
Experiment: 2
AIM: To implement slop tmd wail protocol.
#includc<iastrcmnh>
flincludc<graphicsh>
#includc<conio.lP
#includc<dos.h>
void pt 10
{ int i;
linc(600,20£00,400);
linc(0.20.0,400);
f0r(i=0:i<580;i++)
{ 1inc(0,20,0‘400);
linc(600,20,600‘400);
circ1c(20+i,100+(i/l 0),20);
dclay(2);
cleardeviceo;
void p120
1ine(0,20,0,400);
1ine(600,20,600,400);
int kjj;
f01'(i=580j=580;i>0;i--J++)
{ 1ine(0,20,0,400);
line(600,20,600,400);
circle(20+i,100+(j/10),20);
delay(2);
cleardeviceO;
void main()
{ int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
cout<<"Case 1: Succesfull transmission";
delay(1000);
PUO;
PQO;
cout<<"Case 2 : Lost packet";
delayUOOO); /
int i;
1ine(0,20,0,400);
line(600,20,600,400);
for(i=0;i<480;i++)
{ 1ine(0,20,06,:1)80260)
line 600,20, , S
circl(e(20+i,100+(i/10)’20);
delay(2)§
cleardewceO;
Scanned by CamScanner
dclay(]000);
mm;
max
cout<<"Case 3: Lost acknowledgement";
dclay(]000);
mmx
linc(0,20,0,400);
1inc(600,20,600,400);
int 1‘11;
for(i=580.j=580;i>100;i—-J-H)
{ line(0,20‘0,400);
linc(600,20,600,400);
circle(20+i,100+G/10),20);
dclay(2);
cleardeviceO;
delay(100);
p11();
max
getchO;
OUTPUT
Scanned by CamScanner
3.SLIDING WINDOW PROTOCOL:GO BACK N
#include<iostrcam.h>
#include<dos.h>
#include<c0nio.h>
#includc<graphics.h>
void main()
{
int gm=DETECT,gd,crror,n;
initgraph(&gm,&gd,"C:\\TURBOC3\\BGI“);
int k=0,l,ixi;
cout<<"succcssful transmission";
gclchO;
f0r(j=0;j<125;j+=25)
{ cleardcviccO;
for(i=25;i<150;i+=25)
{ rectangle(i,25,25+i,50);
rectangle(i+100,125,125+i,150);
}
1:0;
1ine(37+j,50,137+j,] 25);
delay(400);
//linc(62+j,50,97+j,75); "
k=j; \/
while(k>=0)
{ floodfillU37+k,129,Wl-IITE);
Scanned by CamScanner
k-=25;
}
delay(400);
}
cleardeviceO;
cout<<"resend packet";
getcho;
for(j=0;j<100;j+=25)
{ cleardeviceO;
f0r(i=25;i<150;i+=25)
{ rectangle(i,25,25+i,50);
rectangle(i+100,] 25,125+i,] 50);
}
I 1:0;
1ine(37+j,50,1 37+j, 125);
delay(400);
//1ine(62+j,50,97+j ,75);
// if(j>=75)
//
k=j;
if(j >25)
k=25;
whi1e(k>:0)
{ floodfill(137+k,129,WHITE);
-=25;
Scanned by CamScanner I
I
!
delayHOO):
}
f0rLi=50;i<120:j+=25)
{ Cleardcviceo:
tbfii=35;i<150:i+=25)
{ rectangle(i.25.25+i,50);
rectangle(i+100,125,125+i,150);
}
l=0;
line(37+j,50,137+j,125);
delay(400);
//line(62+j,50,97+j,75);
// if0>=75)
L=j;
//if(i>25)
// k=25;
while(k>=0)
{ floodfill(137+k,129,WHITE);
k—=25;
} /
delay(400);
}
getchO;
Scanned by CamScanner
OUTPUT:
Scanned by lCamScanher
4.SLIDING WINDOW PRUTOCOLSELECTIVE REPEAT
#includc’iostrcarnh/
fiincludensh»
fincludc’wniohz
#includc/graphics.h>
void main()
{
int gm=DETECT,gd,error,n;
initgraph(&gm,&gd,"C:k TURBOCBZ 86]");
int k=0,l,ij;
cout<<"sucoessful tranmission”;
gctcho;
for(i=0;j<125;j+=25)
{ cleardeviceO;
for(i=25;i<150;i+=25)
{ rectangle(i,25,25+i,50);
rectangle(i+100,125,125+i,150);
}
1:0;
Iine(37+j,50, l37+j,125); /
delay(400); \/
//line(62+j,50,97+j,75);
k=j;
while(k>=0)
{ floodfill(137+k,129,WHlTE);
Scanned by CamScanner
k-=25;
}
delay(400);
}
cleardeviceO;
cout<<"resend packet";
gelchO;
f0r(j=0;j<lOO;j+=25)
{ cleardeviceO;
for(i=25;i<150;i+=25)
{ rectangle(i,25,25+i,50);
rectangle(i+100, 125,125+i, 150);
}
Iine(37+j,50,137+j,125);
delay(400);
//1ine(62+j,50,97+j,75);
// if(j>=75)
//
k=j;
1f(j==25)
k=0;
while(k>=0)
{ if(k==25)
{ -=25;
}
Scanned by CamScanner
//
//
floodfill(1 37+k,129,WHITE);
-=25;
}
delay(400);
j=25;
cleardeviceo;
for(i=25;i<150;i+=25)
{ rectangle(i,25,25+i,50);
rectangle(i+100,125,125+i,150);
}
line(37+j,50,130+j,125);
delay(400);
//1ine(62+j,50,97+j,75);
ifG>=75)
k=j+50;
//if(j>25)
// k=25;
while(k>=0)
{ floodfill(130+k,129,WHITE);
k-=25;
}
delay(400);
j=125;
Scanned by CamScanner
cleanlm in“);
thui 355430;“ 15}
Q lm‘tnnglqifiilfi t LSD):
rwmnglcu1-100.IJSJZS'Q‘LISO);
1‘
linc(3?a‘j.50.1204il‘25):
dclayHOO):
Jiuqfilfii,50.97fi.75);
ifijz=75)
k=j-S:
3.3311125)
21' F25;
while(k>=0)
{ floodfi1l(120+k,129,WHlTE);
k225; }
delay(400);
getch0;}
Scanned by CamScanner
OUTPUT:
Scanned by CamScanher
Experiment: 5
AIM: To implement Djikstra Algorithm.
#include <iostream>
#includc <1nap>
#include <queue>
#include <vector>
#include <cmath>
#include <cstri11g>
#include <cli1nits> \
#include <set>
#include <algorithm>
#define inf LONG_MAX
#deflne MAX 100010
#define pb push_back
#define mp make_pair
#define f first
#define 5 second
#define RESET(a,b) memset(a, b, sizeof 21)
#define PH pair<ll,ll>
typedef long long int 11;
using namespace std;
vector <PII> 1ist[MAX+5];
bool Visited[MAX+5];
11 dist[MAX+5];
void initializeO
{
for(ll i=0;i<MAX;i++)
{
visited[i]=false;
dist[i]=inf;
}
}
void Dijkstrafll x)
{
priority_queue<PII,Vect0r<PII>,greater<PII> > q;
PH P;
dist[x]=0;
q-push(mp(0,X));
While(!q.empty())
{
P=q.t0p0;
q-p0p0;
Scanned by CamScanner
11 k:P.s;
if(visited[k]=true)
continue;
for(ll i=0;i<list[k].size();i—|—!—)
{
if(!visited[list[k][i].s]&&dist[list[k][i].s]>dist[k]+list[k][i].l‘)
{
dist[list[k][i].s]=dist[k]+1ist[k][i].f;
q.push(make _pair(dist[1ist[k][i].s],1ist[k][i].s));
}
}
visited[k]=true;
}
}
int main() {
boost;
11 i,x,y,w,n,m,j;
cin>>n>>m;
for(i=1 ;i<:m;i++)
{
cin>>x>>y>>w; ,
1ist[X]~pb(mp(W,y));
1ist[y]-pb(mp(W:X)); /
}
for(i=1;i<=n;i++) ,
{ cout<<"TABLE FOR "<<i<<end1;
iriitializeO;
Dijkstrafi); u
11 mini=le9;
f0r0=1;j<=n;i++)
cout<<i<<" "<<j<<" "<<dist[j]<<endl;
cout<<endl;
}
return 0;
}
Wawark is represented as a graph)
6 8 (No. of vertices , No. of edges)
1 2 9 (Starting vertex , Ending vertex , Weight)
1 4 10
2 4 10
2 3 2
3 4 4
Scanned by CamScanner
s;‘
‘r V
:C‘__
559
OYTPUT:
-- " ,-x'«
5122:12373\-‘;LE::\?:ex.}ixnsunnxyagnfi
“Y" 1"!
.ADLEFUAL
tn
‘1
4-.“
:W
~‘_
Ht
:3?
5-‘
_ ,_ fi~
LABL::OK-
‘78
_~;
{-11
1"
_‘_.0
7:7:
:51}
W
b:
1"
WI
’11
O
W
U)
3111
322
330
:14
3511
3611
men: FOR 4 H
41 10
426
4
414
440
+57
4615
:2.
TABLEFORS
5117
5213
5312
547
550
563
TABLEFORG
6122
6213
Scanned by CamScanner
(11“
6415
(‘58
(‘00
Scanned by CamScanner
Experiment: 6
AIM: To implement Bellman Ford Algorithm.
#includc <stdio.h>
#includc <stdlib.h>
#include <string.h>
#include <limits.h>
#include<iostrcam>
using namcspacc std;
struct Edge
{
int src, dest, weight;
};
struct Graph
{ im V, E;
struct Edgc* edge;
};
struct Graph“ createGraphUnt V, int E)
{
strucl Graph* graph =
(struct Graph*) malloc( sizeof(struct Graph) );
graph—>V = V;
graph—>E = E;
graph->edge =
(struct Edge*) malloc( graph->E * sizeof( struct Edge ) );
return graph;
}
void printArrUnt dist[], int n)
{
printf("Vertcx Distance from Source\n"); /
for (int i = 0; i < n; ++i)
printf("%d \t\t %d\n", i, dist[i]);
}
void BellmanFord(strucl Graph“ graph, int src)
{
int V = graph->V;
int E = graph->E;
int disl[V];
for(inl i = 0; i<V; i++)
dist[i] = INT_MAX;
dist[src] = O;
for(int i = l; i<=V-1; i++)
{
for (intj = 0;j < 13; i++)
{
Scanned by CamScanner
int u = graph->cdge[j].src;
int v = graph->cdge[j].dest;
int weight = graph—‘zcdgefilwcight;
if(disqu] 3Z1NT_MAX && dist[u] + weight < dist[v])
dist[v] = dist[u] + weight;
}
}
for (int i = 0; i < E; i++)
{
int u = graph->cdge[i].src;
inl v = graph->cdge[i].dcst;
int weight = graph—>cdge[i].weight;
if(dist[u] I= INT_MAX && dist[u] + weight < dist[v])
printf("Graph contains negative weight cycle");
}
printArr(dist, V);
return;
}
int main()
{
int V ;
int E ;
cin>>V>>E;
struct Graph* graph = createGraph(V, E);
for(int i=0;i<E;i-+-‘r)
{cin>>graph—>edge[i].src;
cin>>graph—>edge[i] . dest;
cin>>graph->edge[i].weight;}
BellmanFord(graph, 0);
return 0;
H.»
Scanned by CamScanner
Zlmnlement Distance Vector Routing Algorithm
Code :
#include<iostream>
using namespace std;
#include<stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}IT[10];
int main()
{
int dmat[20] [20];
int n,i,j,k,count=0;
printf("\nEnter the number of nodes : ");
scanf("%d",&n);
printf("\nEnter the cost matrix :\n");
for(i=0;i<n;i++)
f0r(j=0;j <n ;j++) /
{
scanf("%d",&dmat[i][i]);
~dmat[i] [i]=0;
rt[i]‘dist[j]=dmat[i][j];
rt[i]-fr0m[i]=j;
Scanned by CamScanner
do
count=0;
f0r(i=0;i<n;i++)
f0r0=0;j<n;j++)
for(k=0;k<n;k++)
if(rt[i].dist[j]>dmat[i][k]+rt[k].dist[j])
{
rt[i].distfi]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
count++;
}
}whi1e(count!=0);
f0r(i=0;i<n;i++)
{
printf("\n\nState value for router %d is \n",i+1);
f0r0:0;j<n;j++)
{
printf("\t\nn0de %d Via %d Distance%d",j+1,rt[§].from[i]+1,tt[i]-dist[i]);
}
printf("\n\n");
Scanned by CamScanner
iEnter the nunber of nodes : 4
Enter the number of nodes : 3 ,
1Enter the cost na:r1x :
Enter the cost: matrix :
1 2 3
4 S 6
7 8 9
jstate value for router
. :node 1 via 1 Distancee
State value for router 1 IS
.node 3 via 3 Distances
node 1 via 1 Distances 9"“ 4 V“ 2 “Stem“
node 2 via 2 Distancez .sunevmueforrmner
node 3 via 3, Distance3
, vla 1 Distance;
vla 2 Distances
State value for router ‘ : via 4 Distance:
via 4 Distance:
u
x
.
l
u
n
n
.
.
.
.
.
n
‘
n
n
x
u
l
.
a
u
u
.
n
v
a
n
u
n
n
u
u
»
.
node 1 via 1 Distancea.
node 2 via 2 Distancee
node 3 via 3 Distances
State value for router
node 1 via 1 Distance?
node 2 via 2 Distances
node 3 via 3 Distanceo
Scanned by CamScanner
8.FLOYD—WARSHAL SINGLE SOURCE SHORTEST PATH
#includc<stdio.h>
#define v 5
#define rNF 99999
void printSolulion(int dist[][V]);
void floydWarshall (1m graph[][V])
{
int dist[V][V], i, j, k;
for (i = 0; i < v; M)
for(i =0;j <V;j++)
distmm = graphUHj];
for (k : 0; k < v; k++)
{
for (i = 0; i < V; i++)
{
for(j = 0;j < V;j++)
{
if (dist[i][k] + dist[k][i] < dist[i][j])
dist[i]|'j] = dist[i][k] + dist[k][i];
printSolution(dist);
void printSoluti0n(int distfl[V])
Scanned by CamScanner
|H‘intl'("thu SIHH‘ICSI pull] [0 all the vcrticcs from the source is
for (int i 0;i"’~V;i-I 4-)
i
printl1"\nwhun (11c snurcu vertex is: %d",i);
for (int j ();_i ‘1‘ VLi'H‘)
t
1
if(disflflljj == INF)
p1‘intf("°/o7s", "INF");
else
printf("%7d", dist[i][j]);
}
printf("\n");
-,-I
int main()
{
int graph[V][V] = { {0, 1, 4,1NF,INF},
{INF, 0, 3, 2,2},
{[NF,INF,O, INF,INF},
{INF,1, 5, O,INF},
{INF,INF,INF,3,0}
};
floydWarshall(graph);
return 0;}
: ll);
Scanned by CamScanner
OUTPUT:
3&1 E:Knctv:c:kIahkshtglesourceshorieshtath.exe
the shortest path to all the vertices from the source is:
xhen the source vertex is: e e 1 4
when the source vertex is:
xhen the source vertex is: .
xhen the source vertex is:
xhen the source vertex Ea:
?vocess exi€ed after 8.67955 secends aifh return 03302 8
Dress any key to centinue . . .
Scanned by CamScanner