免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2780 | 回复: 0
打印 上一主题 下一主题

简单的ns2实验全过程-比较tcp和udp的丢包行为 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-24 17:36 |只看该作者 |倒序浏览

               
实验名称:比较tcp和udp的丢包行为
试验目的
1. 熟练用ns2做网络仿真试验的整个流程;
2. 练习写tcl脚本,了解怎么应用http和rtp;
3. 练习用awk处理trace数据,了解怎么计算丢包率;
4. 练习用gnuplot绘制曲线图,熟练gnuplot的使用。
实验步骤
1。确定网络拓扑。
   一个简单的三个节点的拓扑,两个运行cbr(const-bitrate)应用的发送结点,一个接收结点。一条链路使用tcp链接,一条链路使用udp连接。如图。

2。写tcl脚本。
# jiqing 2007-6-5
# this script is to compare the loss rates of http and rtp.
set ns [new Simulator]
#open a nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#open a trace file
set tf [open out.tr w]
$ns trace-all $tf
#finish procedure
proc finish {} {
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam out.nam &
exit 0
}
#create nodes
set node(http) [$ns node]
set node(rtp) [$ns node]
set node(recv) [$ns node]
#create links
$ns duplex-link $node(http) $node(recv) 0.9Mb 10ms DropTail
$ns duplex-link $node(rtp) $node(recv) 0.9Mb 10ms DropTail
#set queue size
$ns queue-limit $node(http) $node(recv) 10
$ns queue-limit $node(rtp) $node(recv) 10
#relayout nodes
$ns duplex-link-op $node(http) $node(recv) orient right-down
$ns duplex-link-op $node(rtp) $node(recv) orient right-up
#set colors
$ns color 1 blue
$ns color 2 red
#set a tcp connection
set tcp [new Agent/TCP]
$ns attach-agent $node(http) $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $node(recv) $sink
$ns connect $tcp $sink
$tcp set fid_ 1
#set a cbr above tcp connection
set cbr(http) [new Application/Traffic/CBR]
$cbr(http) attach-agent $tcp
$cbr(http) set type_ CBR
$cbr(http) set packet_size_ 1000
$cbr(http) set rate_ 1mb
$cbr(http) set random_ false
#set a rtp connection
set rtp [new Agent/UDP]
$ns attach-agent $node(rtp) $rtp
set null [new Agent/Null]
$ns attach-agent $node(recv) $null
$ns connect $rtp $null
$rtp set fid_ 2
#set a cbr above tcp connection
set cbr(rtp) [new Application/Traffic/CBR]
$cbr(rtp) attach-agent $rtp
$cbr(rtp) set type_ CBR
$cbr(rtp) set packet_size_ 1000
$cbr(rtp) set rate_ 1mb
$cbr(rtp) set random_ false
#schedule
$ns at 0.1 "$cbr(http) start"
$ns at 0.1 "$cbr(rtp) start"
$ns at 4.0 "$cbr(http) stop"
$ns at 4.0 "$cbr(rtp) stop"
$ns at 4.1 "finish"
$ns run
3。仿真。在命令提示符下输入ns http_vs_rtp.tcl,回车。仿真结束,会调用nam演示动画。
4。用awk处理trace数据。
   awk的语法和c很像,不同的是awk中使用变量不用事先声明。一个awk程序的结构如下面所示:
BEGIN{
...
}
{
...
}
END{
...
}
可见,程序分为三块,BEGIN只在程序进入时执行一次,END只在程序退出前执行一次。而中间的程序块会在处理每行数据时都执行一次。
这里用awk计算tcp和udp连接的丢包率,具体程序如下:
BEGIN {
tcp_droped = 0;
udp_droped = 0;
last_tcp = -1;
last_udp = -1;
total_tcp = 0;
total_udp = 0;
i = 0;
}
{
action = $1;
time = $2;
type = $5;
seq_no = $11;
if(action == "+" && type == "tcp"){
   total_tcp = total_tcp + 1;
}
if(action == "+" && type == "cbr"){
   total_udp = total_udp + 1;
}
if(action=="r"){
   if(type=="tcp"){
    if(seq_no != last_tcp + 1){
     tcp_droped = tcp_droped + 1;
    }
    last_tcp = seq_no;
   }
   if(type=="cbr"){
    if(seq_no != last_udp + 1){
     udp_droped = udp_droped + 1;
    }
    last_udp = seq_no;
   }
   time_point = time;
   if(total_tcp > 0)
    tcp_loss = tcp_droped / total_tcp * 100;
   else
    tcp_loss = 0;
   if(total_udp > 0)
    udp_loss = udp_droped / total_udp * 100;
   else
    udp_loss = 0;
   i = i + 1;
  
}
}
END {
printf("%.2f\t%.2f\t%.2f\n",0,0,0);
for(j=0;j  i; j++){
   printf("%.2f\t%.2f\t%.2f\n",time_point[j], tcp_loss[j], udp_loss[j]);
}
}
awk程序写好后,在命令行下输入awk -f measure_loss.awk > http_rtp.txt,将把printf输出的结果写入http_rtp.txt。参见http_rtp.txt。

。。。。。。。
0.91 6.45 0.00
0.91 6.45 0.00
0.92 6.38 0.00
0.92 6.38 0.00
0.92 6.38 0.00
0.93 6.32 0.96
0.93 6.32 0.96
0.93 6.32 0.95
0.94 6.25 0.95
0.94 6.25 0.94
0.94 6.25 0.94
0.95 6.19 0.94
0.95 6.19 0.93
0.95 6.19 0.93
0.95 6.12 0.93
0.96 6.12 0.93
。。。。。。。
。用gnuplot画图。
[color="#ff0000"]怎么进入和退出gnuplot?
开启xserver(运行startxwin.bat)后,输入gnuplot,将进入交互式的画图。如果要退出,输入quit。
http_rtp.txt中有三列数据,分别是时间、tcp丢包率、udp丢包率。
[color="#ff0000"]怎么控制gnuplot选择其中两列作为x,y轴的数据呢?
plot "filename" using 2将会把第二列作为y轴的数据,第一列默认是x轴的数据。
plot "filename" using 2:3将把第二列作为x轴的数据,第三列作为y轴的数据。
[color="#ff0000"]怎么在一张图中绘制多条线?
多个数据源之间用逗号隔开就可以了,如:
plot "filename1" using 2, "filename2" using 2:3, ...
[color="#ff0000"]怎么把图输出为图片?
set terminal gif //设置输出格式
set output "http_rtp.gif" //设置输出文件名
replot   //输出
[color="#ff0000"]怎么为图片增加说明?
set title "title" //设置标题
set xlabel "xlabel" //设置x轴说明
set ylabel "ylabel" //设置y轴说明
所以输入:$ [color="#0000ff"]gawk -f loss.awk out.tr >>http_loss.txt
然后[color="#0000ff"]gnuplot
接着:gnuplot> [color="#0000ff"]plot "http_loss.txt" using 1:2,"http_loss.txt" using 1:3

实验中遇到的困难及解决办法:
1。问题:不知道怎么使用rtp agent。
   解决:找不到关于这方面的资料,先用udp代替。
2。问题:不知道怎么使用http
   解决:关于http的使用ns-2手册 39.7web cache一节,可供借鉴。但是建立一个http应用比较麻烦,需要http/server, http/client, http/cache。这里用cbr来代替。
实验结果
1。当链路带宽设为1mb,tcp和udp速率都为1000时,tcp有丢包现象,udp没有丢包现象,这大概是因为tcp有确认报文,而udp没有的缘故。当把链路带宽都设定为0.9Mb,tcp和udp都有严重的丢包现象。可见虽然tcp有重传机制,但在带宽太低的情况下,仍然会丢包。
2。最终的曲线图说明,tcp的拥塞控制机制发挥了作用,丢包率逐步下降。而udp没有拥塞控制机制,当缓冲队列满后,丢包率迅速上升。
参考文献:
1. The ns Manual.
2. ja2 chung, mark claypool. ns by example.
3. 柯志亨的个人站点:
[color="#000000"]http://140.116.72.80/~smallko/


               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/21684/showart_1090977.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP