Sunday, May 5, 2013

NS2 TCP Example

NS2 is a free but a powerful simulator. It is capable to simulate most of complex scenarios in wired and wireless networks.

How to install:
Get your teminal and type this
sudo apt-get install ns2
view raw gistfile1.sh hosted with ❤ by GitHub
Then you'll need NAM

sudo apt-get install nam
view raw gistfile1.txt hosted with ❤ by GitHub
Source code:
# Create a ns object
set ns [new Simulator]
$ns color 1 Blue
$ns color 2 Red
# Open the Trace files
set TraceFile [open outtcp.tr w]
$ns trace-all $TraceFile
# Open the NAM trace file
set NamFile [open outtcp.nam w]
$ns namtrace-all $NamFile
# Create six nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 0.25Mb 100ms DropTail # bottleneck link
$ns duplex-link $n3 $n4 2Mb 10ms DropTail
$ns duplex-link $n3 $n5 2Mb 10ms DropTail
$ns queue-limit $n2 $n3 20
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n3 $n4 orient right-up
$ns duplex-link-op $n3 $n5 orient right-down
#TCP N0 and N4
set tcp1 [new Agent/TCP/Newreno]
$ns attach-agent $n0 $tcp1
set sink1 [new Agent/TCPSink/DelAck]
$ns attach-agent $n4 $sink1
$ns connect $tcp1 $sink1
$tcp1 set fid_ 1
$tcp1 set window_ 8000
$tcp1 set packetSize_ 600
#TCP N0 and N4
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ftp1 set type_ FTP
#TCP N1 and N5
set tcp2 [new Agent/TCP/Newreno]
$ns attach-agent $n1 $tcp2
set sink2 [new Agent/TCPSink/DelAck]
$ns attach-agent $n5 $sink2
$ns connect $tcp2 $sink2
$tcp2 set fid_ 2
$tcp2 set window_ 8000
$tcp2 set packetSize_ 600
#FTP TCP N1 and N5
set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp2
$ftp2 set type_ FTP
$ns at 0.1 "$ftp1 start"
$ns at 10.0 "$ftp2 start"
$ns at 50.0 "$ftp1 stop"
$ns at 45.0 "$ftp2 stop"
proc finish {} {
global ns TraceFile NamFile
$ns flush-trace
close $TraceFile
close $NamFile
exec nam outtcp.nam &
exit 0
}
$ns at 60.0 "finish"
$ns run
view raw gistfile1.tcl hosted with ❤ by GitHub
How to run:
ns newTCP.tcl
view raw gistfile1.txt hosted with ❤ by GitHub
You may need to create outtcp.nam file in source code directory.

Sample screen of out put.



Related post:

http://beyondtechs.blogspot.com/2013/05/ns2-udp-example.html

2 comments:

  1. Would you have time to see if you can implement my code in NS2, I have tried and I am having issues. I would be happy to compensate you for your time and effort.

    ReplyDelete