网络协议模拟NS2课程设计报告

一、 课程设计目的
掌握网络模拟工具NS2的使用,学习基本的网络模拟方法。
二、 课程设计内容
协议模拟
工具:NS2,awk,shell,perl等;
要求:掌握NS2网络模拟的基本流程;
内容:NS2网络模拟基本流程
编写TCL脚本,搭建如下图所示的一个网络,共6个节点,其中2、3节点用做ftp服务器和客户端,4、5节点用做cbr流量的源和目的,而0、1节点用做转发设备。各节点间的链路属性见图。
在这里插入图片描述
模拟时间设为13秒钟,在0.1秒开始产生cbr流量,在1.0秒开发发送发ftp流量;8.0秒ftp流量结束,12.0秒cbr流量结束。编写脚本(可用shell,awk,或perl等)分析模拟日志文件,统计每0.5s内0、1节点间链路通过的分组数以及字节数。
三、 设计与实现过程
1.仿真脚本代码与详细注解

#Create a simulator object
set ns [new Simulator]   
#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]  
$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] 
set n4 [$ns node] 
set n5 [$ns node] 
 
#Create links between the nodes 
$ns duplex-link $n0 $n2 1.5Mb 10ms DropTail 
$ns duplex-link $n0 $n4 1.5Mb 10ms DropTail 
$ns duplex-link $n1 $n3 1.5Mb 10ms DropTail 
$ns duplex-link $n1 $n5 1.5Mb 10ms DropTail 
$ns duplex-link $n0 $n1 2Mb 20ms DropTail 

#定义节点位置,可选配置for NAM
$ns duplex-link-op $n2 $n0 orient right-down
$ns duplex-link-op $n4 $n0 orient right-up
$ns duplex-link-op $n1 $n3 orient right-up
$ns duplex-link-op $n1 $n5 orient right-down
$ns duplex-link-op $n0 $n1 orient right
$ns duplex-link-op $n0 $n1 orient right
 
#Set Queue Size of link (n0-n1) to 10 
$ns queue-limit $n0 $n1 10 
 
#Setup a TCP connection 
set tcp [new Agent/TCP] 
$ns attach-agent $n2 $tcp 
set sink [new Agent/TCPSink] 
$ns attach-agent $n3 $sink 
$ns connect $tcp $sink 
$tcp set fid_ 1 
 
#Setup a UDP connection 
set udp [new Agent/UDP] 
$ns attach-agent $n4 $udp 
set null [new Agent/Null] 
$ns attach-agent $n5 $null
$ns connect $udp $null 
$udp set fid_ 2 
 
#Setup a FTP over TCP connection 
set ftp [new Application/FTP] 
$ftp attach-agent $tcp 
$ftp set type_ FTP 
 
#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_ 1mb 
$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 8.0 "$ftp stop" 
$ns at 12.0 "$cbr stop" 
 
#Call the finish procedure after 13 seconds of simulation time 
$ns at 13.0 "finish" 
 
#Run the simulation
$ns run

2.仿真过程示意
在这里插入图片描述
3.日志分析脚本设计(设计思路与代码、注解)

#!/bin/bash
time=0      #记录时间
byte=0      #记录字节数
pack=0      #记录包数
flag=0      #判断是否间隔0.5秒的标志
i=0.5       #以0.5单位递增
#将out.tr文件中包含“0 1 cbr +”字符的行取出,并且以空格为分解符取出第二第六块数据,逐行读取到line变量中
cat out.tr | grep "0 1 cbr" | grep "+" | cut -d' ' -f 2,6 | while read line
do
    time=${line% *}    #赋予时间
    byte=$(($byte+${line#* }))  #统计字节和
    pack=$(($pack+1))   #统计总的包数
    flag='echo "$time>$i" | bc'  #判断是否间隔了0.5秒
    if [ $flag -eq "1" ]
   then 
  echo $pack $byte >> log1.txt  #将结果写入到日志文件
  pack=0
  byte=0
  i='echo "$i + 0.5" | bc'
  fi
 done

4.分析结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值