You are on page 1of 47

Computer Network Laboratory-17CSL57

IMPACT COLLEGE OF ENGINEERING & APPLIED SCIENCES


Kodigehalli Post, Bangalore – 560 092
Department of Computer Science and Engineering

LAB MANUAL-2019-20
COMPUTER NETWORK LABORATORY-17CSL57
SEMESTER-05
CSE

Prepared By
SIJIN P
HoD, CSE, ICEAS

Approved By
Dr.Khaleel Ahmed
Principal, CSE, ICEAS

CSE Page 1
Computer Network Laboratory-17CSL57

PART A

1.Implement three nodes point – to – point network with duplex links between them.
Set the queue size, vary the bandwidth and find the number of packets dropped.

Design

Fig. 1a. Three nodes point to point network

Source code

//PROGRAM1
#===================================
# Simulation parameters setup
#===================================
set val(stop) 10.0 ;# time of simulation end

#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]

#Open the NS trace file


set tracefile [open pr1n2.tr w]
$ns trace-all $tracefile

#Open the NAM trace file


set namfile [open pr1n2.nam w]
$ns namtrace-all $namfile

#===================================
# Nodes Definition

CSE Page 2
Computer Network Laboratory-17CSL57
#===================================
#Create 3 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]

#===================================
# Links Definition
#===================================
#Createlinks between nodes
$ns duplex-link $n0 $n2 0.2Mb 10ms DropTail
$ns queue-limit $n0 $n2 10
$ns duplex-link $n1 $n2 0.4Mb 15ms DropTail
$ns queue-limit $n1 $n2 10

#Give node position (for NAM)


$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up

#===================================
# Agents Definition
#===================================
#Setup a TCP connection
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set sink2 [new Agent/TCPSink]
$ns attach-agent $n2 $sink2
$ns connect $tcp0 $sink2
$tcp0 set packetSize_ 1500

#Setup a TCP connection


set tcp1 [new Agent/TCP]
$ns attach-agent $n1 $tcp1
set sink3 [new Agent/TCPSink]
$ns attach-agent $n2 $sink3
$ns connect $tcp1 $sink3
$tcp1 set packetSize_ 1500

#===================================
# Applications Definition
#===================================
#Setup a FTP Application over TCP connection
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ns at 1.0 "$ftp0 start"
$ns at 6.0 "$ftp0 stop"

#Setup a FTP Application over TCP connection


set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1

CSE Page 3
Computer Network Laboratory-17CSL57
$ns at 4.0 "$ftp1 start"
$ns at 9.0 "$ftp1 stop"

#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam pr1n2.nam &
exit 0
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run

Result

Fig. 1b. Three nodes point to point network on execution

Fig. 1c. The number of packets dropped

//AWK FILE

//pr1n2.awk

BEGIN{
count=0;
}
{
if($1=="d")

CSE Page 4
Computer Network Laboratory-17CSL57

count++
}
END{
printf("The Total no of Packets Drop is :%d\n\n", count)
}

CSE Page 5
Computer Network Laboratory-17CSL57

2. Implement transmission of ping messages/trace route over a network topology consisting


of 6 nodes and find the number of packets dropped due to congestion.
Design

Fig. 2a. Ping messages over a 6-nodes network

Source code
//program2

#===================================
# Simulation parameters setup
#===================================
set val(stop) 10.0 ;# time of simulation end

#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]

#Open the NS trace file


set tracefile [open pr2n1.tr w]
$ns trace-all $tracefile

#Open the NAM trace file


set namfile [open pr2n1.nam w]
$ns namtrace-all $namfile

#===================================
# Nodes Definition
#===================================
#Create 6 nodes
set n0 [$ns node]
set n1 [$ns node]

CSE Page 6
Computer Network Laboratory-17CSL57
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]

#===================================
# Links Definition
#===================================
#Createlinks between nodes
$ns duplex-link $n0 $n4 1005.0Mb 1ms DropTail
$ns queue-limit $n0 $n4 5
$ns duplex-link $n1 $n4 50.0Mb 1ms DropTail
$ns queue-limit $n1 $n4 0
$ns duplex-link $n2 $n4 2000.0Mb 1ms DropTail
$ns queue-limit $n2 $n4 3
$ns duplex-link $n3 $n4 200.0Mb 1ms DropTail
$ns queue-limit $n3 $n4 0
$ns duplex-link $n5 $n4 1.0Mb 1ms DropTail
$ns queue-limit $n5 $n4 2

set p1 [new Agent/Ping]


$ns attach-agent $n0 $p1
$p1 set packetSize_ 50000
$p1 set interval_ 0.0001
set p2 [new Agent/Ping]
$ns attach-agent $n1 $p2
set p3 [new Agent/Ping]
$ns attach-agent $n2 $p3
$p3 set packetSize_ 30000
$p3 set interval_ 0.00001
set p4 [new Agent/Ping]
$ns attach-agent $n3 $p4
set p5 [new Agent/Ping]
$ns attach-agent $n5 $p5
Agent/Ping instproc recv {from rtt} {
$self instvar node_
puts "node [$node_ id] received answer from $from with round trip time
$rtt msec"
}
$ns connect $p1 $p5
$ns connect $p3 $p4
proc finish { } {
global ns namfile tracefile
$ns flush-trace
close $namfile
close $tracefile
exec nam lab2.nam &
exit 0
}
$ns at 0.1 "$p1 send"
$ns at 0.2 "$p1 send"

CSE Page 7
Computer Network Laboratory-17CSL57
$ns at 0.3 "$p1 send"
$ns at 0.4 "$p1 send"
$ns at 0.5 "$p1 send"
$ns at 0.6 "$p1 send"
$ns at 0.7 "$p1 send"
$ns at 0.8 "$p1 send"
$ns at 0.9 "$p1 send"
$ns at 1.0 "$p1 send"
$ns at 0.1 "$p3 send"
$ns at 0.2 "$p3 send"
$ns at 0.3 "$p3 send"
$ns at 0.4 "$p3 send"
$ns at 0.5 "$p3 send"
$ns at 0.6 "$p3 send"
$ns at 0.7 "$p3 send"
$ns at 0.8 "$p3 send"
$ns at 0.9 "$p3 send"
$ns at 1.0 "$p3 send"
$ns at 2.0 "finish"
$ns run

#Give node position (for NAM)


$ns duplex-link-op $n0 $n4 orient right
$ns duplex-link-op $n1 $n4 orient right-down
$ns duplex-link-op $n2 $n4 orient left-down
$ns duplex-link-op $n3 $n4 orient left-up
$ns duplex-link-op $n5 $n4 orient right-up

#===================================
# Agents Definition
#===================================

#===================================
# Applications Definition
#===================================

#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam pr2n1.nam &
exit 0
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"

CSE Page 8
Computer Network Laboratory-17CSL57
$ns run

Result

Fig. 2b. Packet drops over a 6-nodes network during ping

//AWK FILE

//pr2n1.awk

BEGIN { drop=0;}
{
if($1=="d" )
{
drop++;
}
}
END{
printf("Total number of %s packets dropped due to congestion
=%d\n",$5,drop);
}

CSE Page 9
Computer Network Laboratory-17CSL57

3. Implement an Ethernet LAN using n nodes and set multiple traffic nodes and plot
congestion window for different source / destination.

Design

Fig. 3a. Ethernet lan using n-nodes

Source code

#===================================
# Simulation parameters setup
#===================================
set val(stop) 10.0 ;# time of simulation end

#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]

#Open the NS trace file


set tracefile [open pr3n1.tr w]
$ns trace-all $tracefile

#Open the NAM trace file


set namfile [open pr3n1.nam w]
$ns namtrace-all $namfile

#===================================
# Nodes Definition
#===================================
#Create 6 nodes

CSE Page 10
Computer Network Laboratory-17CSL57
set n0 [$ns node]
$n0 label "src1"
$n0 color "red"
set n1 [$ns node]
set n2 [$ns node]
$n2 label "src2"
$n2 color "red"
set n3 [$ns node]
$n3 label "dest2"
$n3 color "blue"
set n4 [$ns node]
set n5 [$ns node]
$n5 label "dest1"
$n5 color "blue"
#===================================
# Links Definition
#===================================
#Createlinks between nodes
set lan [$ns newLan "$n0 $n1 $n2 $n3 $n4" 100Mb 100ms LL Queue/
DropTail Mac/802_3]
$ns duplex-link $n4 $n5 10.0Mb 1ms DropTail
$ns queue-limit $n4 $n5 50

#Give node position (for NAM)


$ns duplex-link-op $n4 $n5 orient right

#===================================
# Agents Definition
#===================================
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set tcp1 [new Agent/TCP]
$ns attach-agent $n2 $tcp1
set sink2 [new Agent/TCPSink]
$ns attach-agent $n5 $sink2
$ns connect $tcp0 $sink2
set sink3 [new Agent/TCPSink]
$ns attach-agent $n3 $sink3
$ns connect $tcp1 $sink3
#===================================
# Applications Definition
#===================================
#Setup a FTP Application over TCP connection
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ftp0 set packetSize_ 500
$ftp0 set interval_ 0.0001

#Setup a FTP Application over TCP connection

CSE Page 11
Computer Network Laboratory-17CSL57
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ftp1 set packetSize_ 600
$ftp1 set interval_ 0.001

#===================================
# Termination
#===================================
set file1 [open file1.tr w]
$tcp0 attach $file1
set file2 [open file2.tr w]
$tcp1 attach $file2
$tcp0 trace cwnd_ # must put underscore ( _ ) after cwnd and no space
between them
$tcp1 trace cwnd_

#Define a 'finish' procedure


proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam pr3n1.nam &
exit 0
}

$ns at 0.1 "$ftp0 start"


$ns at 5 "$ftp0 stop"
$ns at 7 "$ftp0 start"
$ns at 0.2 "$ftp1 start"
$ns at 8 "$ftp1 stop"
$ns at 9 "$ftp0 stop"
$ns at 9.1 "$ftp1 start"
$ns at 9.8 "$ftp1 stop"
$ns at 10 "finish"
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run

CSE Page 12
Computer Network Laboratory-17CSL57

Result

Fig. 3b. Ethernet lan on execution

Fig. 3c. Ethernet lan packet drops

//AWK FILE

//pr3n1.awk

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

CSE Page 13
Computer Network Laboratory-17CSL57

Fig. 3d. Ethernet lan packet drops

CSE Page 14
Computer Network Laboratory-17CSL57

4. Implement simple ESS and with transmitting nodes in wire-less LAN by simulation and
determine the performance with respect to transmission of packets.

Design

Fig. 4a. simple ESS over wireless LAN

Source code

#===================================
# Simulation parameters setup
#===================================
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set val(netif) Phy/WirelessPhy ;# network interface type
set val(mac) Mac/802_11 ;# MAC type
set val(ifq) Queue/DropTail/PriQueue ;# interface queue type
set val(ll) LL ;# link layer type
set val(ant) Antenna/OmniAntenna ;# antenna model
set val(ifqlen) 50 ;# max packet in ifq
set val(nn) 3 ;# number of mobilenodes
set val(rp) DSDV ;# routing protocol
set val(x) 478 ;# X dimension of topography
set val(y) 347 ;# Y dimension of topography

CSE Page 15
Computer Network Laboratory-17CSL57
set val(stop) 10.0 ;# time of simulation end

#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]

#Setup topography object


set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
create-god $val(nn)

#Open the NS trace file


set tracefile [open l4.tr w]
$ns trace-all $tracefile

#Open the NAM trace file


set namfile [open l4.nam w]
$ns namtrace-all $namfile
$ns namtrace-all-wireless $namfile $val(x) $val(y)
set chan [new $val(chan)];#Create wireless channel

#===================================
# Mobile node parameter setup
#===================================
$ns node-config -adhocRouting DSDV\
-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail\
-ifqLen 50 \
-antType Antenna/OmniAntenna \
-propType $val(prop) \
-phyType $val(netif) \
-channel $chan \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace ON \
-movementTrace ON \

#===================================
# Nodes Definition
#===================================
#Create 3 nodes
set n0 [$ns node]
$n0 set X_ 50
$n0 set Y_ 50
$n0 set Z_ 0.0
$ns initial_node_pos $n0 20
set n1 [$ns node]

CSE Page 16
Computer Network Laboratory-17CSL57
$n1 set X_ 400
$n1 set Y_ 50
$n1 set Z_ 0.0
$ns initial_node_pos $n1 20
set n2 [$ns node]
$n2 set X_ 200
$n2 set Y_ 30
$n2 set Z_ 0.0
$ns initial_node_pos $n2 20

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


$ns at 0.1 "$n1 setdest 400 50 25"
$ns at 0.1 "$n2 setdest 200 20 25"
#===================================
# Agents Definition
#===================================
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
set sink1 [new Agent/TCPSink]
$ns attach-agent $n1 $sink1
$ns connect $tcp0 $sink1
set tcp1 [new Agent/TCP]
$ns attach-agent $n1 $tcp1
set sink2 [new Agent/TCPSink]
$ns attach-agent $n2 $sink2
$ns connect $tcp1 $sink2

#===================================
# Applications Definition
#===================================
#Setup a FTP Application over TCP connection
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ns at 1 "$ftp0 start"

set ftp1 [new Application/FTP]


$ftp1 attach-agent $tcp1
$ns at 2.0 "$ftp1 start"

#Setup a FTP Application over TCP connection

$ns at 1 "$n2 setdest 100 50 25"


$ns at 50 "$n1 setdest 500 70 50"
#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile

CSE Page 17
Computer Network Laboratory-17CSL57
$ns flush-trace
close $tracefile
close $namfile
exec nam l4.nam &
exit 0
}
for {set i 0} {$i < $val(nn) } { incr i } {
$ns at $val(stop) "\$n$i reset"
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns at 250 "finish"
$ns run

Result

Fig. 4b. Wireless LAN on execution

Fig. 4c. Packet drops in wireless LAN

CSE Page 18
Computer Network Laboratory-17CSL57

//AWK FILE

//p4.awk

BEGIN{
count1=0
count2=0
pack1=0
pack2=0
time1=0
time2=0
}
{
if($1 == "r" && $3 == "_1_" && $4 == "AGT")
{
count1++
pack1=pack1+$8
time1=$2
}
if($1 == "r" && $3 == "_2_" && $4 =="AGT")
{
count2++
pack2=pack2+$8
time2=$2
}
}
END{
printf("The Throughput from n0 to n1: %f Mbps \n",
((count1*pack1*8)/(time1*1000000)));
printf("The Throughput from n1 to n2: %f Mbps \n",
((count2*pack2*8)/(time2*1000000)));
}

CSE Page 19
Computer Network Laboratory-17CSL57

5. Implement and study the performance of GSM on NS2/NS3 (Using MAC layer) or
equivalent environment.

Design

Fig. 5a. Performance of GSM


Source code

#===================================
# Simulation parameters setup
#===================================
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set val(netif) Phy/WirelessPhy ;# network interface type
set val(mac) Mac/802_11 ;# MAC type
set val(ifq) Queue/DropTail/PriQueue ;# interface queue type
set val(ll) LL ;# link layer type
set val(ant) Antenna/OmniAntenna ;# antenna model
set val(ifqlen) 50 ;# max packet in ifq
set val(nn) 6 ;# number of mobilenodes
set val(rp) AODV ;# routing protocol
set val(x) 603 ;# X dimension of topography
set val(y) 405 ;# Y dimension of topography
set val(stop) 10.0 ;# time of simulation end

#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]

#Setup topography object


set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
create-god $val(nn)

CSE Page 20
Computer Network Laboratory-17CSL57

#Open the NS trace file


set tracefile [open pr5n1.tr w]
$ns trace-all $tracefile

#Open the NAM trace file


set namfile [open pr5n1.nam w]
$ns namtrace-all $namfile
$ns namtrace-all-wireless $namfile $val(x) $val(y)
set chan [new $val(chan)];#Create wireless channel

#===================================
# Mobile node parameter setup
#===================================
$ns node-config -adhocRouting $val(rp) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channel $chan \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace ON \
-movementTrace ON

#===================================
# Nodes Definition
#===================================
#Create 6 nodes
set n0 [$ns node]
$n0 set X_ 178
$n0 set Y_ 234
$n0 set Z_ 0.0
$ns initial_node_pos $n0 20
set n1 [$ns node]
$n1 set X_ 296
$n1 set Y_ 237
$n1 set Z_ 0.0
$ns initial_node_pos $n1 20
set n2 [$ns node]
$n2 set X_ 390
$n2 set Y_ 237
$n2 set Z_ 0.0
$ns initial_node_pos $n2 20
set n3 [$ns node]
$n3 set X_ 503
$n3 set Y_ 239

CSE Page 21
Computer Network Laboratory-17CSL57
$n3 set Z_ 0.0
$ns initial_node_pos $n3 20
set n4 [$ns node]
$n4 set X_ 135
$n4 set Y_ 305
$n4 set Z_ 0.0
$ns initial_node_pos $n4 20
set n5 [$ns node]
$n5 set X_ 302
$n5 set Y_ 142
$n5 set Z_ 0.0
$ns initial_node_pos $n5 20
$ns at 1 " $n5 setdest 400 142 20 "
#===================================
# Agents Definition
#===================================
set tcp0 [new Agent/TCP]
$ns attach-agent $n4 $tcp0
set sink1 [new Agent/TCPSink]
$ns attach-agent $n5 $sink1
$ns connect $tcp0 $sink1
$tcp0 set packetSize_ 1500
#===================================
# Applications Definition
#===================================
#Setup a FTP Application over TCP connection
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ns at 1.0 "$ftp0 start"
$ns at 10.0 "$ftp0 stop"

#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam pr5n1.nam &
exit 0
}
for {set i 0} {$i < $val(nn) } { incr i } {
$ns at $val(stop) "\$n$i reset"
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run

CSE Page 22
Computer Network Laboratory-17CSL57

Result

Fig. 5b. GSM network on execution

Fig. 5c. Packet drops in GSM network

Program5,6:

//AWK FILE

//prog5.awk

BEGIN{
PacketRcvd=0;
Throughput=0.0;
}
{
if(($1=="r") && ($5=="tcp") && ($10=4.0))
{
PacketRcvd++;
}
}
END {
Throughput=((PacketRcvd*1000*8)/(95.0*1000000));
printf("packet received:%f\n", PacketRcvd);
printf( "the throughput is:%f\n",Throughput);
}

CSE Page 23
Computer Network Laboratory-17CSL57

6. Implement and study the performance of CDMA on NS2/NS3 (Using stack called Call
net) or equivalent environment.
Design

Fig. 6a. Performance of CDMA/GSM


Source code

#===================================
# Simulation parameters setup
#===================================
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set val(netif) Phy/WirelessPhy ;# network interface type
set val(mac) Mac/802_11 ;# MAC type
set val(ifq) Queue/DropTail/PriQueue ;# interface queue type
set val(ll) LL ;# link layer type
set val(ant) Antenna/OmniAntenna ;# antenna model
set val(ifqlen) 50 ;# max packet in ifq
set val(nn) 6 ;# number of mobilenodes
set val(rp) TORA ;# routing protocol CDMA
set val(x) 603 ;# X dimension of topography
set val(y) 405 ;# Y dimension of topography
set val(stop) 10.0 ;# time of simulation end

#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]

#Setup topography object


set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
create-god $val(nn)

CSE Page 24
Computer Network Laboratory-17CSL57
#Open the NS trace file
set tracefile [open pr5n1.tr w]
$ns trace-all $tracefile

#Open the NAM trace file


set namfile [open pr5n1.nam w]
$ns namtrace-all $namfile
$ns namtrace-all-wireless $namfile $val(x) $val(y)
set chan [new $val(chan)];#Create wireless channel

#===================================
# Mobile node parameter setup
#===================================
$ns node-config -adhocRouting $val(rp) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channel $chan \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace ON \
-movementTrace ON

#===================================
# Nodes Definition
#===================================
#Create 6 nodes
set n0 [$ns node]
$n0 set X_ 178
$n0 set Y_ 234
$n0 set Z_ 0.0
$ns initial_node_pos $n0 20
set n1 [$ns node]
$n1 set X_ 296
$n1 set Y_ 237
$n1 set Z_ 0.0
$ns initial_node_pos $n1 20
set n2 [$ns node]
$n2 set X_ 390
$n2 set Y_ 237
$n2 set Z_ 0.0
$ns initial_node_pos $n2 20
set n3 [$ns node]
$n3 set X_ 503
$n3 set Y_ 239
$n3 set Z_ 0.0

CSE Page 25
Computer Network Laboratory-17CSL57
$ns initial_node_pos $n3 20
set n4 [$ns node]
$n4 set X_ 135
$n4 set Y_ 305
$n4 set Z_ 0.0
$ns initial_node_pos $n4 20
set n5 [$ns node]
$n5 set X_ 302
$n5 set Y_ 142
$n5 set Z_ 0.0
$ns initial_node_pos $n5 20
$ns at 1 " $n5 setdest 400 142 20 "
#===================================
# Agents Definition
#===================================
set tcp0 [new Agent/TCP]
$ns attach-agent $n4 $tcp0
set sink1 [new Agent/TCPSink]
$ns attach-agent $n5 $sink1
$ns connect $tcp0 $sink1
$tcp0 set packetSize_ 1500
#===================================
# Applications Definition
#===================================
#Setup a FTP Application over TCP connection
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ns at 1.0 "$ftp0 start"
$ns at 10.0 "$ftp0 stop"

#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam pr5n1.nam &
exit 0
}
for {set i 0} {$i < $val(nn) } { incr i } {
$ns at $val(stop) "\$n$i reset"
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run

CSE Page 26
Computer Network Laboratory-17CSL57

Result

Fig. 6b. CDMA network on execution

Fig. 6c. Packet drops in CDMA network

CSE Page 27
Computer Network Laboratory-17CSL57

//prog5.tcl

# General Parameters
set opt(ecn) 0 ;
set opt(window) 30 ;
# Topology
set opt(type) gsm ; #type of link:
# AQM parameters
set opt(minth) 5 ;
set opt(maxth) 10 ;
set opt(adaptive) 1 ; # 1 for Adaptive RED, 0 for plain RED
#default downlink bandwidth in bps
set bwDL(gsm) 9600
#default uplink bandwidth in bps
set bwUL(gsm) 9600
#default downlink propagation delay in seconds
set propDL(gsm) .500
#default uplink propagation delay in seconds
set propUL(gsm) .500
#default buffer size in packets
set buf(gsm) 10
set ns [new Simulator]
set tf [open prog5.tr w]
set nf [open prog5.nam w]
$ns trace-all $tf
$ns namtrace-all $nf
set nodes(s) [$ns node]
set nodes(bs1) [$ns node]
set nodes(ms) [$ns node]
set nodes(bs2) [$ns node]
set nodes(d) [$ns node]
proc cell_topo {} {
global ns nodes
$ns duplex-link $nodes(s) $nodes(bs1) 3Mbps 10ms DropTail
$ns duplex-link $nodes(bs1) $nodes(ms) 1Mbps 1ms RED
$ns duplex-link $nodes(ms) $nodes(bs2) 1Mbps 1ms RED
$ns duplex-link $nodes(bs2) $nodes(d) 3Mbps 50ms DropTail puts "Cell Topology"
}
proc set_link_params {t} {
global ns nodes bwUL bwDL propUL propDL buf
$ns bandwidth $nodes(bs1) $nodes(ms) $bwDL($t) simplex
$ns bandwidth $nodes(ms) $nodes(bs1) $bwUL($t) simplex
$ns bandwidth $nodes(bs2) $nodes(ms) $bwDL($t) simplex
$ns bandwidth $nodes(ms) $nodes(bs2) $bwUL($t) simplex
$ns delay $nodes(bs1) $nodes(ms) $propDL($t) simplex
$ns delay $nodes(ms) $nodes(bs1) $propDL($t) simplex

CSE Page 28
Computer Network Laboratory-17CSL57

$ns delay $nodes(bs2) $nodes(ms) $propDL($t) simplex

$ns delay $nodes(ms) $nodes(bs2) $propDL($t) simplex


$ns queue-limit $nodes(bs1) $nodes(ms) $buf($t)
$ns queue-limit $nodes(ms) $nodes(bs1) $buf($t)
$ns queue-limit $nodes(bs2) $nodes(ms) $buf($t)
$ns queue-limit $nodes(ms) $nodes(bs2) $buf($t)
}
# RED and TCP parameters
Queue/RED set summarystats_ true
Queue/DropTail set summarystats_ true
Queue/RED set adaptive_ $opt(adaptive)
Queue/RED set q_weight_ 0.0
Queue/RED set thresh_ $opt(minth)
Queue/RED set maxthresh_ $opt(maxth)
Queue/DropTail set shrink_drops_ true
Agent/TCP set ecn_ $opt(ecn)
Agent/TCP set window_ $opt(window)
DelayLink set avoidReordering_ true
#Create topology
switch $opt(type) {
gsm - gprs - umts {cell_topo}
}
set_link_params $opt(type)
$ns insert-delayer $nodes(ms) $nodes(bs1) [new Delayer]
$ns insert-delayer $nodes(bs1) $nodes(ms) [new Delayer]
$ns insert-delayer $nodes(ms) $nodes(bs2) [new Delayer]
$ns insert-delayer $nodes(bs2) $nodes(ms) [new Delayer]
# Set up forward TCP connection
set tcp1 [$ns create-connection TCP/Sack1 $nodes(s) TCPSink/Sack1 $nodes(d) 0]
set ftp1 [[set tcp1] attach-app FTP]
$ns at 0.5 "$ftp1 start"
proc stop {} {
global nodes ns opt nf tf
$ns flush-trace
close $nf
close $tf
exec nam prog5.nam &
exit 0
}
$ns at 100 "stop"
$ns run

CSE Page 29
Computer Network Laboratory-17CSL57

Fig. 6d. CDMA network

CSE Page 30
Computer Network Laboratory-17CSL57

7. Write a program for error detecting code using CRC-CCITT (16- bits)

//Crc.java

import java.io.*;
class Crc
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int[ ] data;
int[ ]div;
int[ ]divisor;
int[ ]rem;
int[ ] crc;
int data_bits, divisor_bits, tot_length;
System.out.println("Enter number of data bits : ");
data_bits=Integer.parseInt(br.readLine());
data=new int[data_bits];
System.out.println("Enter data bits : ");
for(int i=0; i<data_bits; i++)
data[i]=Integer.parseInt(br.readLine());
System.out.println("Enter number of bits in divisor : ");
divisor_bits=Integer.parseInt(br.readLine());
divisor=new int[divisor_bits];
System.out.println("Enter Divisor bits : ");
for(int i=0; i<divisor_bits; i++)
divisor[i]=Integer.parseInt(br.readLine());
tot_length=data_bits+divisor_bits;
div=new int[tot_length];
rem=new int[tot_length];
crc=new int[tot_length];
/*------------------ CRC GENERATION-----------------------*/
for(int i=0;i<data.length;i++)
div[i]=data[i];
System.out.print("Dividend (after appending 0's) are : ");
for(int i=0; i< div.length; i++)
System.out.print(div[i]);
System.out.println();
for(int j=0; j<div.length; j++){
rem[j] = div[j];
}
rem=divide(div, divisor, rem);
for(int i=0;i<div.length;i++)
//append dividend and ramainder
{
crc[i]=(div[i]^rem[i]);

CSE Page 31
Computer Network Laboratory-17CSL57

}
System.out.println();
System.out.println("CRC code : ");
for(int i=0;i<crc.length;i++)
System.out.print(crc[i]);
/*-------------------ERROR DETECTION---------------------*/
System.out.println();
System.out.println("Enter CRC code of "+tot_length+" bits : ");
for(int i=0; i<crc.length; i++)
crc[i]=Integer.parseInt(br.readLine());
for(int j=0; j<crc.length; j++){
rem[j] = crc[j];
}

rem=divide(crc, divisor, rem);


for(int i=0; i< rem.length; i++)
{
if(rem[i]!=0)
{
System.out.println("Error");
break;
}
if(i==rem.length-1)
System.out.println("No Error");
}
}
static int[] divide(int div[],int divisor[], int rem[])
{
int cur=0;
while(true)
{
for(int i=0;i<divisor.length;i++)
rem[cur+i]=(rem[cur+i]^divisor[i]);
while(rem[cur]==0 && cur!=rem.length-1)
cur++;
if((rem.length-cur)<divisor.length)
break;
}
return rem;
}
}

CSE Page 32
Computer Network Laboratory-17CSL57

Output:
C:\Program Files\Java\jdk1.7.0_80\bin>javac Crc.java
C:\Program Files\Java\jdk1.7.0_80\bin>java Crc
Enter number of data bits :
4
Enter data bits :
0
1
0
1
Enter number of bits in divisor :
4
Enter Divisor bits :
1
0
1
1
Dividend (after appending 0's) are : 01010000

CRC code :
01010011
Enter CRC code of 8 bits :
0
1
0
1
0
0
1
1
No Error

CSE Page 33
Computer Network Laboratory-17CSL57

8. Write a program to find the shortest path between vertices using bellman-ford
algorithm.

//Source code

import java.util.Scanner;
public class BellmanFord
{
int n;
int[][] a; int[] d;
int[] p;
int s;
public final static int INFTY=999;
BellmanFord(int n)
{
this.n = n;
a = new int[n][n];
d = new int[n];
p = new int[n];
}
void bellmanFord()
{
// Initialization
for(int i=0; i < n; i++)
{
d[i] = a[s][i];
p[i] = a[s][i] == INFTY ? -1 : s;
}
p[s] = -1;
for(int i=0; i < n-1; i++)
{
// Relax all edges iteratively (n-1) times
for(int u=0; u < n; u++)
{
for(int v=0; v < n; v++)
{
if(d[v] > d[u]+a[u][v])
{
d[v] = d[u]+a[u][v]; p[v] =
u;

CSE Page 34
Computer Network Laboratory-17CSL57

}
}
}
}
}
void input(Scanner scanner)
{
System.out.println("Enter the graph G: ");
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
a[i][j] = scanner.nextInt();
if (i != j && a[i][j] == 0) a[i][j] = INFTY;
}
}
System.out.print("Enter the source vertex: ");
s =scanner.nextInt();
scanner.close();
}
void path(int v)
{
if (v == -1) return;
path(p[v]); System.out.print("."+v);
}
void output()
{
int i;
for(i=0; i < n; i++)
{
System.out.print("d(" + s + "," + i + ")=" + d[i]+" :p");
path(i);
System.out.println();
}
}
public static void main(String[] args)
{
int n;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of nodes: ");

CSE Page 35
Computer Network Laboratory-17CSL57

n = scanner.nextInt();
BellmanFord bf = new BellmanFord(n);
bf.input(scanner);
bf.bellmanFord();
bf.output();
}
}

//output
C:\Program Files\Java\jdk1.7.0_80\bin>javac BellmanFord.java
C:\Program Files\Java\jdk1.7.0_80\bin>java BellmanFord
Enter the number of nodes: 4
Enter the graph G:
0224
2030
2301
4010
Enter the source vertex: 3
d(3,0)=3 :p.3.2.0
d(3,1)=4 :p.3.2.1
d(3,2)=1 :p.3.2
d(3,3)=0 :p.3

CSE Page 36
Computer Network Laboratory-17CSL57

9. Using TCP/IP sockets, write a client – server program to make the client send the file
name and to make the server send back the contents of the requested file if present.

//TcpClient.java

import java.io.*;
import java.net.*;
import java.util.*;
public class TcpClient
{
void client() throws Exception
{
Socket s=new Socket("localhost",3333);
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
Scanner scanner = new Scanner(System.in);
System.out.print("Enter filename:");
String fileName = scanner.next();
dout.writeUTF(fileName);
String message;
do
{
message = din.readUTF();
System.out.println("Client: " +message);
}
while(!message.equals("eof"));
scanner.close();
dout.close();
s.close();
}
public static void main(String[] args) throws Exception
{
TcpClient tc = new TcpClient();
tc.client();
}
}

CSE Page 37
Computer Network Laboratory-17CSL57

//Tcpserver.java

import java.io.*;
import java.net.*;
import java.nio.file.*;
import java.nio.charset.*;
import java.util.*;
public class TcpServer
{
void server() throws Exception
{
ServerSocket ss=new ServerSocket(3333);
System.out.println("Server waiting for connection from client");
Socket cs=ss.accept();
DataInputStream din=new DataInputStream(cs.getInputStream());
DataOutputStream dout=new DataOutputStream(cs.getOutputStream());
String fileName = din.readUTF();
List<String> lines = Files.readAllLines(Paths.get(fileName), Charset.defaultCharset());
for (int i=0; i < lines.size(); i++)
{
System.out.println("server: "+lines.get(i));
dout.writeUTF(lines.get(i));
}
din.close();
cs.close();
ss.close();
}
public static void main(String[] args) throws Exception {
TcpServer ts = new TcpServer();
ts.server();
}
}

//testfile.txt
I am from ICEAS
I like network programs
eof

CSE Page 38
Computer Network Laboratory-17CSL57

//output
Open two command prompt or terminals(linux/ubuntu) and execute java files separately.

C:\Program Files\Java\jdk1.7.0_80\bin>javac TcpServer.java


C:\Program Files\Java\jdk1.7.0_80\bin>java TcpServer
Server waiting for connection from client
server: I am from ICEAS
server: I like network programs
server: eof

C:\Program Files\Java\jdk1.7.0_80\bin>javac TcpClient.java


C:\Program Files\Java\jdk1.7.0_80\bin>java TcpClient
Enter filename:testfile.txt
Client: I am from ICEAS
Client: I like network programs
Client: eof

CSE Page 39
Computer Network Laboratory-17CSL57

10. Write a program on datagram socket for client/server to display the messages on client
side, typed at the server side.

//UdpClient.java

import java.net.*;
class UdpClient
{
public void client() throws Exception
{
DatagramSocket socket = new DatagramSocket();
DatagramPacket packet;
String message = "Start";
packet = new DatagramPacket(message.getBytes(), message.length(),
InetAddress.getByName("127.0.0.1"),3333);
socket.send(packet);
System.out.println("Client: Sent data to Server ; Message = " + message);
byte[] msgBuffer = new byte[1024];
packet = new DatagramPacket(msgBuffer, msgBuffer.length);
do
{
socket.receive(packet);
message = new String(msgBuffer, 0, packet.getLength());
System.out.println(message);
}
while (!message.equals("stop"));
socket.close();
}
public static void main(String args[]) throws Exception
{
UdpClient uc = new UdpClient(); uc.client();
}
}

CSE Page 40
Computer Network Laboratory-17CSL57

//UdpServer.java
import java.net.*;
import java.util.Scanner;
class UdpServer
{
public void server() throws Exception
{
DatagramSocket socket = new DatagramSocket(3333);
DatagramPacket packet;
System.out.println("UDP Server Listening in " + 3333);
byte[] msgBuffer = new byte[1024];
packet = new DatagramPacket(msgBuffer, msgBuffer.length);
socket.receive(packet);
String message = new String(msgBuffer, 0, packet.getLength());
System.out.println("Client: Message received = " + message);
InetAddress address = packet.getAddress();
int port = packet.getPort();
Scanner scanner = new Scanner(System.in);
System.out.println("Server: type lines of text; type 'stop' to terminate");
do
{
message = scanner.nextLine();
packet = new DatagramPacket(message.getBytes(), message.length(), address, port);
socket.send(packet);
}
while (message.compareTo("stop") != 0);
scanner.close();
socket.close();
}
public static void main(String args[]) throws Exception
{
UdpServer us = new UdpServer(); us.server();
}
}

CSE Page 41
Computer Network Laboratory-17CSL57

//Output
C:\Program Files\Java\jdk1.7.0_80\bin>javac UdpServer.java
C:\Program Files\Java\jdk1.7.0_80\bin>java UdpServer
UDP Server Listening in 3333
Client: Message received = Start
Server: type lines of text; type 'stop' to terminate
hi
welcome to network programs
stop

C:\Program Files\Java\jdk1.7.0_80\bin>javac UdpClient.java


C:\Program Files\Java\jdk1.7.0_80\bin>java UdpClient
Client: Sent data to Server ; Message = Start
hi
welcome to network programs
stop

CSE Page 42
Computer Network Laboratory-17CSL57

11. Write a program for simple RSA algorithm to encrypt and decrypt the data.

//RSA.java

import java.util.Scanner;
public class RSA
{
int d,e,n;
String message;
int gcd(int m, int n)
{
int rv = n == 0 ? m : gcd(n, m % n);
return rv;
}
int pow(int a, int m, int n)
{
int r = 1;
while (m-- != 0)
r = (r*a) % n;
return r ;
}
void rsa()
{
int p,q,z;
//odd prime numbers 3, 5 (3, 11?)
p = 11;
q = 13;
n = p*q;
z = (p-1)*(q-1);
// choose e
for (e=2; gcd(e, z) != 1; e++);
// choose d as inverse of e
for (d=2; (d*e) % z != 1; d++);
}
void input()
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the message:");
message = scanner.next();
scanner.close();
}
void output()
{
System.out.println("S=(d,n)=(" + d + "," + n + ")");
System.out.println("P=(e,n)=(" + e + "," + n + ")");
System.out.println("Plain Text: " + message);

CSE Page 43
Computer Network Laboratory-17CSL57

char[] T = message.toCharArray();
for (int i=0; i < message.length(); i++)
{
T[i] = (char)pow((int)T[i], e, n);
}
System.out.println("Cipher Text: " + String.valueOf(T));
for (int i=0; i < message.length(); i++)
{
T[i] = (char)pow((int)T[i], d, n); //convert(a, d, n);
}
System.out.println("Plain Text: " + String.valueOf(T));
}
public static void main(String[] args)
{
RSA r = new RSA();
r.input();
r.rsa();
r.output();
}
}

//Output
C:\Program Files\Java\jdk1.7.0_80\bin>javac RSA.java
C:\Program Files\Java\jdk1.7.0_80\bin>java RSA
Enter the message:ICEAS
S=(d,n)=(103,143)
P=(e,n)=(7,143)
Plain Text: ICEAS
Cipher Text: SYlA
Plain Text: ICEAS

CSE Page 44
Computer Network Laboratory-17CSL57

12. Write a program for congestion control using leaky bucket algorithm.

//LeakyBucket.java
import java.util.*;
import java.util.Random;
public class LeakyBucket
{
int n;
int burst,outgoingRate,bucketSize,incoming,outgoing;
int pending,overflow,duration,interval;
LeakyBucket()
{
pending = 0;
incoming = 0;
overflow = 0;
outgoing = 0;
}
void leakyBucket()
{
System.out.println("Time Incoming Pending Overflow Outgoing");
Random rand = new Random();
int time=0;
while (time < duration)
{
incoming = rand.nextInt(burst);
if ((pending + incoming) > bucketSize)
{
overflow = (pending + incoming) - bucketSize;
pending = bucketSize;
}
else pending += incoming;
//interval = rand.nextInt(); // Next packet will come at time interval = 1;
for(int clk = 0; clk < 1; ++clk)
{
output(time, incoming, pending, overflow, outgoing);
outgoing = Math.min(outgoingRate, pending);
pending -= outgoing;
incoming = 0;
++time;
}

CSE Page 45
Computer Network Laboratory-17CSL57

}
}
void input()
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter burst size: ");
burst = scanner.nextInt();
System.out.println("Enter bucket size: ");
bucketSize = scanner.nextInt();
System.out.println("Enter outgoing rate: ");
outgoingRate = scanner.nextInt();
System.out.println("Enter duration: ");
duration = scanner.nextInt();
scanner.close();
}
void output(int time, int incoming, int pending, int overflow, int outgoing)
{
System.out.printf("%d\t%d\t%d\t%d\t%d\n",time,incoming,pending,overflow,outgoing);
}
public static void main(String[] args)
{
LeakyBucket lb = new LeakyBucket();
lb.input();
lb.leakyBucket();
}
}

CSE Page 46
Computer Network Laboratory-17CSL57

//output
C:\Program Files\Java\jdk1.7.0_80\bin>java LeakyBucket
Enter burst size:
6
Enter bucket size:
5
Enter outgoing rate:
2
Enter duration:
6
Time Incoming Pending Overflow Outgoing
0 4 4 0 0
1 3 5 0 2
2 1 4 0 2
3 4 5 1 2
4 3 5 1 2
5 5 5 3 2

CSE Page 47

You might also like