网络仿真器NS-2及其一个应用实例
- 格式:pdf
- 大小:690.41 KB
- 文档页数:5
Ns2简单有线网络仿真实验报告一、实验概述1、在windows系统下安装Centos虚拟机2、在Centos系统下安装NS2仿真工具包3、Nam方式示例二、实验内容1)NS2仿真工具包安装说明1.在Centos系统下设置root账户2.解压NS2文件3.安装ns-allinone-2.35设置环境变量4.验证NS2工具包安装情况2)Nam方式Otcl脚本# 产生一个仿真的对象set ns [new Simulator]#针对不同的资料流定义不同的颜色,这是要给NAM用的$ns color 1 Green$ns color 2 Red#开启一个NAM trace fileset nf [open out.nam w]$ns namtrace-all $nf#开启一个trace file,用来记录封包传送的过程set nd [open out.tr w]$ns trace-all $nd#定义一个结束的程序proc finish {} {global ns nf nd$ns flush-traceclose $nfclose $nd#以背景执行的方式去执行NAMexec nam out.nam &exit 0}#产生6个网络节点set n0 [$ns node]set n1 [$ns node]set n2 [$ns node]set n3 [$ns node]set n4 [$ns node]set n5 [$ns node]#把节点连接起来$ns duplex-link $n0 $n2 2Mb 10ms DropTail$ns duplex-link $n1 $n2 2Mb 10ms DropTail$ns duplex-link $n2 $n3 1.7Mb 20ms DropTail$ns duplex-link $n3 $n4 1.7Mb 30ms DropTail$ns duplex-link $n3 $n5 1.5Mb 30ms DropTail#设定ns2到n3之间的Queue Size为10个封包大小$ns queue-limit $n2 $n3 10$ns queue-limit $n3 $n4 5#$ns queue-limit $n3 $n5 10#设定节点的位置,这是要给NAM用的$ns duplex-link-op $n0 $n2 orient right-down$ns duplex-link-op $n1 $n2 orient right-up$ns duplex-link-op $n2 $n3 orient right$ns duplex-link-op $n3 $n4 orient right-up$ns duplex-link-op $n3 $n5 orient right-down#观测n2到n3之间queue的变化,这是要给NAM用的$ns duplex-link-op $n2 $n3 queuePos 0.5#建立一条n0-n5TCP的联机set tcp [new Agent/TCP]$ns attach-agent $n0 $tcpset sink [new Agent/TCPSink]$ns attach-agent $n5 $sink$ns connect $tcp $sink#在NAM中,TCP的联机会以Green表示$tcp set fid_ 1#在TCP联机之上建立FTP应用程序set ftp [new Application/FTP]$ftp attach-agent $tcp$ftp set type_ FTP#建立一条UDP的联机set udp [new Agent/UDP]$ns attach-agent $n1 $udpset null [new Agent/Null]$ns attach-agent $n3 $null$ns connect $udp $null#在NAM中,UDP的联机会以红色表示$udp set fid_ 2#在UDP联机之上建立CBR应用程序set cbr [new Application/Traffic/CBR]$cbr attach-agent $udp$cbr set type_ CBR$cbr set packet_size_ 1000$cbr set rate_ 1mb$cbr set random_ false#设定FTP和CBR资料传送开始和结束时间$ns at 0.1 "$cbr start"$ns at 1.0 "$ftp start"$ns at 4.0 "$ftp stop"$ns at 4.5 "$cbr stop"#结束TCP的联机(不一定需要写下面的程序代码来实际结束联机)$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"#在仿真环境中,5秒后去呼叫finish来结束仿真(这样要注意仿真环境中#的5秒并不一定等于实际仿真的时间$ns at 5.0 "finish"#执行仿真$ns run3)仿真结果仿真结束后,会产生out.nam和out.tr两个档案用来把仿真的过程用可视化的方式呈现出来4)数据分析1.End-to-End Delay把测量CBR封包端点到端点间延迟时间的awk程序,写在档案measure-delay.awk档案中BEGIN {#程序初始化,设定一变量以记录目前最高处理封包的ID。
一、NS2 程序简介对于如何验证网络协议的正确性和进行相关性能测试,人们提出了很多方法,目前最广泛使用的方法就是通过虚拟环境进行模拟仿真。
NS-2 是进行网络仿真最流行的软件,已广泛被科研院所和各大高校用于进行网络分析、研究和教学。
它支持众多的协议,并提供了丰富的测试脚本。
NS-2 全称是Network Simulator Version 2。
它是面向对象的,离散事件驱动的网络环境模拟器,主要用于解决网络研究方面的问题。
NS-2 提供在无线或有线网络上,TCP、路由、多播等多种协议的模拟。
NS-2最早来源与1989年的Real Network Simulator 项目,经过多年的发展之后,于1995年得到施乐公司(Xerox)的支持,加入VINT 项目。
NS 一直以来都在吸收全世界各地研究者的成果,包括UCB、CMU 等大学和SUN等公司的无线网络方面的代码。
NS-2 由两种编程语言,OTCL(具有面向对象特性的TCL 脚本程序设计语言)和C++实现。
之所以使用两种编程语言,是因为模拟器有两方面的事情需要做。
一方面,具体协议的模拟和实现,需要一种程序设计语言,能够高效率的处理字节(Byte),报头(Packet Header)等信息,能够应用合适的算法在大量的数据集合上进行操作。
为了实现这个任务,程序内部模块的运行速度(run-time speed)是非常重要的,而运行模拟环境的时间、寻找和修复bug 的时间,重新编译和运行的时间(run-around time)不是很重要。
这种情况下,C++语言是非常合适的。
另一方面,许多网络中的研究工作都围绕着网络组件和环境的具体参数的设置和改变而进行的,需要在短时间内快速的开发和模拟出所需要的网络环境(scenarios),并且方便修改和发现、修复程序中的Bug。
在这种任务中,网络环境布置的时间就显得很重要了,因为模拟环境的建立和参数信息的配置只需要运行一次。
网络模拟器NS-2及其应用分析本例将介绍如何使用一些工具来分析和呈现模拟结果,主要是测量端到端的延迟,而采用的方法一是去分析Trace文件,方法二是去修改NS核心,把所需要测量的数据直接记录下来,限于篇幅我们不在此进一步介绍方法二。
模拟场景如图4所示,包含四个节点(n0,n1,n2,n3),假设n0,n2和n1,n2之间链路带宽2Mbps,延迟为10ms;n2和n3之间链路带宽1.7Mbps,延迟为20ms;每个链路用DropT ail策略;n2和n3之间链路最大队列长度为10;n0,n3之间有一条建立在TCP上的FTP连接,n1,n3之间有一条建立在UDP上CBR连接;CBR是在0.1秒开始发送,在4.5秒结束,FTP是在1.0秒开始发送,在4.0秒结束。
图4 模拟场景3.1 建立Otcl模拟代码文件#创建一个模拟对象set ns [new Simulator]#为数据流定义不同的颜色,供NAM用$ns color 1 Blue$ns color 2 Red#打开一个NAM trace fileset nf [open out.nam w]$ns namtrace-all $nf#打开一个trace file 记录数据包的传送过程set nd [open out.tr w]$ns trace-all $nd#定义一个结束程序proc finish {} {global ns nf nd$ns flush-trace#关闭NAM trace fileclose $nfclose $nd#以后台方式执行NAMexec nam out.nam &exit 0}#创建四个节点set n0 [$ns node]set n1 [$ns node]set n2 [$ns node]set n3 [$ns node]#把节点连接起来$ns duplex-link $n0 $n2 2Mb 10ms DropTail $ns duplex-link $n1 $n2 2Mb 10ms DropTail $ns duplex-link $n2 $n3 1.7Mb 20ms DropTail #设定n2和n3之间最大队列长度为10$ns queue-limit $n2 $n3 10#设定节点的位置,供NAM用$ns duplex-link-op $n0 $n2 orient right-down $ns duplex-link-op $n1 $n2 orient right-up $ns duplex-link-op $n2 $n3 orient right#设定n2-n3间的队列位置,供NAM用$ns duplex-link-op $n2 $n3 queuePos 0.5#建立一条TCP连接set tcp [new Agent/TCP]$tcp set class_ 2$ns attach-agent $n0 $tcpset sink [new Agent/TCPSink]$ns attach-agent $n3 $sink$ns connect $tcp $sink#在NAM中,TCP的连接以蓝色表示$tcp set fid_ 1#在TCP连接之上建立FTP应用set ftp [new Application/FTP]$ftp attach-agent $tcp$ftp set type_ FTP#建立一条UDP连接set udp [new Agent/UDP]$ns attach-agent $n1 $udpset null [new Agent/Null]$ns attach-agent $n3 $null$ns connect $udp $null#在NAM中,UDP的连接以红色表示$udp set fid_ 2#在UDP连接之上建立CBR应用set cbr [new Application/Traffic/CBR]$cbr attach-agent $udp$cbr set type_ CBR$cbr set packet_size_ 1000$cbr set rate_ 1mb$cbr set random_ false# 设定FTP和CBR的开始和结束时间$ns at 0.1 "$cbr start"$ns at 1.0 "$ftp start"$ns at 4.0 "$ftp stop"$ns at 4.5 "$cbr stop"#在5.0秒调用finish过程结束模拟$ns at 5.0 "finish"#执行模拟$ns run本例子在FedoraCore4,ns-2.29下测试通过,模拟结束后,产生两个文件,一个是out.na m,这是供NAM用的,用来可视化整个模拟过程;另一个是out.tr,记录了模拟过程中数据包传送中的所有事件,这是我们分析的重点。
ns2的基本功能和用法。
NS2是一款广泛使用的离散事件网络仿真器,它可以用来模拟网络协议、网络拓扑结构、传输层协议等各种网络方面的问题。
在本文中,我们将会详细介绍NS2的基本功能和用法,让您了解如何使用这个强大的工具来开展网络仿真和探索。
第一部分:NS2的简介NS2全称Network Simulator 2,是一款免费且开源的网络仿真平台,它可以运行于Linux和Windows操作系统。
NS2是C++编写的,它是由一个模块化的体系结构构建而成的。
NS2可以帮助你模拟和测试各种网络协议和技术,包括但不限于TCP/IP、WiFi、无线通信、移动通信、卫星通信等。
NS2的基本组成部分包括:- OTcl:OTcl是一个面向对象的Tcl解释器,它被用来编写NS2的脚本文件。
它可以帮助您表示仿真模型以及控制仿真场景和参数。
- C++代码:NS2的模拟核心是由C++编写的,它包含了底层的网络协议处理逻辑和数据结构。
- Trace:NS2的Trace模块可以记录仿真过程中所有的事件和消息交换。
我们可以通过Trace来分析仿真结果,并对仿真场景进行可视化。
总结NS2是一个开源且强大的网络仿真器,它的核心部分是由C++编写而成的。
NS2可以帮助您模拟和测试各种网络协议和技术,并提供一个强大的OTcl 脚本语言来控制和配置仿真模型。
第二部分:NS2的基本功能NS2提供了很多强大的功能,如下所示:1. 拓扑结构模拟NS2可以帮助我们模拟各种网络拓扑结构,例如星型、树形结构、层次结构等。
通过定义节点、连接和协议,我们可以很容易地构建复杂的拓扑结构,并进行仿真和测试。
2. 参数设置和控制通过OTcl脚本,我们可以轻松地配置和控制仿真模型。
我们可以设置各种参数,例如发送速率、仿真持续时间、节点位置等等。
此外,我们还可以通过设置事件触发器来控制仿真场景的流程。
3. 模拟协议NS2可以帮助我们模拟各种协议,例如TCP、UDP、ICMP等。
2007,43(6Computer Engineering and Applications计算机工程与应用1引言移动自组网具有完全自组的运行方式和灵活的网络结构,可以真正地实现任何时间任何地点的网络互联通信,是一种无中心的网络,网络的带宽有限,而且网络拓扑动态变化的网络,这些特点决定了移动自组网在现实中应用的关键问题取决于路由问题的解决;而在真实的网络中,设计和调试网络协议是一件困难和复杂的事情,仿真成了最佳可供选择的测试、评估和验证手段之一,这不仅适用于网络模型的构造和设计、协议性能的评价与分析,还适用于网络协议的开发与研究以及真实网络的故障诊断。
网络模拟器NS2(Network Simulator v2是一种免费、源代码公开的模拟软件平台,其协议代码与真实网络应用代码很相似,仿真结果具有可靠性。
NS2是一个面向对象的离散事件驱动的网络模拟器,支持多个流行的网络协议如TCP、UDP和路由调度、拥塞控制算法等[1];相对于一般的离散型模拟器,NS2的优势在于它有非常丰富的构件库,而且这些对象易于组合,易于拓展,但是对用户的编程能力,实际网络协议的理解能力要求较高[2]。
在本文中,利用NS2已有的对象,组合所要研究的网络系统模型,在NS2中添加移动自组网中一个单播路由协议后进行网络仿真。
2NS2的仿真原理和拓展方法2.1NS2的仿真原理NS2采用编译和解释双层结构,编译层由C++类组成,C++语言执行速度快而修改和编译速度慢,故只用于编译层协议细节的实现;前端是一个OTCL(Object Tool Command Language解释器,OTCL修改快且可用于交互操作,用于解释层中仿真对象的设置,这种分裂对象仿真增强了构件库的可扩展性和可组合性[3]。
仿真器内核定义了有层次的编译类结构;在OTCL解释器中有相似的解释类结构。
用户通过解释器创立新的仿真对象之后,解释器对它进行初始化,与编译类结构中相应的对象建立映射。
NS-2仿真模拟实例汇总(以方路平的书为主)1.附录1 简单有线网络实例(p105)#Create a simulator objectset ns [new Simulator]#Define different colors for data flows$ns color 1 Blue$ns color 2 Red#Open the nam trace fileset nf [open out.nam w]$ns namtrace-all $nf#Define a 'finish' procedureproc finish {} {global ns nf$ns flush-trace#Close the trace fileclose $nf#Execute nam on the trace fileexec nam out.nam &exit 0}#Create four nodesset n0 [$ns node]set n1 [$ns node]set n2 [$ns node]set n3 [$ns node]#Create links between the nodes$ns duplex-link $n0 $n2 1Mb 10ms DropTail$ns duplex-link $n1 $n2 1Mb 10ms DropTail$ns duplex-link $n3 $n2 1Mb 10ms SFQ$ns duplex-link-op $n0 $n2 orient right-down$ns duplex-link-op $n1 $n2 orient right-up$ns duplex-link-op $n2 $n3 orient right#Monitor the queue for the link between node 2 and node 3 $ns duplex-link-op $n2 $n3 queuePos 0.5#Create a UDP agent and attach it to node n0set udp0 [new Agent/UDP]$udp0 set class_ 1$ns attach-agent $n0 $udp0# Create a CBR traffic source and attach it to udp0set cbr0 [new Application/Traffic/CBR]$cbr0 set packetSize_ 500$cbr0 set interval_ 0.005$cbr0 attach-agent $udp0#Create a UDP agent and attach it to node n1set udp1 [new Agent/UDP]$udp1 set class_ 2$ns attach-agent $n1 $udp1# Create a CBR traffic source and attach it to udp1set cbr1 [new Application/Traffic/CBR]$cbr1 set packetSize_ 500$cbr1 set interval_ 0.005$cbr1 attach-agent $udp1#Create a Null agent (a traffic sink) and attach it to node n3 set null0 [new Agent/Null]$ns attach-agent $n3 $null0#Connect the traffic sources with the traffic sink$ns connect $udp0 $null0$ns connect $udp1 $null0#Schedule events for the CBR agents$ns at 0.5 "$cbr0 start"$ns at 1.0 "$cbr1 start"$ns at 4.0 "$cbr1 stop"$ns at 4.5 "$cbr0 stop"#Call the finish procedure after 5 seconds of simulation time $ns at 5.0 "finish"#Run the simulation$ns run2.附录2简单无线网络实例(p108)# Define options# ========================================================= set val(chan) Channel/WirelessChannelset val(prop) Propagation/TwoRayGroundset val(netif) Phy/WirelessPhyset val(mac) Mac/802_11set val(ifq) Queue/DropTail/PriQueueset val(ll) LLset val(ant) Antenna/OmniAntennaset val(x) 1000 ;# X dimension of the topographyset val(y) 1000 ;# Y dimension of the topographyset val(cp) ""set val(sc) ""set val(ifqlen) 50 ;# max packet in ifqset val(nn) 3 ;# how many nodes are simulatedset val(seed) 0.0set val(stop) 100.0 ;# simulation timeset val(tr) exp.trset val(rp) DSDVset AgentTrace ONset RouterTrace ONset MacTrace OFF# ======================================================== # Main Program# ======================================================== set ns [new Simulator]$ns color 1 blue$ns color 2 red#$ns use-newtrace#set namfd [open nam-exp.tr w]#$ns namtrace-all-wireless $namfd $val(x) $val(y)#set tracefd [open exp.tr w]set tracefd [open exp.tr w]$ns trace-all $tracefdset namfd [open exp.nam w]$ns namtrace-all-wireless $namfd $val(x) $val(y)set topo [new Topography]$topo load_flatgrid $val(x) $val(y)set chan [new $val(chan)]set god [create-god $val(nn)]$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) \-channel $chan \-topoInstance $topo \-agentTrace ON \-routerTrace ON \-macTrace OFF \-movementTrace OFFfor {set i 0} {$i <$val(nn)} {incr i} {set node($i) [$ns node]$node($i) random-motion 0}$node(0) set X_ 350.0$node(0) set Y_ 500.0$node(0) set Z_ 0.0$node(1) set X_ 500.0$node(1) set Y_ 500.0$node(1) set Z_ 0.0$node(2) set X_ 650.0$node(2) set Y_ 500.0$node(2) set Z_ 0.0$god set-dist 1 2 1$god set-dist 0 2 2$god set-dist 0 1 1set god [God instance]#$ns at 10.0 " $node(1) setdest 490.0 100.0 2.0" #x坐标不能相同$ns at 10.0 " $node(1) setdest 500.1 100.0 2.0"$ns at 40.0 " $node(1) setdest 500.0 900.0 2.0"set udp(0) [new Agent/UDP]$udp(0) set fid_ 1$ns attach-agent $node(0) $udp(0)set null(0) [new Agent/Null]$ns attach-agent $node(2) $null(0)set cbr(0) [new Application/Traffic/CBR]$cbr(0) set packetSize_ 200$cbr(0) set interval_ 0.1$cbr(0) set random_ 1$cbr(0) set maxpkts_ 10000$cbr(0) attach-agent $udp(0)$ns connect $udp(0) $null(0)$ns at 10.0 "$cbr(0) start"#set udp(1) [new Agent/UDP]#$ns attach-agent $node(2) $udp(1)#set null(1) [new Agent/Null]#$ns attach-agent $node(1) $null(1)#set cbr(1) [new Application/Traffic/CBR]#$cbr(1) set packetSize_ 64#$cbr(1) set interval_ 0.1#$cbr(0) set random_ 1#$cbr(0) set maxpkts_ 10000#$cbr(1) attach-agent $udp(1)#$ns connect $udp(1) $null(1)#$ns at 10.0 "$cbr(1) start"for {set i 0} {$i<$val(nn)} {incr i} {$ns initial_node_pos $node($i) 30}for {set i 0} {$i < $val(nn)} {incr i} {$ns at $val(stop) "$node($i) reset";}#$ns at $val(stop) "puts \"NS EXITING\"; $ns halt"#$ns at $val(stop) "stop"puts $tracefd "M 0.0 nn $val(nn) x $val(x) y $val(y) rp $val(rp)" puts $tracefd "M 0.0 sc $val(sc) cp $val(cp) seed $val(seed)" puts $tracefd "M 0.0 prop $val(prop) ant $val(ant)"puts "Starting Simulation..."#proc stop {} {# global ns tracefd namfd# $ns flush-trace# close $tracefd# close $namfd# exec nam exp.nam &# exit 0#}proc finish {} {global ns tracefd namfd$ns flush-traceclose $tracefdclose $namfdexec nam exp.nam &exit 0 }$ns at $val(stop) "finish"$ns run3.TCP滑动窗口及慢启动算法模拟(P119)if {$argc==4} {set bandwidth [lindex $argv 0]set delay [lindex $argv 1]set window [lindex $argv 2]set time [lindex $argv 3]} else {puts "bandwidth"puts "n0----------n1"puts "TCP_window delay"puts "Usage:$argv0 bandwidth delay window simulation_time"exit 1}set ns [new Simulator]set nf [open out.nam w]$ns namtrace-all $nfset ftr [open slidewin.tr w]$ns trace-all $ftrproc finish {} {global ns nf$ns flush-traceclose $nfexec nam out.nam &exit 0}set n0 [$ns node]set n1 [$ns node]$ns duplex-link $n0 $n1 $bandwidth $delay DropTail$ns duplex-link-op $n0 $n1 orient left-rightset tcp [$ns create-connection TCP/RFC793edu $n0 TCPSink $n1 1] $tcp set window_ $window$tcp set ssthresh_ 60$tcp set packetSize_ 500set ftp [new Application/FTP]$ftp attach-agent $tcp$ftp set type_ FTP#set f0 [open cwndrecNoss.tr w]#proc Record {}#{# global f0 tcp ns# set intval 0.1# set now [$ns now]# set cwnd [$tcp set cwnd_]# puts $f0 "$now $cwnd"# $ns at [expr $now + $intval ] "Record"#}#$ns at 0.1 "Record"$ns at 0.5 "$ftp start"$ns at $time "finish"$ns run4.例4.1b:快速重传和快速恢复模拟(P123)set ns [new Simulator]set nf [open example4.1b1.nam w]$ns namtrace-all $nfset nd [open example4.1b1.tr w]$ns trace-all $ndproc finish {} {global ns nf nd$ns flush-traceclose $nfclose $ndexec nam example4.1b1.nam &exit 0}foreach i "0 1 2 3" {set n$i [$ns node]}$ns duplex-link $n0 $n1 5Mb 20ms DropTail$ns duplex-link $n1 $n2 0.2Mb 100ms DropTail$ns duplex-link $n2 $n3 5Mb 20ms DropTail$ns queue-limit $n1 $n2 5$ns duplex-link-op $n0 $n1 orient right-up$ns duplex-link-op $n1 $n2 orient right$ns duplex-link-op $n2 $n3 orient right-down$ns duplex-link-op $n1 $n2 queuePos 0.5set tcp [new Agent/TCP/Reno]$ns attach-agent $n0 $tcpset sink [new Agent/TCPSink]$ns attach-agent $n3 $sink$ns connect $tcp $sinkset ftp [new Application/FTP]$ftp attach-agent $tcp$ns at 0.0 "$n0 label TCP"$ns at 0.0 "$n3 label TCP"$ns at 0.0 "$ns trace-annotate \"TCP Reno: Fast Recovery\"" $ns at 0.1 "$ftp start"$ns at 5.0 "$ftp stop"$ns at 5.25 "finish"$tcp set nam_tracevar_ true$ns add-agent-trace $tcp tcp$ns monitor-agent-trace $tcp$tcp tracevar cwnd_$tcp tracevar ssthresh_$tcp tracevar maxseq_$tcp tracevar ack_$tcp tracevar dupacks_$ns run5.例4.2:LAN模拟示例(P130)set opt(tr) "out.tr"set opt(namtr) "lantest.nam"set opt(stop) 5set opt(node) 8set opt(qsize) 100set opt(bw) 10Mbset opt(delay) 10msset opt(ll) LLset opt(ifq) Queue/DropTailset opt(mac) Mac/802_3set opt(chan) Channelset opt(tcp) TCP/Renoset opt(sink) TCPSinkset opt(app) FTPproc finish {} {global ns opt trfdclose $trfdexec nam lantest.nam &exit 0}proc create-trace {} {global ns optif [file exists $opt(tr)] {catch "exec rm -f $opt(tr) $opt(tr) -bw [glob $opt(tr) * ]"}set trfd [open $opt(tr) w]$ns trace-all $trfdif {$opt(namtr) != ""} {$ns namtrace-all [open $opt(namtr) w]}return $trfd}proc create-topology {} {global ns optglobal lan node source node0set num $opt(node)for {set i 0} {$i<$num} {incr i} {set node($i) [$ns node]lappend nodelist $node($i)}set lan [$ns newLan $nodelist $opt(bw) $opt(delay) \-llType $opt(ll) \-macType $opt(mac) \-ifqType $opt(ifq) \-chanType $opt(chan)]set node0 [$ns node]$ns duplex-link $node0 $node(0) 2Mb 2ms DropTail$ns duplex-link-op $node0 $node(0) orient right}set ns [new Simulator]set trfd [create-trace]create-topologyset tcp0 [$ns create-connection TCP/Reno $node(7) TCPSink $node0 0] $tcp0 set window_ 32set ftp0 [$tcp0 attach-app FTP]set tcp1 [$ns create-connection TCP/Reno $node(2) TCPSink $node0 0] $tcp1 set window_ 32set ftp1 [$tcp1 attach-app FTP]set tcp2 [$ns create-connection TCP/Reno $node(4) TCPSink $node0 0] $tcp2 set window_ 32set ftp2 [$tcp2 attach-app FTP]$ns at 0.0 "$ftp0 start"$ns at 0.0 "$ftp1 start"$ns at 0.0 "$ftp2 start"$ns at $opt(stop) "finish"$ns run6.例4.3a:单播路由模拟(P141)set ns [new Simulator]$ns color 1 blue$ns color 2 redset file2 [open out.nam w]$ns namtrace-all $file2set file1 [open out.tr w]$ns trace-all $file1proc finish {} {global ns file1 file2$ns flush-traceclose $file1close $file2exec nam out.nam &exit 0}$ns rtproto DVset n0 [$ns node]set n1 [$ns node]set n2 [$ns node]set n3 [$ns node]set n4 [$ns node]set n5 [$ns node]set n6 [$ns node]$ns duplex-link $n0 $n1 0.3Mb 10ms DropTail $ns duplex-link $n1 $n2 0.3Mb 10ms DropTail $ns duplex-link $n2 $n3 0.3Mb 10ms DropTail $ns duplex-link $n1 $n4 0.3Mb 10ms DropTail $ns duplex-link $n3 $n5 0.5Mb 10ms DropTail$ns duplex-link $n1 $n6 0.5Mb 10ms DropTail $ns duplex-link $n6 $n5 0.5Mb 10ms DropTail#$ns duplex-link $n2 $n5 0.5Mb 10ms DropTail$ns duplex-link $n4 $n5 0.5Mb 10ms DropTail $ns duplex-link-op $n0 $n1 orient right$ns duplex-link-op $n1 $n2 orient right$ns duplex-link-op $n2 $n3 orient up$ns duplex-link-op $n1 $n4 orient up-left$ns duplex-link-op $n3 $n5 orient left-up$ns duplex-link-op $n1 $n6 orient up$ns duplex-link-op $n6 $n5 orient up#$ns duplex-link-op $n2 $n5 orient left-down $ns duplex-link-op $n4 $n5 orient right-up set tcp [new Agent/TCP/Newreno]$ns attach-agent $n0 $tcpset sink [new Agent/TCPSink/DelAck]$ns attach-agent $n5 $sink$ns connect $tcp $sink$tcp set fid_ 1set ftp [new Application/FTP]$ftp attach-agent $tcp$ftp set type_ FTP$ns rtmodel-at 2.0 down $n1 $n4$ns rtmodel-at 4.5 up $n1 $n4$ns rtmodel-at 3.0 down $n2 $n3$ns rtmodel-at 3.5 up $n2 $n3#$ns rtmodel-at 2.0 down $n1 $n2#$ns rtmodel-at 2.5 up $n1 $n2$ns rtmodel-at 1.0 down $n1 $n6$ns rtmodel-at 5.0 up $n1 $n6$ns at 0.1 "$ftp start"$ns at 6.0 "finish"$ns run7.例4.3b:多播路由模拟(P143)set ns [new Simulator]$ns multicast$ns color 1 blue$ns color 2 redset file2 [open out.nam w]$ns namtrace-all $file2set file1 [open out.tr w]$ns trace-all $file1proc finish {} {global ns file1 file2$ns flush-traceclose $file1close $file2exec nam out.nam &exit 0}set n0 [$ns node]set n1 [$ns node]set n2 [$ns node]set n3 [$ns node]set n4 [$ns node]set n5 [$ns node]#$n3 set X_ 65.0#$n3 set Y_ 50.0#$n3 set Z_ 0.0$ns duplex-link $n0 $n1 0.8Mb 20ms DropTail $ns duplex-link $n1 $n2 0.3Mb 10ms DropTail $ns duplex-link $n2 $n3 0.8Mb 10ms DropTail $ns duplex-link $n1 $n3 1.0Mb 10ms DropTail $ns duplex-link $n3 $n5 0.8Mb 10ms DropTail $ns duplex-link $n1 $n4 0.3Mb 10ms DropTail $ns duplex-link $n3 $n4 0.5Mb 10ms DropTail $ns duplex-link $n4 $n5 0.5Mb 10ms DropTail $ns duplex-link $n4 $n5 0.5Mb 10ms DropTail #$ns duplex-link-op $n3 $n1 orient down#$ns duplex-link-op $n3 $n2 orient down#$ns duplex-link-op $n3 $n4 orient left#$ns duplex-link-op $n3 $n5 orient right-up#$ns duplex-link-op $n1 $n0 orient right-down set group [Node allocaddr]DM set CacheMissMode dvmrpset mproto DMset mrthandle [$ns mrtproto $mproto]set udp1 [new Agent/UDP]$udp1 set fid_ 1set udp2 [new Agent/UDP]$udp2 set fid_ 2$ns attach-agent $n1 $udp1$ns attach-agent $n2 $udp2set cbr1 [new Application/Traffic/CBR]$cbr1 attach-agent $udp1$udp1 set dst_addr_ $group$udp1 set dst_port_ 0$cbr1 set random_ falseset cbr2 [new Application/Traffic/CBR]$cbr2 attach-agent $udp2$udp2 set dst_addr_ $group$udp2 set dst_port_ 0$cbr2 set random_ falseset rcvr [new Agent/LossMonitor]$ns at 0.6 "$n3 join-group $rcvr $group"$ns at 1.3 "$n4 join-group $rcvr $group"$ns at 1.6 "$n5 join-group $rcvr $group"$ns at 1.9 "$n4 leave-group $rcvr $group"$ns at 2.3 "$n5 join-group $rcvr $group"$ns at 3.5 "$n3 leave-group $rcvr $group"$ns at 0.1 "$cbr1 start"$ns at 0.1 "$cbr2 start"$ns at 6.0 "finish"$ns run8.例4.4:利用NS-2进行无线网络模拟(P152)set val(chan) Channel/WirelessChannelset val(prop) Propagation/TwoRayGroundset val(netif) Phy/WirelessPhyset val(mac) Mac/802_11set val(ifq) Queue/DropTail/PriQueueset val(ll) LLset val(ant) Antenna/OmniAntennaset val(x) 500 ;# X dimension of the topographyset val(y) 400 ;# Y dimension of the topographyset val(ifqlen) 50 ;# max packet in ifqset val(nn) 3 ;# how many nodes are simulated set val(stop) 150.0 ;# simulation timeset val(rp) DSDVset ns [new Simulator]set tracefd [open simple.tr w]set windowVsTime2 [open win.tr w]set namtrace [open simwrls.nam w]$ns trace-all $tracefd$ns namtrace-all-wireless $namtrace $val(x) $val(y) #proc finish {} {#global ns tracefd namtrace#$ns flush-trace#close $tracefd#close $namtrace#exec nam simwrls.nam &#exit 0 }set topo [new Topography]$topo load_flatgrid $val(x) $val(y)set chan [new $val(chan)]create-god $val(nn)$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) \-channel $chan \-topoInstance $topo \-agentTrace ON \-routerTrace ON \-macTrace OFF \-movementTrace OFFfor {set i 0} {$i <$val(nn)} {incr i} {set node_($i) [$ns node]}$node_(0) set X_ 5.0$node_(0) set Y_ 5.0$node_(0) set Z_ 0.0$node_(1) set X_ 490.0$node_(1) set Y_ 285.0$node_(1) set Z_ 0.0$node_(2) set X_ 150.0$node_(2) set Y_ 240.0$node_(2) set Z_ 0.0$ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0"$ns at 15.0 "$node_(1) setdest 45.0 285.0 5.0"$ns at 110.0 "$node_(0) setdest 480.0 300.0 5.0"set tcp [new Agent/TCP/Newreno]$tcp set class_ 2set sink [new Agent/TCPSink]$ns attach-agent $node_(0) $tcp$ns attach-agent $node_(1) $sink$ns connect $tcp $sinkset ftp [new Application/FTP]$ftp attach-agent $tcp$ns at 10.0 "$ftp start"proc plotWindow {tcpSource file} {global nsset time 0.01set now [$ns now]set cwnd [$tcpSource set cwnd_]puts $file "$now $cwnd"$ns at [expr $now+$time] "plotWindow $tcpSource $file" $ns at 10.1 "plotWindow $tcp $windowVsTime2"}for {set i 0} {$i<$val(nn)} {incr i} {$ns initial_node_pos $node_($i) 30}for {set i 0} {$i < $val(nn)} {incr i} {$ns at $val(stop) "$node_($i) reset";}$ns at $val(stop) "$ns nam-end-wireless $val(stop)" $ns at $val(stop) "stop"$ns at 150.01 "puts \" end simulation\" ;$ns halt"proc stop {} {global ns tracefd namtrace$ns flush-traceclose $tracefdclose $namtraceexec nam simwrls.nam &exit 0}$ns run9.例4.5a:DropTail队列管理的NS_2模拟实例(P161) set ns [new Simulator]set nf [open out.nam w]$ns namtrace-all $nfset tf [open out.tr w]set windowVsTime [open win w]set param [open parameters w]$ns trace-all $tfproc finish {} {global ns nf tf$ns flush-traceclose $nfclose $tfexec nam out.nam &exit 0}set n2 [$ns node]set n3 [$ns node]$ns duplex-link $n2 $n3 0.7Mb 20ms DropTailset NumbSrc 3set Duration 50for {set j 1} {$j <=$NumbSrc} {incr j} {set S($j) [$ns node]}set rng [new RNG]$rng seed 2set RVstart [new RandomV ariable/Uniform]$RVstart set min_ 0$RVstart set max_ 7$RVstart use-rng $rngfor {set i 1} {$i <=$NumbSrc} {incr i} {set startT($i) [expr [$RVstart value]]set dly($i) 1puts $param "startT($i) $startT($i) sec"}for {set j 1} {$j <=$NumbSrc} {incr j} {$ns duplex-link $S($j) $n2 10Mb $dly($j)ms DropTail$ns queue-limit $S($j) $n2 20}$ns queue-limit $n2 $n3 100for {set j 1} {$j <=$NumbSrc} {incr j} {set tcp_src($j) [new Agent/TCP/Reno]$tcp_src($j) set window_ 8000}for {set j 1} {$j<=$NumbSrc} {incr j} {set tcp_snk($j) [new Agent/TCPSink]}for {set j 1} {$j <=$NumbSrc} {incr j} {$ns attach-agent $S($j) $tcp_src($j)$ns attach-agent $n3 $tcp_snk($j)$ns connect $tcp_src($j) $tcp_snk($j)}for {set j 1} {$j <=$NumbSrc} {incr j} {set ftp($j) [$tcp_src($j) attach-source FTP]}for {set j 1} {$j <=$NumbSrc} {incr j} {$tcp_src($j) set packetSize_ 552}for {set i 1} {$i <=$NumbSrc} {incr i} {$ns at $startT($i) "$ftp($i) start"$ns at $Duration "$ftp($i) stop"}proc plotWindow {tcpSource file k} {global ns NumbSrcset time 0.03set now [$ns now]set cwnd [$tcpSource set cwnd_]if {$k==1} {puts -nonewline $file "$now \t $cwnd \t"} else {if {$k< $NumbSrc} {puts -nonewline $file "$cwnd \t"}}if {$k== $NumbSrc } {puts -nonewline $file "$cwnd \n"}$ns at [expr $now+$time] "plotWindow $tcpSource $file $k"} for {set j 1} {$j <= $NumbSrc} {incr j} {$ns at 0.1 "plotWindow $tcp_src($j) $windowVsTime $j"}$ns monitor-queue $n2 $n3 [open queue.tr w] 0.05[$ns link $n2 $n3] queue-sample-timeout$ns at [expr $Duration] "finish"$ns run10.例4.5b:RED队列管理的NS-2模拟实例(P164) set ns [new Simulator]set nf [open out.nam w]$ns namtrace-all $nfset tf [open out.tr w]set windowVsTime [open win w]set param [open parameters w]$ns trace-all $tfproc finish {} {global ns nf tf$ns flush-traceclose $nfclose $tfexec nam out.nam &exit 0}set n2 [$ns node]set n3 [$ns node]$ns duplex-link $n2 $n3 0.7Mb 20ms REDset NumbSrc 3set Duration 50for {set j 1} {$j <=$NumbSrc} {incr j} {set S($j) [$ns node]}set rng [new RNG]$rng seed 2set RVstart [new RandomV ariable/Uniform]$RVstart set min_ 0$RVstart set max_ 7$RVstart use-rng $rngfor {set i 1} {$i <=$NumbSrc} {incr i} {set startT($i) [expr [$RVstart value]]set dly($i) 1puts $param "startT($i) $startT($i) sec"}for {set j 1} {$j <=$NumbSrc} {incr j} {$ns duplex-link $S($j) $n2 10Mb $dly($j)ms DropTail$ns queue-limit $S($j) $n2 20}$ns queue-limit $n2 $n3 100for {set j 1} {$j <=$NumbSrc} {incr j} {set tcp_src($j) [new Agent/TCP/Reno]$tcp_src($j) set window_ 8000}for {set j 1} {$j<=$NumbSrc} {incr j} {set tcp_snk($j) [new Agent/TCPSink]}for {set j 1} {$j <=$NumbSrc} {incr j} {$ns attach-agent $S($j) $tcp_src($j)$ns attach-agent $n3 $tcp_snk($j)$ns connect $tcp_src($j) $tcp_snk($j)}for {set j 1} {$j <=$NumbSrc} {incr j} {set ftp($j) [$tcp_src($j) attach-source FTP]}for {set j 1} {$j <=$NumbSrc} {incr j} {$tcp_src($j) set packetSize_ 552}for {set i 1} {$i <=$NumbSrc} {incr i} {$ns at $startT($i) "$ftp($i) start"$ns at $Duration "$ftp($i) stop"}proc plotWindow {tcpSource file k} {global ns NumbSrcset time 0.03set now [$ns now]set cwnd [$tcpSource set cwnd_]if {$k==1} {puts -nonewline $file "$now \t $cwnd \t"} else {if {$k< $NumbSrc} {puts -nonewline $file "$cwnd \t"}}if {$k== $NumbSrc } {puts -nonewline $file "$cwnd \n"}$ns at [expr $now+$time] "plotWindow $tcpSource $file $k"} for {set j 1} {$j <= $NumbSrc} {incr j} {$ns at 0.1 "plotWindow $tcp_src($j) $windowVsTime $j"}set redq [[$ns link $n2 $n3] queue]set traceq [open red-queue.tr w]$redq trace curq_$redq trace ave_$redq attach $traceq$ns at [expr $Duration] "finish"$ns run$grep "a" red-queue.tr > ave.tr$grep "Q" red-queue.tr > cur.trgnplot > set title "current and average queue size"gnplot > set xlabel "time"gnplot > set ylabel "queue size"gnplot > plot ""11.例4.6:DiffServ在NS-2中的模拟实例(P174) set ns [new Simulator]set cir0 30000set cir1 30000set pktSize 1000set NodeNb 20set NumberFlows 160set sduration 25$ns color 1 Blue$ns color 2 Red$ns color 3 Green$ns color 4 Brown$ns color 5 Y ellow$ns color 6 Blackset fOut [open fOut.ns w]set Conn [open Conn.tr w]set tf [open fOut.tr w]$ns trace-all $tfset file2 [open fOut.nam w]$ns namtrace-all $file2set D [$ns node]set Ed [$ns node]set Core [$ns node]set flink [$ns simplex-link $Core $Ed 10Mb 1ms dsRED/core]$ns queue-limit $Core $Ed 100$ns simplex-link $Ed $Core 10Mb 1ms dsRED/edge$ns duplex-link $Ed $D 10Mb 0.01ms DropTailfor {set j 1} {$j <= $NodeNb} {incr j} {set S($j) [$ns node]set E($j) [$ns node]$ns duplex-link $S($j) $E($j) 6Mb 0.01ms DropTail$ns simplex-link $E($j) $Core 6Mb 0.1ms dsRED/edge$ns simplex-link $Core $E($j) 6Mb 0.1ms dsRED/core$ns queue-limit $S($j) $E($j) 100}#############################################set qEdC [[$ns link $Ed $Core] queue]$qEdC meanPktSize 40$qEdC set numQueues_ 1$qEdC set NumPrec 2$qEdC addPolicerEntry TSW2CM 10 11$qEdC addPHBEntry 10 0 0$qEdC addPHBEntry 11 0 1$qEdC configQ 0 0 10 30 0.1$qEdC configQ 0 1 10 30 0.1$qEdC printPolicyTable$qEdC printPolicerTablefor {set j 1} {$j <= $NodeNb} {incr j} {$qEdC addPolicyEntry [$D id] [$S($j) id] TSW2CM 10 $cir0 0.02 }set qCEd [[$ns link $Core $Ed] queue]$qCEd meanPktSize $pktSize$qCEd set numQueues_ 1$qCEd set NumPrec 2$qCEd addPHBEntry 10 0 0$qCEd addPHBEntry 11 0 1$qCEd set MREDMode RIO-D # the same as setMREDMode$qCEd configQ 0 0 15 45 0.5 0.01$qCEd configQ 0 1 15 45 0.5 0.01for {set j 1} {$j <= $NodeNb} {incr j} {set qEC($j) [[$ns link $E($j) $Core] queue]$qEC($j) meanPktSize $pktSize$qEC($j) set numQueues_ 1$qEC($j) set NumPrec 2$qEC($j) addPolicyEntry [$S($j) id] [$D id] TSW2CM 10 $cir1 0.02 $qEC($j) addPolicerEntry TSW2CM 10 11$qEC($j) addPHBEntry 10 0 0$qEC($j) addPHBEntry 11 0 1$qEC($j) configQ 0 0 10 20 0.1$qEC($j) configQ 0 1 10 20 0.1$qEC($j) printPolicyTable$qEC($j) printPolicerTableset qCE($j) [[$ns link $Core $E($j)] queue]$qCE($j) meanPktSize 40$qCE($j) set numQueues_ 1$qCE($j) set NumPrec 2$qCE($j) addPHBEntry 10 0 0$qCE($j) addPHBEntry 11 0 1$qEC($j) configQ 0 0 10 20 0.1$qEC($j) configQ 0 1 10 20 0.1}set monfile [open mon.tr w]set fmon [$ns makeflowmon Fid]$ns attach-fmon $flink $fmon$fmon attach $monfilefor {set i 1} {$i <= $NodeNb} {incr i} {for {set j 1} {$j<=$NumberFlows} {incr j} {set tcpsrc($i,$j) [new Agent/TCP/Newreno]set tcp_snk($i,$j) [new Agent/TCPSink]set k [expr $i*1000+$j]$tcpsrc($i,$j) set fid_ $k$tcpsrc($i,$j) set window_ 2000$ns attach-agent $S($i) $tcpsrc($i,$j)$ns attach-agent $D $tcp_snk($i,$j)$ns connect $tcpsrc($i,$j) $tcp_snk($i,$j)set ftp($i,$j) [$tcpsrc($i,$j) attach-source FTP]}}set rng1 [new RNG]$rng1 seed 22set RV [new RandomVariable/Exponential]$RV set avg_ 0.2$RV set use-rng $rng1set RVSize [new RandomV ariable/Pareto]$RVSize set avg_ 10000$RVSize set shape_ 1.25$RVSize use-rng $rng1set t [$RVSize value]for {set i} {$i<=$NodeNb} {incr i} {set t [$ns now]for{set j 1} {$j<=$NumberFlows} {incr j} {$tcpsrc($i,$j) set sess $j$tcpsrc($i,$j) set node $iset t [expr $t + [$RV value]]$tcpsrc($i,$j) set starts $t$tcpsrc($i,$j) set size [expr [$RVSize value]]$ns at [$tcpsrc($i,$j) set starts] "$ftp($i,$j) send [$tcpsrc($i,$j) set size]"$ns at [$tcpsrc($i,$j) set starts] "countFlows $i 1"}}#初始化计数器for {set j 1} {$j <= $NodeNb} {incr j} {set Cnts($j) 0}Agent/TCP instproc done {} {global tcpsrc NodeNb NumberFlows ns RV ftp fOut tcp_snk RVSizeset duration [expr [$ns now] - [$self set starts] ]set i [$self set node]set j [$self set sess]set time [$ns now]puts $fOut "$i \t $j \t $time \t\$time \t $duration \t [ $self set ndatapack_][$self set ndatabytes_] \t [$self set nrexmitbytes_] \t\[expr [$self set ndatabytes_]/$duration ]"countFlows [$self set node] 0}#定义一个计算机连接数目的递归过程proc countFlows { ind sign} {global Cnts Conn NodeNbset ns [Simulator instance]if { $sign==0} {set Cnts($ind) [expr $Cnts($ind) - 1]} elseif {$sign==1} {set Cnts($ind) [expr $Cnts($ind) + 1] } else {puts -nonewline $Conn " [$ns now] \t"set sum 0for {set j 1} {$j <= $NodeNb} {incr j} {puts -nonewline $Conn " $Cnts($j) \t"set sum [expr $sum + $Cnts($j)]}puts $Conn "$sum"$ns at [expr [$ns now] + 0.2] "countFlows 1 3" } }proc finish {} {global ns tf file2$ns flush-traceclose $file2exec nam fOut.nam &exit 0}$ns at 0.5 "countFlows 1 3"$ns at [expr $sduration - 0.01] "$fmon dump"$ns at [expr $sduration - 0.001] "$qCEd printStats"$ns at $sduration "finish"$ns run12.Muiticast模拟仿真# Polly Huang 8-7-98set ns [new Simulator]$ns multicastset f [open out.tr w]$ns trace-all $f$ns namtrace-all [open out.nam w]$ns color 1 red# prune/graft packets。
NS2网络模拟器的原理和应用1. NS2网络模拟器简介NS2(Network Simulator 2)是一个开源的网络模拟器,可以用于从高性能网络到无线移动网络的广泛应用。
NS2是一个离散事件模拟器,可以模拟真实网络环境中的各种网络协议和网络应用。
2. NS2网络模拟器的原理NS2的核心是基于离散事件的模拟器,它采用事件驱动模型来模拟网络中的各种事件。
NS2的模拟对象包括路由器、主机、链路等,每个对象都有自己的状态和行为。
NS2的模拟器通过以下步骤进行模拟:•创建拓扑结构:在NS2中,需要事先定义网络的拓扑结构,即网络中的节点和连接关系。
可以通过使用OTcl(Object TCL)脚本来定义网络拓扑。
•设置通信行为:在NS2中,可以设置节点之间的通信行为,包括传输协议、数据包大小、传输速率等等。
可以通过OTcl脚本来设置节点的通信属性。
•生成事件:NS2中的事件包括节点的发送、接收、路由更新等等。
可以通过OTcl脚本生成相应的事件。
•事件调度:NS2会根据事件发生的时间顺序来调度事件的处理。
在每个时间点,NS2会根据当前时间来决定下一个事件处理的顺序。
•事件处理:根据事件类型,NS2会调用相应的函数来处理事件。
比如,当一个节点发送数据包时,NS2会调用节点的发送函数来处理此事件。
3. NS2网络模拟器的应用NS2的应用非常广泛,主要应用于以下几个方面:3.1 网络协议研究NS2可以用于研究和评估各种网络协议的性能。
可以通过在网络拓扑中设置不同的协议参数和网络条件,来模拟和评估协议的性能指标,例如吞吐量、延迟、丢包率等。
3.2 网络性能优化NS2可以用于优化网络性能,通过对网络拓扑、协议参数和网络条件的调整来提高网络的性能。
可以通过计算模拟结果来评估不同优化策略的效果,从而选择最佳的优化方案。
3.3 网络设备设计和测试NS2可以用于设计和测试网络设备,例如路由器、交换机等。
可以通过在NS2中创建相应的网络拓扑,模拟网络设备的行为和性能,从而评估设备的可靠性和性能。