NS 2.35 柯志亨书实验3笔记TCP UDP模拟

上图是笔记

下面是tcl代码:

View Code
#Create a simulator object
set ns [new Simulator]

#Set different color for dif flow
$ns color 1 Blue
$ns color 2 Red

set tracefd [open example1.tr w]
$ns trace-all $tracefd
set namtracefd [open example1.nam w]
$ns namtrace-all $namtracefd

proc finish {} {
        global ns tracefd namtracefd
        $ns flush-trace
        close $tracefd
        close $namtracefd
        exec nam example1.nam &
        exit 0
}

# Set nodes, s1's id is 0, s2'id is 1
set s1 [$ns node]
set s2 [$ns node]

# Set router node, id of r is 2
set r [$ns node]
# Set dest node, id of r is 3
set d [$ns node]

# Set link parameters
$ns duplex-link $s1 $r 2Mb   10ms DropTail
$ns duplex-link $s2 $r 2Mb   10ms DropTail
$ns duplex-link $r  $d 1.7Mb 20ms DropTail

# Set Queue limit 10 for r and d
$ns queue-limit $r $d 10

# Set Node pos for NAM
$ns duplex-link-op $s1 $r orient right-down
$ns duplex-link-op $s2 $r orient right-up
$ns duplex-link-op $r  $d orient right

# Observe the change of queue between r and d for NAM
$ns duplex-link-op $r $d queuePos 0.5

# Setup TCP agent and FTP traffic
set tcp [new Agent/TCP]
$ns attach-agent $s1 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $d $sink
$ns connect $tcp $sink

# In NAM, TCP will diplay in Blue
$tcp set fid_ 1

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


# Setup a UDP Agent and CBR Traffic
set udp [new Agent/UDP]
$ns attach-agent $s2 $udp
set null [new Agent/Null]
$ns attach-agent $d $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 packetSize_ 1000
$cbr set rate_ 1mb
$cbr set random_ false


# Setup time line
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 4.0 "$ftp stop"
$ns at 4.5 "$cbr stop"

$ns at 5.0 "finish"

$ns run

运行tcl后,会生.tr文件,利用awk分析.tr文件,awk代码如下,计算cbr_delay的awk

View Code
# Measure the end to end delay by the trace file

BEGIN{
    # program initialize
    highest_packet_id = 0;
}

{
    # awk会自动循环执行这个{}
    action = $1;
    time = $2;
    from = $3;
    to = $4;
    type = $5;
    pktsize = $6;
    flow_id = $8;
    src = $9;
    dst = $10;
    seq_no = $11;
    packet_id = $12;

    # Record the current max packet ID
    if ( packet_id > highest_packet_id )
        highest_packet_id = packet_id;

    # Record the tx time of packet
    if ( start_time[packet_id] == 0 )
        start_time[packet_id] = time;

    # Record CBR flow_id=2 rx time
    # 这里既要判断flow=2,没有drop,还要判断recv
    # drop是必须的,因为有可能1-2 recv,2-3 drop了
    # CBR 路径是1-2-3,整条路径上都有可能drop
    if ( flow_id == 2 && action != "d" )
    {
        if (action == "r")
        {
            end_time[packet_id] = time;
        }
    }
    else
        end_time[packet_id] = -1;
}

END {
# When read over, start to calculate
    for ( packet_id=0; packet_id<=highest_packet_id; packet_id++ )
    {
        start = start_time[packet_id];
        end = end_time[packet_id];
        duration = start-end;
        if (start<end)
            printf("%f %f\n", start, duration);
    }
}

执行awk的shell文件如下:

View Code
#!/bin/bash

file=cbr_throughput
awk -f $file.awk example1.tr > $file

执行gnuplot绘图的shell文件如下:

View Code
#!/bin/bash

file=cbr_throughput

gnuplot -persist<<EOF

set terminal gif
set output "$file.gif"
set title "$file"
set xlabel "simulation time"
set ylabel "time/s"
unset key

plot "$file" with linespoints

EOF

-persist<<EOF

能够阻止gnuplot弹出一堆东西,具体原因没有找到,找到了gnuplot的手册,但没看明白persist的意思手册解释如下:

To give gnuplot commands directly in the command line, using the "-persist" option so that the plot remains
on the screen afterwards:
gnuplot -persist -e "set title ’Sine curve’; plot sin(x)"

加persist之后,可以不用弹出一堆信息,即不会进入gnuplot的session

后面的<<EOF也不太清楚是什么意思?

gnuplot下有Batch session和interactive session,不知道这两个有什么区别???

cbr delay的图形

还有可以改进的地方,两个shell脚本,应该可以合并。。。

合并后的shell脚本:

View Code
#!/bin/bash

file=cbr_throughput

##########################
# awk
awk -f $file.awk example1.tr > $file


##########################
# gnuplot
gnuplot -persist<<EOF

#set terminal gif
#set output "$file.gif"
set title "$file"
set xlabel "simulation time"
set ylabel "time/s"
unset key

plot "$file" with linespoints

EOF
原文地址:https://www.cnblogs.com/yanhc/p/3073892.html