ns2相关学习——TCL脚本编写(2)
下面来学习更加复杂一点的TCL脚本的编写
简述:建立有4个节点的拓扑,其中一个节点作为路由器,用来将两个节点发出的数据包转发到第四个节点上面。
在这里将解释将两个节点的数据流区分开来的方法,展示如何去检测一个队列是否是满的,以及一个数据包是如何被丢弃的。
1、建立拓扑
Tcl脚本大多是大同的,我们需要做的是在基础上进行自己需要的修改。基础代码如下:
#Create a simulator object
set ns [new Simulator]#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf#Define a 'finish' procedure
proc finish {} {global ns nf$ns flush-trace
#Close the trace fileclose $nf
#Execute nam on the trace fileexec nam out.nam &exit 0
}
# Insert your own code for topology creation
# and agent definitions, etc. here#Call the finish procedure after 5 seconds simulation time
$ns at 5.0 "finish"#Run the simulation
$ns run
做出修改如下:
① 添加四个节点:
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
② 添加链接
$ns duplex-link $n0 $n2 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n3 $n2 1Mb 10ms DropTail
其实上面这样就可以了,但是,为了让它更加美观,在跑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
在改变的时候我们可以使用right, left, up, down,以及这些单词的结合(结合方式:某-某)
2、添加事件
① 设置代理——建立起两个CBR代理和一个NULL代理。CBR代理给n0和n1,n0、n1用于发送数据,n3用于接收数据。
#Create a UDP agent and attach it to node n0
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0# Create a CBR traffic source and attach it to udp0
set 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 n1
set udp1 [new Agent/UDP]
$ns attach-agent $n1 $udp1# Create a CBR traffic source and attach it to udp1
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1set null0 [new Agent/Null]
$ns attach-agent $n3 $null0
② CBR代理和NULL代理进行连接
$ns connect $udp0 $null0
$ns connect $udp1 $null0
③ 设置数据流开始的时间和结束的时间
$ns at 0.5 "$cbr0 start"
$ns at 1.0 "$cbr1 start"
$ns at 4.0 "$cbr1 stop"
$ns at 4.5 "$cbr0 stop"
3、区别流
因为有俩发送端,为了区别数据流的情况,我们来给他们加点颜色;
$udp0 set class_ 1
$udp1 set class_ 2
$ns color 1 Blue
$ns color 2 Red
然后这样就udp0发出的数据流变蓝色,另外一个变红色。
4、查看链接的队列
添加下面的语句来监视链接的情况
$ns duplex-link-op $n2 $n3 queuePos 0.5
为了得到相对公平的情况,我们可以更换使用队列,
$ns duplex-link $n3 $n2 1Mb 10ms SFQ
SQF(随机公平排队)这样最后得到的是相对公平的结果
本节完完整代码如下:
#Create a simulator object
set ns [new Simulator]#Define different colors for data flows
$ns color 1 Blue
$ns color 2 Red#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf#Define a 'finish' procedure
proc 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 nodes
set 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 n0
set udp0 [new Agent/UDP]
$udp0 set class_ 1
$ns attach-agent $n0 $udp0# Create a CBR traffic source and attach it to udp0
set 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 n1
set udp1 [new Agent/UDP]
$udp1 set class_ 2
$ns attach-agent $n1 $udp1# Create a CBR traffic source and attach it to udp1
set 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 run
ns2相关学习——TCL脚本编写(2)相关推荐
- ns2相关学习——TCL脚本编写(3)
在这里我们将学习动态网络的建立 1.建立拓扑 当节点很多的时候,我们可以使用循环的方式来建立拓扑. for {set i 0} {$i < 7} {incr i} {set n($i) [$ns ...
- ns2相关学习——tcl脚本编写(1)
新建一个仿真实例: set ns [new Simulator] 为了让nam文件和trace文件有地方可以依托,我们要打开.nam文件进行写入,并且使用句柄nf set nf [open out.n ...
- NS2相关学习——在ns中模拟无线场景
之前学习的都是有线场景下的NS2相关应用,现在开始,终于要学习无线啦!无线是我研究的重点,要好好学习呀! 在本节中,我们将学习使用ns中提供的移动无线仿真模型. 该部分由两部分组成. 在第一小节中,我 ...
- NS2相关学习——创建Xgraph的输出文件
经过前面学习代码的编写,这一部分,我们要学会如何进行分析,一个很直观的方式就是将结果图形化表示出来. ns-allinone包的一部分是"xgraph",一个绘图程序,可用于创建模 ...
- 从0开始学习Jmeter脚本编写
本文章出自[码同学软件测试] 码同学公众号:自动化软件测试 码同学抖音号:小码哥聊软件测试 在实际工作中,Jmeter大多用来做接口的功能和性能测试.要做接口测试,首先要编写接口测试脚本,本节内容以实 ...
- NS2相关学习——完成一个新的协议(1)
接下来要进入对我来说老大难的环节了,从表面的TCL慢慢进入到后端的C++,一起加油学习吧~ 在本节学习中,将给出一个在ns中实现新的协议的例子.但是可以想见的是,如果由我们自己来完成这个工作,势必要对 ...
- NS2相关学习——完成一个新协议(3)
在前面已经基本学习了怎么完成一个新协议(一个神奇的ping协议,然鹅还是有点懵...) 接下来继续学习相关知识 接着上一部分从1开始 1.必要的修改 如果想要添加添加新的代理程序,就需要修改NS源文件 ...
- NS2相关学习——可靠的MANET应用程序的Gossip协议分析
好久不写,应该努力啦!老师把这篇论文给了我,现在还不知道它在讲什么,来边翻译边学习吧! 文章链接:https://www.researchgate.net/publication/316844643_ ...
- NS2相关学习——完成一个新协议(2)
在上节中,我们把教程要求的3.1-3.3过了一遍,这一次回到正途上来.看看到底是怎么完成一个新的协议的. 本节中的代码实现了一些简单的"ping"协议(灵感来自"ns注释 ...
最新文章
- unity 骨骼击碎_保证击碎$ 100挑战的创新策略
- Business Contact Mnanager for Outlook2010之二:常用基本功能
- MyBatis学习总结(一)——MyBatis快速入门
- ABAP 实现新会话新窗口方法
- Android随机生成四则运算
- mime java_MIME - Wei_java - 博客园
- kotlin编写后台_在Kotlin编写图书馆的提示
- OCP Java 自测
- Java设计模式-外观模式(Facade)
- Java 基础 之 赋值运算符
- 吴军《谷歌面试题:倒置英文句子》
- nginx高性能web服务器详解_Nginx服务器高性能优化轻松实现10万并发访问量
- 【游戏】基于matlab GUI音乐时钟设计【含Matlab源码 1104期】
- 初识UNIX操作系统
- 在线JSON转Excel工具
- ubuntu系统如何安装最新版R语言?
- 【解决问题】java.lang.ClassNotFoundException: com.Application 问题的几种解决方案
- ki51单片机流水灯c语言程序,STC89C51单片机流水灯程序
- python 桌面应用 h5_hdf 5文件格式及python中利用h5py模块读写h5文件
- 用计算机画有常数的函数图像,函数图像
热门文章
- mysql 报错 Specified key was too long; max key length is 767 bytes,开启系统变量:innodb_large_prefix
- 云服务和serverless
- 基础知识很扎实 - 但是面试就是做不出来, 怎么办? (长, 慎入)
- 现代软件工程系列 学生的精彩文章 (6) 项目总结
- Spring的AOP-基本概念
- java课外兴趣小组管理系统_课外兴趣小组活动方案
- vue实现进度条隐藏_实现带有进度条的Vue延迟加载
- python如何训练图片_Python练习第一题,在图片上加入数字
- 数据可视化demo_为更快读懂报表,我们将数据可视化了
- c++求n的几次方_14.八年级数学:若a+b+c=1,怎么求 a+b+c的值?分式培优拓展