Вы находитесь на странице: 1из 26

experiment 1: example

set ns [new Simulator]


set nf [open ex1.nam w]
$ns namtrace-all $nf
set tf [open ex1.tr w]
$ns trace-all $tf
set n1 [$ns node]
set n2 [$ns node]
$ns duplex-link $n1 $n2 10mb 10ms DropTail
$ns queue-limit $n1 $n2 20
set tcp0 [new Agent/TCP]
$ns attach-agent $n1 $tcp0
set ftp0 [new Application/FTP]
$ftp0 set packetsize_ 500
$ftp0 set interval_ 0.005
$ftp0 attach-agent $tcp0

set Sink1 [new Agent/TCPSink]


$ns attach-agent $n2 $Sink1
$ns connect $tcp0 $Sink1
proc finish {} {
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam ex1.nam &
exit 0
}
$ns at 1.0 "$ftp0 start"
$ns at 3.0 "finish"
$ns run
experiment 2: tcp and tcp

set ns [new Simulator]


set nf [open t1.nam w]
$ns namtrace-all $nf
set tf [open t1.tr w]
$ns trace-all $tf
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

$ns duplex-link $n0 $n2 0.005mb 10ms DropTail


$ns queue-limit $n0 $n2 2
$ns duplex-link $n1 $n2 0.005mb 10ms DropTail
$ns queue-limit $n1 $n2 2
$ns duplex-link $n2 $n3 0.05mb 10ms DropTail
$ns queue-limit $n2 $n3 5

set tcp0 [new Agent/TCP]


$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp0 set packetsize_ 500
$ftp0 set interval_ 0.005
$ftp0 attach-agent $tcp0

set tcp1 [new Agent/TCP]


$ns attach-agent $n1 $tcp1
set ftp1 [new Application/FTP]
$ftp1 set packetsize_ 500
$ftp1 set interval_ 0.005
$ftp1 attach-agent $tcp1

set sink0 [new Agent/TCPSink]


$ns attach-agent $n3 $sink0
$ns connect $tcp0 $sink0

set sink1 [new Agent/TCPSink]


$ns attach-agent $n3 $sink1
$ns connect $tcp1 $sink1

proc finish { } {
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam t1.nam &
exit 0
}

$ns at 1.0 "$ftp0 start"


$ns at 1.0 "$ftp1 start"
$ns at 3.0 "$ftp0 stop"
$ns at 3.0 "$ftp1 stop"
$ns at 3.0 "finish"
$ns run

awk file:

BEGIN{
c=0; d=0; c1=0; d1=0;
}

{
if($1=="r" && $4=="3" && $5=="tcp" && $6=="1040")
{
c++;
}
if($1=="d" && $4=="3" && $5=="tcp" && $6=="1040")
{
d++;
}
if($1=="r" && $4=="2" && $5=="tcp" && $6=="1040")
{
c1++;
}
if($1=="d" && $4=="2" && $5=="tcp" && $6=="1040")
{
d1++;
}
}
END{
printf("no of packets recieved at n3: %d\n",c);
printf("no of packets recieved at n2: %d\n",c1);
printf("no of packets dropped at n3: %d\n",d);
printf("no of packets dropped at n2: %d\n",d1);
}
experiment 3: tcp and udp

set ns [new Simulator]


set nf [open tu4n.nam w]
$ns namtrace-all $nf
set tf [open tu4n.tr w]
$ns trace-all $tf

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

$ns duplex-link $n0 $n2 0.05mb 10ms DropTail


$ns queue-limit $n0 $n2 2
$ns duplex-link $n1 $n2 0.05mb 10ms DropTail
$ns queue-limit $n1 $n2 2
$ns duplex-link $n2 $n3 0.05mb 10ms DropTail
$ns queue-limit $n2 $n3 2

set tcp0 [new Agent/TCP]


$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp0 set packetsize_ 500
$ftp0 set interval_ 0.005
$ftp0 attach-agent $tcp0

set sink0 [new Agent/TCPSink]


$ns attach-agent $n3 $sink0
$ns connect $tcp0 $sink0

set udp0 [new Agent/UDP]


$ns attach-agent $n1 $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetsize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0

set null0 [new Agent/Null]


$ns attach-agent $n3 $null0
$ns connect $udp0 $null0

proc finish { } {
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam tu4n.nam &
exit 0
}

$ns at 1.0 "$ftp0 start"


$ns at 3.0 "finish"
$ns at 1.0 "$cbr0 start"
$ns at 3.0 "finish"
$ns run

awk file:

BEGIN{
c1=0;c2=0;d1=0;d2=0;tc=0;uc=0;sent=0;
}

{
if($3=="0" && $5=="tcp")
{
tc++;
}
if($3=="1" && $5=="cbr")
{
uc++;
}
if($1=="r" && $4=="2" && $5=="tcp")
{
c1++;
}
if($1=="d" && $3=="2" && $5=="tcp")
{
d1++;
}
if($1=="r" && $4=="2" && $5=="cbr")
{
c2++;
}
if($1=="d" && $3=="2" && $5=="cbr")
{
d2++;
}
sent=tc+uc;
}
END{
printf("tcp packets sent from n0: %d\n",tc);
printf("udp packets sent from n0: %d\n",uc);
printf("tcp packets recieved at n3: %d\n",c1);
printf("tcp packets dropped at n2: %d\n",d1);
printf("udp packets recieved at n3: %d\n",c2)
printf("udp packets dropped at n2: %d\n",d2);
print("total packets sent: %d\n",sent);
}
experiment 4: ethernet LAN

set ns [new Simulator]


set nf [open lan1.nam w]
$ns namtrace-all $nf
set tf [open lan1.tr w]
$ns trace-all $tf

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
set n6 [$ns node]
set n7 [$ns node]

$n1 label "Source/UDP"


$n3 label "Error Node"
$n7 label "Destination"

$ns make-lan "$n0 $n1 $n2 $n3" 1000mb 10ms LL Queue/DropTail Mac/802_3
$ns make-lan "$n4 $n5 $n6 $n7" 1000mb 10ms LL Queue/DropTail Mac/802_3
$ns duplex-link $n3 $n4 100mb 10ms DropTail

set err [new ErrorModel]


$ns lossmodel $err $n3 $n4
$err set rate_ 0.2

set udp0 [new Agent/UDP]


$ns attach-agent $n1 $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetsize_ 500
$cbr0 set interval_ 0.001
$cbr0 attach-agent $udp0

set null0 [new Agent/Null]


$ns attach-agent $n7 $null0
$ns connect $udp0 $null0

proc finish {} {
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam lan1.nam &
exit 0
}

$ns at 1.0 "$cbr0 start"


$ns at 3.0 "$cbr0 stop"
$ns at 4.0 "finish"
$ns run

awk file:

BEGIN{
c=0; d=0;
}
{
if($1=="r" && $4=="7" && $5=="cbr")
{
c++;
}
if($1=="d" && $3=="3" && $5=="cbr")
{
d++;
}
}
END{
printf("no of packets recieved at n7 : %d\n",c);
printf("no of packets dropped at n3 : %d\n",d);
}
experiment 5: ethernet lan congestion window

set ns [new Simulator]


set nf [open ecw.nam w]
$ns namtrace-all $nf
set tf [open ecw.tr w]
$ns trace-all $tf

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]

$ns make-lan "$n0 $n1 $n2 $n3" 100Mb 10ms LL Queue/DropTail Mac/802_3
$ns duplex-link $n3 $n4 10Mb 10ms DropTail
$ns queue-limit $n3 $n4 25

set tcp0 [new Agent/TCP]


$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp0 set packetsize_ 1500
$ftp0 set interval_ 0.005
$ftp0 attach-agent $tcp0

set sink0 [new Agent/TCPSink]


$ns attach-agent $n2 $sink0
$ns connect $tcp0 $sink0

set tcp1 [new Agent/TCP]


$ns attach-agent $n1 $tcp1
set ftp1 [new Application/FTP]
$ftp1 set packetsize_ 1500
$ftp1 set interval_ 0.005
$ftp1 attach-agent $tcp1

set sink1 [new Agent/TCPSink]


$ns attach-agent $n3 $sink1
$ns connect $tcp1 $sink1

$tcp0 trace cwnd_


$tcp1 trace cwnd_

set file1 [open file1.tr w]


$tcp0 attach $file1
set file2 [open file2.tr w]
$tcp1 attach $file2

proc finish { } {
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam ecw.nam &
exit 0
}

$ns at 0.1 "$ftp0 start"


$ns at 5.1 "$ftp0 stop"
$ns at 1.1 "$ftp1 start"
$ns at 6.1 "$ftp1 stop"
$ns at 7.1 "$ftp0 start"
$ns at 9.0 "$ftp0 stop"
$ns at 6.2 "$ftp1 start"
$ns at 9.2 "$ftp1 stop"
$ns at 10.0 "finish"
$ns run

awk file:

BEGIN{
}
{
if($6 == "cwnd_")
printf("%f\t %f\t \n",$1,$7);
}
END{
}

commands: awk -f ecw.awk file1.tr>a1


awk -f ecw.awk file2.tr>a2
xgraph a1 a2
experiment 6: link state routing algorithm

set ns [new Simulator]


set nf [open lsr.nam w]
$ns namtrace-all $nf
set tf [open lsr.tr w]
$ns trace-all $tf

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]

$ns duplex-link $n0 $n1 100Mb 10ms DropTail


$ns queue-limit $n0 $n1 50

$ns duplex-link $n0 $n2 100Mb 10ms DropTail


$ns queue-limit $n0 $n2 50

$ns duplex-link $n2 $n3 100Mb 10ms DropTail


$ns queue-limit $n2 $n3 50

$ns duplex-link $n1 $n3 100Mb 10ms DropTail


$ns queue-limit $n1 $n3 50

$ns duplex-link $n3 $n4 100Mb 10ms DropTail


$ns queue-limit $n3 $n4 50

$ns duplex-link $n0 $n3 100Mb 10ms DropTail


$ns queue-limit $n0 $n3 50

$ns duplex-link $n1 $n2 100Mb 10ms DropTail


$ns queue-limit $n1 $n2 50

$ns cost $n0 $n1 2


$ns cost $n0 $n2 1
$ns cost $n0 $n3 3

$ns cost $n1 $n0 2


$ns cost $n1 $n2 2
$ns cost $n1 $n3 3

$ns cost $n2 $n1 2


$ns cost $n2 $n0 1
$ns cost $n2 $n3 1

$ns cost $n3 $n2 1


$ns cost $n3 $n1 3
$ns cost $n3 $n0 3

$ns cost $n3 $n4 2


$ns cost $n4 $n3 2

set udp0 [new Agent/UDP]


$ns attach-agent $n0 $udp0
set null0 [new Agent/Null]
$ns attach-agent $n4 $null0
$ns connect $udp0 $null0
$udp0 set packetsize_ 1500
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$cbr0 set packetsize_ 1000
$cbr0 set rate_ 1Mb
$cbr0 set random_ null0

proc finish { } {
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam lsr.nam &
exit 0
}

$ns at 1.0 "$cbr0 start"


$ns at 5.0 "$cbr0 stop"
$ns rtproto LS
$ns at 5.1 "finish"
$ns run

awk file:

BEGIN {
n1=0;n2=0;n3=0;n4=0;
}
{
if($1 == "r" && $4 == "1")
{
n1++;
}
if($1 == "r" && $4 == "2")
{
n2++;
}
if($1 == "r" && $4 == "3")
{
n3++;
}
if($1 == "r" && $4 == "4")
{
n4++;
}
}
END {
printf("no of packets at n1: %d\n",n1);
printf("no of packets at n2: %d\n",n2);
printf("no of packets at n3: %d\n",n3);
printf("no of packets at n4: %d\n",n4);
}
experiment 7: ESS wireless LAN

set ns [new Simulator]


set tf [open wl.tr w]
$ns trace-all $tf
set topo [new Topography]
$topo load_flatgrid 1000 1000
set nf [open wl.nam w]
$ns namtrace-all-wireless $nf 1000 1000

$ns node-config -adhocRouting AODV \


-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail \
-ifqLen 50 \
-phyType Phy/WirelessPhy \
-channelType Channel/WirelessChannel \
-propType Propagation/TwoRayGround \
-antType Antenna/OmniAntenna \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON

create-god 3
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]

$n0 set X_ 80
$n0 set Y_ 80
$n0 set Z_ 0

$n1 set X_ 50
$n1 set Y_ 50
$n1 set Z_ 0

$n2 set X_ 90
$n2 set Y_ 90
$n2 set Z_ 0

$ns at 0.1 "$n0 setdest 50 50 15"


$ns at 0.1 "$n1 setdest 100 100 25"
$ns at 0.1 "$n2 setdest 600 600 25"

set tcp0 [new Agent/TCP]


$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
set sink0 [new Agent/TCPSink]
$ns attach-agent $n1 $sink0
$ns connect $tcp0 $sink0
set tcp1 [new Agent/TCP]
$ns attach-agent $n1 $tcp1
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
set sink1 [new Agent/TCPSink]
$ns attach-agent $n2 $sink1
$ns connect $tcp1 $sink1

proc finish { } {
global ns nf tf
$ns flush-trace
close $tf
close $nf
exec nam wl.nam &
exit 0
}

$ns at 5 "$ftp0 start"


$ns at 5 "$ftp1 start"
$ns at 100 "$n1 setdest 550 550 15"
$ns at 190 "$n1 setdest 70 70 15"
$ns at 250 "finish"
$ns run

awk file:

BEGIN {
c1=0; c2=0; p1=0; p2=0; t1=0; t2=0;
}
{
if($1 == "r" && $3 == "_1_" && $4 == "AGT")
{
c1++;
p1=p1+$8;
t1=$2;
}
if($1 == "r" && $3 == "_2_" && $4 == "AGT")
{
c2++;
p2=p2+$8;
t2=$2;
}
}
END {
printf("Throughput from n0 to n1 : %f
Mbps\n",((c1*p1*8)/(t1*1000000)));
printf("Throughput from n1 to n2 : %f
Mbps\n",((c2*p2*8)/(t2*1000000)));
}
experiment 8: bit stuffing and character stufffing

bit stuffing

#include<stdio.h>
#include<string.h>
void main()
{
int a[20],b[30],i,j,k,count,n;
printf("enter frame length\n");
scanf("%d",&n);
printf("enter the input frame\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
i=0;
count=1;
j=0;
while(i<n)
{
if(a[i]==1)
{
b[j]=a[i];
for(k=i+1;a[k]==1&&k<n&&count<5;k++)
{
j++;
b[j]=a[k];
count++;
if(count==5)
{
j++;
b[j]=0;
}
i=k;
}
}
else
{
b[j]=a[i];
}
i++;
j++;
count=1;
}
printf("after stuffing the frame : ");
printf("01111110\t");
for(i=0;i<j;i++)
printf("%d",b[i]);
printf("\t01111110\n");
}
character stuffing:

#include<stdio.h>
#include<string.h>
void main()
{
char a[30],fs[50]=" ",t[3],sd,ed,x[3],y[3],s[3],d[3];
int i,j,p=0,q=0;
printf("enter character to be stuffed:\n");
scanf("%s",a);
printf("enter a character that represents sd:\n");
scanf("%s",&sd);
printf("enter a character that represents ed:\n");
scanf("%s",&ed);
x[0]=s[0]=s[1]=sd;
x[1]=s[2]='\0';
y[0]=d[0]=d[1]=ed;
y[1]=d[2]='\0';
strcat(fs,x);
for(i=0;i<strlen(a);i++)
{
t[0]=a[i];
t[1]='\0';
if(t[0]==sd)
strcat(fs,s);
else
if(t[0]==ed)
strcat(fs,d);
else
strcat(fs,t);
}
strcat(fs,y);
printf("after stuffing : %s\n",fs);
}
experiment 9: stop and wait & sliding window

stop and wait:

#include<stdio.h>
#include<stdlib.h>
#define RTT 4
#define TIMEOUT 4
#define TOT_FRAMES 7
enum {NO,YES}ACK;
int main()
{
int wait_time,i=1;
ACK=YES;
for(;i<=TOT_FRAMES;)
{
if(ACK==YES && i!=1)
{
printf("\n SENDER : ACK for Frame %d Received \n",i-1);
}
printf("\n SENDER : Frame %d sent,Waiting for ACK......\n",i);
ACK=NO;
wait_time=rand() % 4+1;
if(wait_time==TIMEOUT)
{
printf("\n SENDER : ACK not received for Frame %d => TIMEOUT
Resending Frame......\n",i);
}
else
{
sleep(RTT/2);
printf("\n Received : Frame %d received , ACK sent \n",i);

printf("______________________________________________________________
\n");
ACK=YES;
sleep(RTT/2);
i++;
}
}
return 0;
}

sliding window:

#include <stdio.h>
#define RTT 5
int main ()
{
int w,i,f,frames [50];
printf("Enter the window size :\n");
scanf("%d",&w);
printf("Enter the no of frames to trasmit :\n");
scanf("%d",&f);
printf("Enter %d frames:\n",f);
for(i=1;i<=f;i++)
scanf("%d",& frames[i]);
printf("After sending %d frames at each stage sender waits for ACK
\n",w);
printf("Sending frames in the following manner... \n\n");
for(i=1;i<=f;i++)
{
if((i%w)==0)
{
printf("%d \n",frames [i]);
printf("ACK of above frames sent is recieved by sender \n");
}
else
printf("%d \n",frames [i]);
}
if(f%w!=0)
printf("ACK of above frames sent is recieved by sender\n");
return 0;
}
experiment 10:crc-ccitt

#include <stdio.h>
#include <string.h>
#define N strlen(g)
char t[28],cs[28],g[]="10001000000100001";
int a,i,j;
void xor()
{
for(j=1;j<N;j++)
{
cs[j]=((cs[j]==g[j])?'0':'1');
}
}
void crc()
{
for(i=0;i<N;i++)
cs[i]=t[i];
` do
{
if(cs[0]=='1')
xor();
for(j=0;j<N-1;j++)
cs[j]=cs[j+1];
cs[j]=t[i++];
}
while(i<=a+N-1);
}
int main()
{
printf("msg to be sent is:\n");
scanf("%s",t);
printf("____________________\n");
printf("generating polynomial :%s \n",g);
a=strlen(t);
for(i=a;i<=a+N-1;i++)
t[i]='0';
printf("____________________\n");
crc();
printf("checksum is :%s \n",cs);
for(i=a;i<=a+N-1;i++)
t[i]=cs[i-a];
printf("____________________\n");
printf("final code :%s \n",t);
printf("enter recieved msg \n ");
scanf("%s",t);
crc();
printf("remainder : %s \n",cs);
for(i=0;(i<N-1) && (cs[i]!='1');i++);
if(i<N-1)
printf("\n error detected \n");
else
printf("\n no error \n");
printf("____________________\n");
return 0;
}
experiment 11: leaky bucket

#include <stdio.h>
#define bucketsize 1000
#define n 5
#define MIN(x,y) (x>y)?y:x
int main()
{
int orate,drop=0,cap,x,count=0,inp[10]={0},i=0,nsec,ch;
printf("\n enter bucketsize: ");
scanf("%d",&cap);
printf("\n enter output rate:");
scanf("%d",&orate);
do
{
printf("\n enter the no of packets coming at second
%d:",i+1);
scanf("%d",&inp[i]);
i++;
printf("\n enter 1 to continue or 0 to quit ----------");
scanf("%d",&ch);
}while(ch);
nsec=i;
printf("\n second \t recieved \t sent \t dropped \t remained
\n");
for(i=0;count||i<nsec;i++)
{
printf(" %d ",i+1);
printf(" \t %d \t",inp[i]);
printf(" \t %d \t ",MIN((inp[i]+count),orate));
if((x=inp[i]+count-orate)>0)
{
if(x>cap)
{
count =cap;
drop=x-cap;
}
else
{
count=x;
drop=0;
}
}
else
{
drop=0;
count=0;
}
printf(" \t%d \t%d\n",drop,count);
}
return 0;
}
experiment 12: dijkstra algorithm

#include<stdio.h>
#define INFINITY 99
#define MAX 10
#define startnode 0
void dijkstra(int cost [MAX][MAX],int n);
int main ()
{
int cost [MAX][MAX],i,j,n,u;
printf("enter no. of vertices\n");
scanf("%d",&n);
printf("`enter the cost matrix :\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&cost[i][j]);
dijkstra(cost,n);
return 0;
}

void dijkstra (int cost[MAX][MAX],int n)


{
int distance[MAX],pred[MAX];
int visited[MAX],count ,mindistance,nextnode,i,j;
for(i=0;i<n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
}
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count<n-1)
{
mindistance=INFINITY;
for(i=0;i<n;i++)
if(distance[i] < mindistance && !visited[i])
{
mindistance=distance[i];
nextnode=i;
}
visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])

if(mindistance+cost[nextnode][i]<distance[i])
{

distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}
for(i=0;i<n;i++)
if(i!=startnode)
{
printf("\n distance of node %d = %d",i,distance[i]);
printf("\n path = %d",i);
j=i;
do
{
j=pred[j];
printf(" <- %d ",j);
}
while(j!=startnode);
}
}
experiment 13: distance vector algorithm

#include <stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}
rt[10];
int main()
{
int costmat[20][20],source,desti;
int nodes,i,j,k,count=0;
printf("\n Enter the number of nodes:");
scanf("%d",&nodes);
printf("\n Enter the cost matrix:\n");
for(i=0;i<nodes;i++)
{
for(j=0;j<nodes;j++)
{
scanf("%d",&costmat[i][j]);
costmat[i][i]=0;
rt[i].dist[j]=costmat[i][j];
rt[i].from[j]=j;
}
}
do
{
count=0;
for(i=0;i<nodes;i++)
for(j=0;j<nodes;j++)
for(k=0;k<nodes;k++)
if(rt[i].dist[j]>rt[i].dist[k]+rt[k].dist[j])
{
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;

count++;
}
}
while(count!=0);
for(i=0;i<nodes;i++)
{
printf("\n \n For router %d \n",i+1);
for(j=0;j<nodes;j++)
printf("\t \n nodes %d via %d Distance
%d",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
printf("\n \n");
}

Вам также может понравиться