如何用NS2仿真自带的协议

如何用NS2仿真自带的协议,第1张

其实 只需要编写一个.tcl脚本文件

set val(mac)Mac/802_11

然后要配置节点参数时 指定

$ns_ node-config- -macType $val(mac)即可

附上例子一枚:

set val(chan) Channel/WirelessChannel#Channel Type

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 # interface queue type

set val(ll) LL # link layer type

set val(ant)Antenna/OmniAntenna# antenna model

set val(ifqlen) 50# max packet in ifq

set val(nn) 10 # number of mobilenodes

set val(rp) DumbAgent # routing protocol

set val(x) 600

set val(y) 600

Mac/802_11 set dataRate_ 11Mb

#Phy/WirelessPhy set CSThresh_ 10.00e-12

#Phy/WirelessPhy set RXThresh_ 10.00e-11

#Phy/WirelessPhy set Pt_ 0.1

#Phy/WirelessPhy set Pt_ 7.214e-3

# Initialize Global Variables

set ns_ [new Simulator]

set tracefd [open infra.tr w]

$ns_ trace-all $tracefd

# set up topography object

set topo [new Topography]

$topo load_flatgrid $val(x) $val(y)

# Create God

create-god $val(nn)

# Create channel

set chan_1_ [new $val(chan)]

$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 OFF \

-routerTrace OFF \

-macTrace ON \

-movementTrace ON \

-channel $chan_1_

for {set i 0} {$i <[expr $val(nn)]} {incr i} {

set node_($i) [$ns_ node]

$node_($i) random-motion 0 # disable random motion

set mac_($i) [$node_($i) getMac 0]

$mac_($i) set RTSThreshold_ 3000

$node_($i) set X_ $i

$node_($i) set Y_ 0 # Horizontal arrangement of nodes

$node_($i) set Z_ 0.0

}

#Set Node 0 and Node $val(nn) as the APs. Thus the APs are the ends of the horizontal line. Each STA receives different power levels.

set AP_ADDR1 [$mac_(0) id]

$mac_(0) ap $AP_ADDR1

set AP_ADDR2 [$mac_([expr $val(nn) - 1]) id]

$mac_([expr $val(nn) - 1]) ap $AP_ADDR2

#$mac_([expr $val(nn) - 1]) set BeaconInterval_ 0.2

$mac_(1) ScanType ACTIVE

for {set i 3} {$i <[expr $val(nn) - 1]} {incr i} {

$mac_($i) ScanType PASSIVE #Passive

}

$ns_ at 1.0 "$mac_(2) ScanType ACTIVE"

Application/Traffic/CBR set packetSize_ 1023

Application/Traffic/CBR set rate_ 256Kb

for {set i 1} {$i <[expr $val(nn) - 1]} {incr i} {

set udp1($i) [new Agent/UDP]

$ns_ attach-agent $node_($i) $udp1($i)

set cbr1($i) [new Application/Traffic/CBR]

$cbr1($i) attach-agent $udp1($i)

}

set base0 [new Agent/Null]

$ns_ attach-agent $node_(1) $base0

set base1 [new Agent/Null]

$ns_ attach-agent $node_(8) $base1

$ns_ connect $udp1(4) $base0

$ns_ connect $udp1(5) $base1

$ns_ at 2.0 "$cbr1(4) start"

$ns_ at 4.0 "$cbr1(5) start"

$ns_ at 10.0 "$node_(4) setdest 300.0 1.0 30.0"

$ns_ at 20.0 "stop"

$ns_ at 20.0 "puts \"NS EXITING...\" $ns_ halt"

proc stop {} {

global ns_ tracefd

$ns_ flush-trace

close $tracefd

exit 0

}

puts "Starting Simulation..."

$ns_ run

如何在NS2中加入加密解密:http://sce.uhcl.edu/transa/ 哈哈 搜了很久才搜着的 里头有PPT跟代码

昨天找着的,今天把它给调通了,改这几个文件:packet.h. ns-default.tcl, ns-packet.tcl ,makefile,跟添加LMAC协议有点相似,把Security_packet.h和Security_packet.cc文件拷贝到..//apps文件夹中,按照模仿rtp或ping修改那4个文件,就得到仿真结果了。不过这个加密貌似是在应用协议里头做的。

http://blog.csdn.net/kgn28/archive/2010/03/12/5375662.aspx这个上面说的添加网络协议也是一个可以考虑的方法,按照上面说的方法一步步完成,不过还是出了很多错误,不晓得哪出了问题。

各有利弊,入门方面,NS2比较难,OPNET会稍微好些,不过也只是好一点点

NS2开源,OPNET现在有破解的学生版本,所以使用权没什么问题。

NS2编程要求高点 ,OPNET编程要求还好,而且MAC协议可以通过编程和图像结合,比较容易实现。但是物理层那边没有NS2完善,OPNET只能通过管道编写一些基本模型。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11953115.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存