Computer Networks (CN)

NS2 Stop and Wait Program

#	* Stop and Wait NS2 Program
#	* Author:PracsPedia

set ns [new Simulator]

set nf [open out.nam w]
$ns namtrace-all $nf

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

set n0 [$ns node]
set n1 [$ns node]

$ns at 0.0 "$n0 label Sender"
$ns at 0.0 "$n1 label Receiver"

$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link-op $n0 $n1 orient right

$ns queue-limit $n0 $n1 50
 set tcp [new Agent/TCP]
 $tcp set window_ 1
 $tcp set maxcwnd_ 1
 $ns attach-agent $n0 $tcp
 set sink [new Agent/TCPSink]
 $ns attach-agent $n1 $sink
 $ns connect $tcp $sink
 set ftp [new Application/FTP]
 $ftp attach-agent $tcp
$ns add-agent-trace $tcp tcp 
$ns monitor-agent-trace $tcp 
$tcp tracevar cwnd_
$ns at 0.1 "$ftp start" 
$ns at 3.0 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n1 $sink" 
$ns at 3.5 "finish"
$ns at 0.0 "$ns trace-annotate \"Stop and Wait with normal operation\""
$ns at 0.05 "$ns trace-annotate \"FTP starts at 0.1\""
$ns at 0.11 "$ns trace-annotate \"Send Packet_0\"" 
$ns at 0.35 "$ns trace-annotate \"Receive Ack_0\"" 
$ns at 0.56 "$ns trace-annotate \"Send Packet_1\"" 
$ns at 0.79 "$ns trace-annotate \"Receive Ack_1\"" 
$ns at 0.99 "$ns trace-annotate \"Send Packet_2\"" 
$ns at 1.23 "$ns trace-annotate \"Receive Ack_2  \"" 
$ns at 1.43 "$ns trace-annotate \"Send Packet_3\"" 
$ns at 1.67 "$ns trace-annotate \"Receive Ack_3\"" 
$ns at 1.88 "$ns trace-annotate \"Send Packet_4\"" 
$ns at 2.11 "$ns trace-annotate \"Receive Ack_4\"" 
$ns at 2.32 "$ns trace-annotate \"Send Packet_5\"" 
$ns at 2.55 "$ns trace-annotate \"Receive Ack_5    \"" 
$ns at 2.75 "$ns trace-annotate \"Send Packet_6\"" 
$ns at 2.99 "$ns trace-annotate \"Receive Ack_6\""
$ns at 3.1 "$ns trace-annotate \"FTP stops\""

$ns run

Download Source Code Program List