Skip to main content

How to simulate ad hoc network in NS-2 ?

set ns_ [new Simulator]
set val(x) 800 ;# X dimension of topology
set val(y) 800 ;# Y dimension of topology
set val(nn) 50 ;# Number of node
set val(stop) 150.0 ;# Simulation time
set val(routing) AODV
set qtype Queue/DropTail/PriQueue
#set qtype CMUPriQueue # This queue will be used for DSR
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
set tracefd [open w]
$ns_ trace-all $tracefd
$ns_ use-newtrace
set namtrace [open out.nam w]
$ns_ namtrace-all-wireless $namtrace $val(x) $val(y)
set windowsVsTime2 [open w]
# Main Program
# ======================================================================
set god_ [create-god $val(nn)]

# Initialize Global Variables

#global node setting

$ns_ node-config -adhocRouting $val(routing) \
-llType LL \
-macType Mac/802_11\
-ifqType $qtype \
-ifqLen 50 \
-antType Antenna/OmniAntenna \
-propType Propagation/TwoRayGround \
-phyType Phy/WirelessPhy \
-channelType Channel/WirelessChannel \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace ON \
-energyModel EnergyModel\
-rxPower 0.3\
-txPower 0.6\
-initialEnergy 100\
#-idlePower 712e-6 \
#-sleepPower 144e-9\
# -sensePower 0.0175/
Phy/WirelessPhy set CPThresh_ 10.0
Phy/WirelessPhy set CSThresh_ 3.65262e-10 ;#250m
Phy/WirelessPhy set RXThresh_ 3.65262e-10 ;#250m
Phy/WirelessPhy set Rb_ 2*1e6
Phy/WirelessPhy set Pt_ 3.00
Phy/WirelessPhy set freq_ 2.4e+7
Phy/WirelessPhy set L_ 1.0

#Create the specified number of nodes
for {set i 0} { $i < $val(nn) } {incr i} {
set node_($i) [$ns_ node]
source scen-50 # Number of Nodes Download this file and put in the same directory that configures nodes position.

for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ initial_node_pos $node_($i) 30

set tcp1 [new Agent/TCP/Newreno]
$ns_ attach-agent $node_(1) $tcp1

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

set sink1 [new Agent/TCPSink]
$ns_ attach-agent $node_(2) $sink1

$ns_ connect $tcp1 $sink1

$ns_ at 0.5 "$ftp1 start"
$ns_ at 140.0 "$ftp1 stop"

set tcp2 [new Agent/TCP/Newreno]
$ns_ attach-agent $node_(3) $tcp2

set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp2

set sink2 [new Agent/TCPSink]
$ns_ attach-agent $node_(4) $sink2

$ns_ connect $tcp2 $sink2

$ns_ at 0.5 "$ftp2 start"
$ns_ at 140.0 "$ftp2 stop"

set tcp3 [new Agent/TCP/Newreno]
$ns_ attach-agent $node_(5) $tcp3

set ftp3 [new Application/FTP]
$ftp3 attach-agent $tcp3

set sink3 [new Agent/TCPSink]
$ns_ attach-agent $node_(6) $sink3

$ns_ connect $tcp3 $sink3

$ns_ at 0.5 "$ftp3 start"
$ns_ at 140.0 "$ftp3 stop"

set tcp4 [new Agent/TCP/Newreno]
$ns_ attach-agent $node_(7) $tcp4

set ftp4 [new Application/FTP]
$ftp4 attach-agent $tcp4

set sink4 [new Agent/TCPSink]
$ns_ attach-agent $node_(8) $sink4

$ns_ connect $tcp4 $sink4

$ns_ at 0.5 "$ftp4 start"
$ns_ at 140.0 "$ftp4 stop"

proc plotWindow {tcpSource file} {
global ns_
set time 0.1
set now [$ns_ now]
set cwnd [ $tcpSource set cwnd_ ]
puts $file "$now $cwnd"
$ns_ at [expr $now+$time ] "plotWindow $tcpSource $file" }
$ns_ at 1.1 "plotWindow $tcp1 $windowsVsTime2"
for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ at $val(stop) "$node_($i) reset ";
$ns_ at $val(stop) "stop"
$ns_ at [expr $val(stop)+ 0.1 ] "puts \"end simulation\" ; $ns_ halt"
proc stop {} {
global ns_ tracefd
$ns_ flush-trace
close $tracefd
# Tell nodes when the simulation ends

for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ at $val(stop).0 "$node_($i) reset";
$ns_ at $val(stop).000 "$ns_ halt"
$ns_ run


Popular posts from this blog

Top Browser's Plugins that Every Blogger Should Know

To know statistics of your blog or website or others, it helps to understand the performance and necessary actions to take. Furthermore, to help you there are many plugins are developed. But, many bloggers do not know that there are bundels of free plugins are that can help you to understand your website. Such as, what runs, alexa rank plugin, moz rank  plugin and many more. Im going to some of the important plugins that every bloggers should install. 1- Alexa Traffic Rank and Alexa Rank Plugin -   What  happens when you want to know about  alexa rark of websites you are browsing. You open the url "" and put website name. But these plugins show the alexa rank of browsed websites in the right side of address bar and make your life easy. Link for Alexa Traffic Rank 2- What Runs - This  plugin is very useful when you want to about technologies a website is running. Such as, server information, server side scipting language, client side scrip

Social Media and Submission Directories that Can Help to Grow Your Web Traffic

When your blog or website is new it will not be able to get traffic from search engines. Then you need to use social media and submission directories where you will submit your  articles' link. If somebody clicks on the link your article will open and blog will drive traffic. But some submission directories takes long time to approve and some demands reciprocal links which are backlinks from your blog and most of them charge money. Here, I am going to list some social media website and  submission directories which do not take long time to approve.  You can submit your articles link very easily. 1- - This will be the first  the first  choice of any blogger to submit post link to drive traffic. Further, there are many pages and groups that are running. You can find of your own category and can submit your blog posts. 2- -  This is a very good plateform where you can submit you posts links and even you can write articles. Making account in Medium i

Free Blogging Tools for Beginners

If you start a blog, in the begging you think that  write a post and publish it, this is the only work you need to do. However, this is not the case, you will need several tools to understand traffic patterns and to grow your traffic. In the market, both paid and free tools are available. But  beginners usualy likes free, because in starting they do not have money to pay. In this article, I am going to list and explain some useful tools  that can help you in blgging. 1- Google Analytics- This tool is used for multipurpose, such as tracking real time users on your website, users behaviours, users by geographical locations,  type of traffics that come, E-commerce transaction tracking and many more. This tool is provided by google can be linked to your website easiliy. Further, Google Analytics provides a tracking code and if you put on your website, your website will be connected to the the Google Analytics. 2- Search Console ( Google Webmaster ) - Search console  is also provided