简单ns2有线场景模拟
#建立一个模拟对象
set ns [new Simulator]
#定义不同数据流的颜色
$ns color 1 Blue
$ns color 2 Red
#开启Trace跟踪文件和NAM显示文件
set tracefd [open wired.tr w]
$ns trace-all $tracefd
set nf [open wired.nam w]
$ns namtrace-all $nf
#定义结束过程
proc finish {} {
global ns tracefd nf
$ns flush-trace
close $tracefd
close $nf
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 2Mb 10ms DropTail
#定义节点2和节点3的队列长度
$ns queue-limit $n2 $n3 10
#指定节点间的相互位置
$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
#监视队列状况,方便NAM显示时观察
$ns duplex-link-op $n2 $n3 queuePos 0.5
#建立第一个TCP连接
set tcp0 [new Agent/TCP]
$tcp0 set class_ 2
$ns attach-agent $n0 $tcp0
set sink0 [new Agent/TCPSink]
$ns attach-agent $n3 $sink0
$ns connect $tcp0 $sink0
$tcp0 set fid_ 1
#在TCP连接上建立FTP流
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ftp0 set type_ FTP
#建立第二个TCP连接
set tcp1 [new Agent/TCP]
$tcp1 set class_ 1
$ns attach-agent $n1 $tcp1
set sink1 [new Agent/TCPSink]
$ns attach-agent $n3 $sink1
$ns connect $tcp1 $sink1
$tcp0 set fid_ 2
#在TCP连接上建立FTP流
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ftp1 set type_ FTP
#设置代理启动和停止时间
$ns at 0.5 "$ftp0 start"
$ns at 1.0 "$ftp1 start"
$ns at 9.0 "$ftp0 stop"
$ns at 9.5 "$ftp1 stop"
$ns at 10.0 "finish"
$ns run
动作/时间/发送节点/接收节点/分组模型/分组大小/-------资料流/来源端/目的端/分组序号/分组id