Chinaunix首页 | 论坛 | 博客
  • 博客访问: 831895
  • 博文数量: 180
  • 博客积分: 10029
  • 博客等级: 上将
  • 技术积分: 2185
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-06 09:15
文章存档

2010年(133)

2009年(47)

我的朋友

分类: 系统运维

2009-11-06 11:27:12

E-mail:
 
  场景比较简单,主要是为了观察排队和丢包情况!
  关于NS2的网络仿真原理, 请看 NS2中文教材(1):NS2网络模拟的原理
  脚本的拓扑结构:
                  
 
  通过利用NAM演示功能,可以明显看到TCP和UDP机制的区别: 三次握手、ACK消息、慢启动等!
 
实例3:
        (以下的代码,在 入门实例1 中已进行了详细的注释)

# 场景描述:
# 主要用于观察数据包在节点Agent上的排队和丢包情况
# 有线场景,四个节点,FTT+TCP用蓝色表示, CBR+UDP用红色表示
set ns [new Simulator]
#设置颜色
$ns color 1 Blue
$ns color 2 Red
set tracefd [open out.tr w]
$ns trace-all $tracefd
set namtracefd [open out.nam w]
$ns namtrace-all $namtracefd

proc finish {} {
 global ns tracefd namtracefd
 $ns flush-trace
 close $tracefd
    close $namtracefd 
 exec 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 10ms DropTail
$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
$ns duplex-link-op $n2 $n3 queuePos 0.5

set tcp [new Agent/TCP]
$tcp set class_ 2
$ns attach-agent $n0 $tcp

set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
$tcp set fid_ 1

set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP

set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
$ns attach-agent $n3 $null
$ns connect $udp $null
$udp set fid_ 2

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 4.0 "$ftp stop"
$ns at 4.5 "$cbr stop"


#一下这一行可有可无!
$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"
$ns at 5.0 "finish"


#会在命令窗口中输出, 因为默认的stdout是命令窗口啊!
puts "CBR packet size=[$cbr set packet_size_]"
puts "CBR interval=[$cbr set interval_]"
$ns run

 
运行结果:
                                   只有CBR运行时:
  
 
CBR和FTP同时运行时:
丢包的情况:
 
 
 
E-mail:
阅读(5137) | 评论(3) | 转发(1) |
给主人留下些什么吧!~~

chinaunix网友2011-05-14 22:34:24

你好,如果我想模拟http协议,该如何编写代码啊?

chinaunix网友2010-05-26 09:50:50

我又改回来了

chinaunix网友2010-05-26 09:48:01

can,t read "tracefdfile4":no such variable while executing set tracefd [open out.tr w] (line 4)