NS2 入门实例 1---有线网络,两节点(UDP 连接,CBR 应用)
实例 1:
# 场景描述:
# 两个节点 n0 和 n1 ,之间有一条 UDP 连接,
# 设置 CBR 应用,用作数据流发生器
# 模拟时间 5s
#建立一个 Simulator 对象, 并赋值给 ns, 用作模拟过程的时间调度
set ns [new Simulator]
#设置 trace 文件和 Nam 文件, trace 记录整个模拟过程的记录,
#Nam 用于 Nam 的动画显示
set tracefd [open example1.tr w]
$ns trace-all $tracefd
set namtracefd [open example1.nam w]
$ns namtrace-all $namtracefd
#定义结束过程, 将事件写入 trace, 并关闭相关文件, 最后启动 Nam 进行动画
显示
proc finish {} {
global ns tracefd namtracefd
$ns flush-trace
close $tracefd
close $namtracefd
exec nam example1.nam &
exit 0
}
#创建两个节点 n0 和 n1
set n0 [$ns node]
set n1 [$ns node]
#添加一条双向的连接, 传输速率: 1Mb, 时延: 10ms,
#队列管理方式: DropTail(丢弃尾部!)
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
#创建 UDP Agent 发送端, 并与 n0 节点关联
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
#创建 Null, 作为 UDP 的接收端,与 n1 关联
#Null:只接受, 不回复 ACK (与 TCP 和 TCPSink 的区别)
set null0 [new Agent/Null]