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 out.tr 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.tr 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








Comments

Popular posts from this blog

How to Create a WordPress Plugin?

If you want to customise or need to add an extra functionality for your website, you can do this by changing the core of the WordPress, however, if WordPress gets updated your changes will be ineffective. So, to get rid of this problem WordPress introduced modular architecture in which if ones need to extra functionality he can add PHP module called plugins and these plugins are written in PHP . In addition, HTML, JavaScript, JavaQuery and AJAX can also be incorporated to add more functionality , but in this article I will only use an echo command to print a single sentence in  PHP, this plugin will be very simple but at the end you will be able to create your own plugin.
First of all WordPress plugin is a PHP file and this need plugin header in PHP comment which is

____________________________________________
<?php
/*Plugin Name: Simple Plugin
Plugin URI: https://wordpress.org/plugins/simpleplugin/
Description: This is demonstration for simple plugin
Version: 0.1.0
Author: Binde…

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- www.facebook.com- 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- www.medium.com-  This is a very good plateform where you can submit you posts links and even you can write articles. Making account in Medium is ve…

5 Growth Hacking Srategies In Blogging

In my previous article on growth hacking  What Are Growth Hacking Strategies? .  I presented some case studies that how some online businesses grew without spending millions of dollars on advertising, this growth hacking strategies can also be applied for blogging to increase visitors on the blog, further, in simple words growth hacking  is a rapid experiments  to apply all the tactics and appropriate technologies to   increase audiences. In blogging, growth hacking strategy can also be applied, here I will present some tactics that bloggers are applying before growth hacking word was formulated and some are need to be applied.

1. Do experiment on your content- In most  cases, users like simple sentences, but if you introduce few difficult words readers my welcome, some quotes that matches with your article can grab the attention of users. Further, it is fact  that 70% of word in English  are of French origin, you can introduce some French words and french speaking countries, like Fr…