昨天学习了Tcl脚本的语法。今天开始学NS模拟,在一本书上找了一个例子,自己输入到电脑里。
内容如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
################################################################
# 简单有线网络模型模拟
################################################################
# NS仿真模拟必须建立一个模拟对象(Simulator类的对象)
set ns [new Simulator]
# 定义不同数据流的颜色
$ ns color 1 Blue
$ ns color 2 Red
# 开启Trace跟踪文件,记录分组传送的过程
set tracefd [open wired . tr w]
set trace -all $ tracefd
# 开启Nam显示文件
set nf [open wired . nam w]
$ ns namtrace-all $ nf
# 定义结束过程,关闭Trace文件和Nam显示文件,模拟结束时会进行调用
proc finish { } {
global ns tracefd nf
$ ns flush- trace
close $ tracefd
close $ nf
puts " running nam... "
exec nam wired . nam &
exit 0
}
# 定义节点n0~n5
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 $ n1 2Mb 10ms DropTail
$ ns duplex-link $ n1 $ n2 2Mb 10ms DropTail
$ ns duplex-link $ n1 $ n4 2Mb 20ms DropTail
$ ns duplex-link $ n3 $ n4 2Mb 10ms DropTail
$ ns duplex-link $ n4 $ n5 2Mb 10ms DropTail
# 定义关键链路(节点1和节点4之间的链路)的队列长度
$ ns queue-limit $ n1 $ n4 10
# 指定节点间的相互位置(节点的布局、Nam动画显示时使用)
$ ns duplex-link-op $ n0 $ n1 orient right-down ;# n1在n0的右下方
$ ns duplex-link-op $ n2 $ n1 orient right-up ;# n1在n2的右上方
$ ns duplex-link-op $ n1 $ n4 orient right ;# n1在n4的正右方
$ ns duplex-link-op $ n3 $ n4 orient left-down ;# n1在n0的左下方
$ ns duplex-link-op $ n5 $ n4 orient left-up ;# n1在n0的左上方
# 监视关键链路的队列状况,方便Nam显示时观察
$ ns duplex-link-op $ n1 $ n4 queuePos 0.5
# 建立一个TCP连接
set tcp [new Agent / TCP] ;# 建立一个TCP发送代理
$ tcp set class_ 2
$ ns attach-agent $ n0 $ tcp ;# 绑定TCP发送代理到节点0
set sink [new Agent / TCPSink] ;# 建立一个TCP接收代理
$ ns attach-agent $ n5 $ sink
$ ns connect $ tcp $ sink
$ tcp set fid_ 1 ;# 设置TCP分组颜色为蓝色
# 在TCP连接上建立FTP流
set ftp [new Application / FTP]
$ ftp attach-agent $ tcp
$ ftp set type_ FTP
# 建立一个UDP连接
set udp [new Agent / UDP]
$ ns attach-agent $ n2 $ udp
set null [new Agent / Null] ;# 建立一个UDP接收代理
$ ns attach-agent $ n3 $ null
$ ns connect $ udp $ null
$ udp set fid_ 2 ;# 设置UDP分组的颜色为红色
# 在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
# 设置代理的启动和停止时间
$ ns at 0.5 " $cbr start "
$ ns at 1.0 " $ftp start "
$ ns at 9.0 " $ftp stop "
$ ns at 9.5 " $cbr stop "
$ ns at 9.5 " ns detach-agent $n0 $tcp; $ns detach-agent $n5 $sink "
# 在模拟结束时调用结束过程
$ ns at 10.0 " finish "
# 打印CBR数据分组的大小和间隔
puts " CBR packet_size_ = [$cbr set packet_size_] "
puts " CBR interval = [$cbr set interval_] "
# 执行模拟
$ ns run
# 简单有线网络模型模拟
################################################################
# NS仿真模拟必须建立一个模拟对象(Simulator类的对象)
set ns [new Simulator]
# 定义不同数据流的颜色
$ ns color 1 Blue
$ ns color 2 Red
# 开启Trace跟踪文件,记录分组传送的过程
set tracefd [open wired . tr w]
set trace -all $ tracefd
# 开启Nam显示文件
set nf [open wired . nam w]
$ ns namtrace-all $ nf
# 定义结束过程,关闭Trace文件和Nam显示文件,模拟结束时会进行调用
proc finish { } {
global ns tracefd nf
$ ns flush- trace
close $ tracefd
close $ nf
puts " running nam... "
exec nam wired . nam &
exit 0
}
# 定义节点n0~n5
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 $ n1 2Mb 10ms DropTail
$ ns duplex-link $ n1 $ n2 2Mb 10ms DropTail
$ ns duplex-link $ n1 $ n4 2Mb 20ms DropTail
$ ns duplex-link $ n3 $ n4 2Mb 10ms DropTail
$ ns duplex-link $ n4 $ n5 2Mb 10ms DropTail
# 定义关键链路(节点1和节点4之间的链路)的队列长度
$ ns queue-limit $ n1 $ n4 10
# 指定节点间的相互位置(节点的布局、Nam动画显示时使用)
$ ns duplex-link-op $ n0 $ n1 orient right-down ;# n1在n0的右下方
$ ns duplex-link-op $ n2 $ n1 orient right-up ;# n1在n2的右上方
$ ns duplex-link-op $ n1 $ n4 orient right ;# n1在n4的正右方
$ ns duplex-link-op $ n3 $ n4 orient left-down ;# n1在n0的左下方
$ ns duplex-link-op $ n5 $ n4 orient left-up ;# n1在n0的左上方
# 监视关键链路的队列状况,方便Nam显示时观察
$ ns duplex-link-op $ n1 $ n4 queuePos 0.5
# 建立一个TCP连接
set tcp [new Agent / TCP] ;# 建立一个TCP发送代理
$ tcp set class_ 2
$ ns attach-agent $ n0 $ tcp ;# 绑定TCP发送代理到节点0
set sink [new Agent / TCPSink] ;# 建立一个TCP接收代理
$ ns attach-agent $ n5 $ sink
$ ns connect $ tcp $ sink
$ tcp set fid_ 1 ;# 设置TCP分组颜色为蓝色
# 在TCP连接上建立FTP流
set ftp [new Application / FTP]
$ ftp attach-agent $ tcp
$ ftp set type_ FTP
# 建立一个UDP连接
set udp [new Agent / UDP]
$ ns attach-agent $ n2 $ udp
set null [new Agent / Null] ;# 建立一个UDP接收代理
$ ns attach-agent $ n3 $ null
$ ns connect $ udp $ null
$ udp set fid_ 2 ;# 设置UDP分组的颜色为红色
# 在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
# 设置代理的启动和停止时间
$ ns at 0.5 " $cbr start "
$ ns at 1.0 " $ftp start "
$ ns at 9.0 " $ftp stop "
$ ns at 9.5 " $cbr stop "
$ ns at 9.5 " ns detach-agent $n0 $tcp; $ns detach-agent $n5 $sink "
# 在模拟结束时调用结束过程
$ ns at 10.0 " finish "
# 打印CBR数据分组的大小和间隔
puts " CBR packet_size_ = [$cbr set packet_size_] "
puts " CBR interval = [$cbr set interval_] "
# 执行模拟
$ ns run
一开始执行出错:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
--- Classfier
::
no-slot{} default handler (tcl/lib/ns-lib.tcl) ---
_o12: no target for slot - 1
_o12 type : Classifier / Hash / Dest
content dump:
classifier _o12
0 offset
0 shift
2147483647 mask
1 slots
slot 0 : _o37 ( Classifier / Port )
- 1 default
---------- Finished standard no-slot{} default handler ----------
_o12: no target for slot - 1
_o12 type : Classifier / Hash / Dest
content dump:
classifier _o12
0 offset
0 shift
2147483647 mask
1 slots
slot 0 : _o37 ( Classifier / Port )
- 1 default
---------- Finished standard no-slot{} default handler ----------
于是我从网止找解决方案,发现这个问题挺多的。其中有位仁兄更是不断发问,在他的一篇提问中,他提出一个问题,但后来自己解决了,然后回自己的贴子说如何解决的,过一会又发贴说出了另一个问题,又过一会自己又解决了……
这倒给我一个启示,我也能自己解决。
经过4个小时的煎熬,找到错误了:
我把
$ns attach-agent $n2 $udp
这句错抄成了
$ns set attach-agent $n2 $udp
更令人奇怪的是其他类似语句没抄错。
要是让尹老师知道,那得说我打字不过关。
最后脚本运行的效果还是不错的,截个图: