NS2 多跳无线网络实现

set val(simDur) 85.0         ;#simulation duration
set val(basename)  multi-hop ;#basename for this project or scenario
set val(statIntvl) 0.1 ;#statistics collection interval
set val(statStart) 0.5 ;
set val(trafStart) 0.5 ;#CBR start time
set val(cbrIntvl) 1.0  ;#CBR traffic interval
set val(chan)  [new Channel/WirelessChannel]    ;# channel model
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(ifqlen) 50                         ;# max packet in ifq
set val(ll)     LL                         ;# link layer type
set val(ant)    Antenna/OmniAntenna        ;# antenna model
set val(nn)     3                          ;# number of mobilenodes
set val(rp)             AODV                  ;# routing protocol
set val(topo_x_dim)     500
set val(topo_y_dim)     500

#Initialize and create output files
#Create a simulator instance
set ns [new Simulator]

#Crate a trace file and animation record
set tracefd [open $val(basename).tr w]
$ns trace-all $tracefd
set namtracefd [open $val(basename).nam w]
$ns namtrace-all-wireless $namtracefd $val(topo_x_dim) $val(topo_y_dim)

set outfd [open $val(basename).out w]
#Create Topology

# set up topography object
set topo       [new Topography]

$topo load_flatgrid $val(topo_x_dim) $val(topo_y_dim)

# Create God
#
create-god $val(nn)

#  Create the specified number of mobilenodes [$val(nn)] and "attach" them
#  to the channel.
 
# configure node
$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) \
   -topoInstance $topo \
   -agentTrace ON \
   -routerTrace ON \
   -macTrace OFF \
   -movementTrace OFF \
   -channel $val(chan)

for {set i 0} {$i < $val(nn) } {incr i} {
 set node($i) [$ns node]
 $node($i) random-motion 0              ;# disable random motion
}


$node(0) set X_ 155.0
$node(0) set Y_ 142.0
$node(0) set Z_ 0.0

$node(1) set X_ 410.0
$node(1) set Y_ 385.0
$node(1) set Z_ 0.0

$node(2) set X_ 340.0
$node(2) set Y_ 260.0
$node(2) set Z_ 0.0


$ns initial_node_pos $node(0) 10
$ns initial_node_pos $node(1) 10
$ns initial_node_pos $node(2) 10

#Modify these variables accordingly
set proto "tcp"
set src $node(0)
set dst $node(1)

if {$proto=="udp"} {
    #Create a udp agent on node0
    set udp [new Agent/UDP]
    $ns attach-agent $src $udp

    # Create a CBR traffic source on node0
    set cbr0 [new Application/Traffic/CBR]
    $cbr0 set packetSize_ 500
    $cbr0 set interval_ $val(cbrIntvl)
    $cbr0 set random_ 1
    $cbr0 attach-agent $udp

    #Create a Null agent (a traffic sink) on node1
    set sink0 [new Agent/LossMonitor]
    $ns attach-agent $dst $sink0

    #Connet source and dest Agents
    $ns connect $udp $sink0
    $ns at $val(trafStart) "$cbr0 start"
    $ns at $val(simDur) "$cbr0 stop"
} elseif {$proto=="tcp"} {
    #Create a tcp agent on the source node
    set tcp [new Agent/TCP]
    $tcp set class_ 2
    $ns attach-agent $src $tcp

    # Create a CBR traffic source on node0
    set ftp [new Application/FTP]
    $ftp attach-agent $tcp

    #Create a sink(a traffic sink) on the destination node
    set sink0 [new Agent/TCPSink]
    $ns attach-agent $dst $sink0

    #Connet source and dest Agents
    $ns connect $tcp $sink0
    $ns at $val(trafStart) "$ftp start"
}

 

$ns at 0.1 "$node(1) setdest 125.0 160.0 10.0"
$ns at 0.1 "$node(0) setdest 460.0 360.0 5.0"


#a procedure to record stats
proc record {} {
    global sink0 ns outfd val
    set bytes [$sink0 set bytes_]
    set now [$ns now]
    puts $outfd "$now $bytes"
    $sink0 set bytes_ 0
    $ns at [expr $now+$val(statIntvl)] "record"
}

#a procedure to close trace file and nam file
proc finish {} {

 global ns tracefd namtracefd basename val
 $ns flush-trace

 close $tracefd
 close $namtracefd
 exec nam multi-hop.nam &
 exit 0
}

#Schedule trigger events
$ns at $val(statStart) "record"

#Call the finish procedure after 5s (of simulated time)
$ns at $val(simDur) "finish"

#Run the simulation
$ns run

 

参考链接:

http://blog.chinaunix.net/uid-23982854-id-4416998.html

https://www.isi.edu/nsnam/ns/tutorial/index.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值