加入收藏 | 设为首页 | 会员中心 | 我要投稿 | RSS
您当前的位置:首页 > 原创教程 > NS2网络模拟与仿真

对含嘈声的链路的TCP协议仿真(转)

时间:2011-07-29 17:56:08  来源:  作者:

#Create a simulator object
set ns [new Simulator]

# tcp 0
# \
# \
# 2------------3
# /
# /
# cbr 1

#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red

#Open the NAM trace file
set nf [open out.nam w]
$ns namtrace-all $nf

#Open the Trace file
set tf [open out.tr w]
set windowVsTime2 [open WindowVsTimeNReno w]
$ns trace-all $tf

#Define a 'finish' procedure
proc finish {} {
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam out.nam &
exit 0
}

#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

$ns at 0.1 "$n1 label \"CBR\""
$ns at 1.0 "$n0 label \"FTP\""
$ns at 0.1 "$ns lable \"Rcv\""

#Create links between the nodes
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns simplex-link $n2 $n3 0.07Mb 20ms DropTail
$ns simplex-link $n3 $n2 0.07Mb 20ms DropTail

#set the location of these four nodes
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
#set the color of link
$ns duplex-link-op $n2 $n3 color "red"

#Set Queue Size of link (n2-n3) to 10
$ns queue-limit $n2 $n3 10

#Set error model on link n2 to n3.
set loss_module [new ErrorModel]
#uniform error rate in pkt or byte

#这里是设置数据包的丢包率,也就是说10个TCP数据包丢1个
$loss_module set rate_ 0.1
#set the underlying random variate generator
$loss_module ranvar [new RandomVariable/Uniform]
$loss_module drop-target [new Agent/Null]

#把lossmodel运用于哪个具体的链路
$ns lossmodel $loss_module $n2 $n3

#Monitor the queue for link (n2-n3). (for NAM)
$ns simplex-link-op $n2 $n3 queuePos 0.5

#Setup a TCP connection
set tcp [new Agent/TCP/Newreno]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink/DelAck]
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
#Setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
$tcp set fid_ 1

#Setup a UDP connection
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
#Be careful, node3 can rev tcp packets and udp packets
$ns attach-agent $n3 $null

$ns connect $udp $null
$udp set fid_ 2
#Setup a CBR over UDP connection
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set rate_ 0.01mb
$cbr set random_ false

#Schedule events for the CBR and FTP agents
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 10.0 "$ftp stop"
$ns at 11.5 "$cbr stop"

# Printing the window size
proc plotWindow {tcpSource file} {
global ns
set time 0.01
set now [$ns now]
set cwnd [$tcpSource set cwnd_]
puts $file "$now $cwnd"
$ns at [expr $now+$time] "plotWindow $tcpSource $file" }
$ns at 1.1 "plotWindow $tcp $windowVsTime2"

# sample the bottleneck queue every 0.1 sec. store the trace in qm.out
set qmon [$ns monitor-queue $n2 $n3 [open qm.out w] 0.1];
[$ns link $n2 $n3] queue-sample-timeout; # [$ns link $n2 $n3] start-tracing

#Detach tcp and sink agents (not really necessary)
$ns at 12 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"

$ns at 12.5 "finish"
$ns run
 

#===================================================

+ 1 0 2 tcp 40 ------- 1 0.0 3.0 0 2
- 1 0 2 tcp 40 ------- 1 0.0 3.0 0 2
r 1.01016 0 2 tcp 40 ------- 1 0.0 3.0 0 2
+ 1.01016 2 3 tcp 40 ------- 1 0.0 3.0 0 2
- 1.028286 2 3 tcp 40 ------- 1 0.0 3.0 0 2
r 1.048286 2 3 cbr 1000 ------- 2 1.0 3.1 1 1
r 1.052857 2 3 tcp 40 ------- 1 0.0 3.0 0 2
+ 1.152857 3 2 ack 40 ------- 1 3.0 0.0 0 3
- 1.152857 3 2 ack 40 ------- 1 3.0 0.0 0 3
r 1.177429 3 2 ack 40 ------- 1 3.0 0.0 0 3
+ 1.177429 2 0 ack 40 ------- 1 3.0 0.0 0 3
- 1.177429 2 0 ack 40 ------- 1 3.0 0.0 0 3
r 1.187589 2 0 ack 40 ------- 1 3.0 0.0 0 3

.........

#===================================================

上面这些数据是取自out.tr,40Bytes的TCP数据包为TCP协议的三次握手连接

另外,数据行的"r"表示的是对于该行的第4列(to node)代表的节点收到了数据包

注意:一般来说,一个数据包的流向轨迹为:入队(+) ----> 出队(-) ----> 接收(r)

来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
在CentOS下搭建Android 开发环境
在CentOS下搭建Androi
轻松搭建属于自己的Ubuntu发行版
轻松搭建属于自己的Ub
利用SUSE Studio 打造自己的个性化Linux发行版
利用SUSE Studio 打造
那些采用PHP技术的IT大企业
那些采用PHP技术的IT大
相关文章
栏目更新
栏目热门