NS网络模拟与协议仿真源代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

第4章例1#建立一个模拟setns[newSimulator]#定义不同数据流的颜色(NAM显示时用到)$nscolor1Blue$nscolor2Red#开启Trace跟踪和NAM跟踪settracefd[openwired.trw]$nstrace-all$tracefdsetnf[openwired.namw]$nsnamtrace-all$nf#定义结束进程procfinish{}{globalnstracefdnf$nsflush-traceclose$tracefdclose$nfexit0}#定义节点setn0[$nsnode]setn1[$nsnode]setn2[$nsnode]setn3[$nsnode]setn4[$nsnode]setn5[$nsnode]#$n5colorblack$n5colorred#定义节点间的链路$nsduplex-link$n0$n12Mb10msDropTail$nsduplex-link$n1$n22Mb10msDropTail$nsduplex-link$n1$n42Mb20msDropTail$nsduplex-link$n3$n42Mb10msDropTail$nsduplex-link$n4$n52Mb10msDropTail#定义链路的队列长度$nsqueue-limit$n1$n410#指定节点间的相互位置(NAM显示用到)$nsduplex-link-op$n0$n1orientright-down$nsduplex-link-op$n2$n1orientright-up$nsduplex-link-op$n1$n4orientright$nsduplex-link-op$n3$n4orientleft-down$nsduplex-link-op$n5$n4orientleft-up#监视链路的队列$nsduplex-link-op$n1$n4queuePos0.5#建立一个TCP连接settcp[newAgent/TCP]$tcpsetclass_2$nsattach-agent$n0$tcpsetsink[newAgent/TCPSink]$nsattach-agent$n5$sink$nsconnect$tcp$sink$tcpsetfid_1#在TCP连接上建立FTP流setftp[newApplication/FTP]$ftpattach-agent$tcp$ftpsettype_FTP#建立一个UDP代理setudp[newAgent/UDP];#建立一个数据发送代理$nsattach-agent$n2$udp;#将数据发送代理绑定到发送节点setnull[newAgent/Null];#建立一个数据接收代理$nsattach-agent$n3$null;#将数据接收代理绑定到接收节点$nsconnect$udp$null;#连接两个代理(也就决定了数据包的发送和接收节点)$udpsetfid_2#在UDP代理上建立CBR流setcbr[newApplication/Traffic/CBR]$cbrattach-agent$udp$cbrsettype_CBR$cbrsetpacket_size_1000$cbrsetrate_1mb$cbrsetrandom_false#启动和结束流代理$nsat0.5$cbrstart$nsat1.0$ftpstart$nsat9.0$ftpstop$nsat9.5$cbrstop$nsat9.5$nsdetach-agent$n0$tcp;$nsdetach-agent$n5$sink#仿真结束时调用结束进程$nsat10.0finish#打印CBR数据包的大小和间隔putsCBRpacket_size_=[$cbrsetpacket_size_]putsCBRinterval=[$cbrsetinterval_]#执行模拟$nsrun例2#简单无线网络模型模拟#无线节点参数setval(chan)Channel/WirelessChannel;#channeltype信道类型:无线信道setval(prop)Propagation/TwoRayGround;#radio-propagationmodel信道模型:TwoRayGroundsetval(netif)Phy/WirelessPhy;#networkinterfacetype无线物理层setval(mac)Mac/802_11;#MACtypeMAC层协议setval(ifq)Queue/DropTail/PriQueue;#interfacequeuetypesetval(ll)LL;#linklayertypesetval(ant)Antenna/OmniAntenna;#antennamodelsetval(ifqlen)50;#maxpacketinifqsetval(rp)AODV;#路由协议setval(x)600;#拓扑-长度setval(y)200;#拓扑-宽度setval(stop)10.0;#timeofsimulationend#建立一个simulator实例setns[newSimulator]#$nsuse-newtrace#开启Trace跟踪和NAM跟踪settracefd[openwireless.trw]setnamtrace[openwireless.namw]$nstrace-all$tracefd$nsnamtrace-all-wireless$namtrace$val(x)$val(y)#建立topology对象settopo[newTopography]$topoload_flatgrid$val(x)$val(y)#创建godcreate-god3setchan_1_[new$val(chan)]#配置无线节点(包括使用何种路由协议,何种mac协议,无线信道的模型等等)$nsnode-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)\-channel$chan_1_\-topoInstance$topo\-agentTraceON\-routerTraceON\-macTraceON\-movementTraceOFF#建立无线节点并设置节点的位置(节点位置决定了拓扑结构)setn(0)[$nsnode]#$n(0)shapehexagon#$n(0)labeln0#$n(0)label-colorRed$n(0)random-motion0$n(0)setX_100.0$n(0)setY_100.0$n(0)setZ_0.0$nsinitial_node_pos$n(0)20setn(1)[$nsnode]$n(1)random-motion0$n(1)setX_300.0$n(1)setY_100.0$n(1)setZ_0.0$nsinitial_node_pos$n(1)20setn(2)[$nsnode]$n(2)random-motion0$n(2)setX_500.0$n(2)setY_100.0$n(2)setZ_0.0$nsinitial_node_pos$n(2)20#建立一个UDP代理setudp0[newAgent/UDP];#建立一个数据发送代理$nsattach-agent$n(0)$udp0;#将数据发送代理绑定到发送节点setnull0[newAgent/Null];#建立一个数据接收代理$nsattach-agent$n(2)$null0;#将数据接收代理绑定到接收节点$nsconnect$udp0$null0;#连接两个代理(也就决定了数据包的发送和接收节点)#在UDP代理上建立CBR流setcbr[newApplication/Traffic/CBR]$cbrattach-agent$udp0#仿真结束时重置节点for{seti0}{$i3}{incri}{$nsat10.0$n($i)reset;}#启动和结束流代理$nsat0.5$cbrstart$nsat9.5$cbrstop#定义结束进程procfinish{}{globalnstracefdnamtrace$nsflush-traceclose$tracefdclose$namtraceexit0}#仿真结束时调用结束进程$nsat$val(stop)finish$nsat$val(stop)puts\NSEXISTING...\;$nshaltputsStartSimulation...#runthesimulation$nsrun例3#有线无线网络混合模拟#无线节点参数setval(chan)Channel/WirelessChannel;#channeltype信道类型:无线信道setval(prop)Propagation/TwoRayGround;#radio-propagationmodel信道模型:TwoRayGroundsetval(netif)Phy/WirelessPhy;#networkinterfacetype无线物理层setval(mac)Mac/802_11;#MACtypeMAC层协议setval(ifq)Queue/DropTail/PriQueue;#interfacequeuetypesetval(ll)LL;#linklayertypesetval(ant)Antenna/OmniAntenna;#antennamodelsetval(ifqlen)50;#maxpacketinifqsetval(rp)DSDV;#路由协议setval(nn)4;#节点数目setval(x)600;setval(y)600;setval(stop)10.0;#timeofsimulationend#建立一个simulator实例setns[newSimulator]#设定分层路由地址$nsnode-config-addressTypehierarchicalAddrParamssetdomain_num_2;#2个网络lappendcluster_num11;#每个网络一个子网AddrParamssetcluster_num_$cluster_numlappendeilastlevel13;#2个子网的节点数目为1和3AddrParamssetnodes_num_$eilastlevelputsConfigurationofhierarchicaladdressingdone#$nsuse-newtrace#设置tracesettracefd[openwired_wireless2.trw]setnamtrace[openwired_wireless2.namw]$nstrace-all$tracefd$nsnamtrace-all-wireless$namtrace$val(x)$val(y)#建立topology对象settopo[newTopography]$topoload_flatgrid$val(x)$val(y)#创建godcreate-god$val(nn)setsinkNode[$nsnode0.0.0]$sinkNodesetX_500$sinkNodesetY_300$sinkNodesetZ_0$nsinitial_node_pos$sinkNode60setchan_1_[new$val(chan)]#无线节点配置$nsnode-config-wiredRoutingON\-adhocRouting$val(rp)\-llType$val(ll)\-macType$val(mac

1 / 113
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功