文档介绍:第4章例1#建立一个模拟setns[newSimulator]#定义不同数据流的颜色(NAM显示时用到)$nscolor1Blue$nscolor2Red#开启Trace跟踪和NAM跟踪settracefd[]$nstrace-all$tracefdsetnf[]$nsnamtrace-all$nf#定义结束进程procfinish{}{ globalnstracefdnf $nsflush-trace close$tracefd close$nf exit0}#定义节点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$#建立一个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#启动和结束流代理$"$cbrstart"$"$ftpstart"$"$ftpstop"$"$cbrstop"$"$nsdetach-agent$n0$tcp;$nsdetach-agent$n5$sink"#仿真结束时调用结束进程$"finish"#打印CBR数据包的大小和间隔puts"CBRpacket_size_=[$cbrsetpacket_size_]"puts"CBRinterval=[$cbrsetinterval_]"#执行模拟$nsrun例2#简单无线网络模型模拟#无线节点参数setval(chan)Channel/WirelessChannel;#channeltype信道类型:无线信道setval(prop)Propagation/TwoRayGround;#radio-propagationmodel信道模型:if)Phy/WirelessPhy;#networkinterfacetype无线物理层setval(mac)Mac/802_11;#MACtypeMAC层协议setval(ifq)Queue/DropTail/PriQueue;#interfacequeuetypesetval(ll)LL;#linklayertypesetval(ant)Antenn