⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 一个完整ns脚本程序.txt

📁 采用NS2进行的网络流量的仿真,linux环境下执行通过.
💻 TXT
字号:
一个完整NS脚本程序
set ns [new Simulator]
$ns color 1 Blue
$ns color 2 Red
set nf [open out1.nam w]
$ns namtrace-all $nf
set tf [open out.tr w]
$ns trace-all $tf
proc finish {} {
        global ns tf 
        $ns flush-trace
        close $nf
        close $tf
        exec nam out.nam &
        exit 0
}
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
#band 是网络带宽,delay是该连路的延迟
set band 1
set var1 0.2
set var2 0.2
#set var_user 0.1
set delay 10ms
# normal 是正常流量速率,attack1,attack2是攻击流量速率
set normal [expr $var_user*$band]mb
set attack1 [expr $var1*$band]mb 
set attack2 [expr $var2*$band]mb
set bandwith [expr 1*$band]mb
set queuelim 60
$ns duplex-link $n0 $n3 $bandwith $delay DropTail 
$ns duplex-link $n1 $n3 $bandwith $delay DropTail
$ns duplex-link $n2 $n3 $bandwith $delay DropTail
$ns duplex-link $n3 $n4 $bandwith $delay DropTail
$ns queue-limit $n3 $n4 $queuelim
$ns duplex-link-op $n0 $n3 orient right-down
$ns duplex-link-op $n1 $n3 orient right-up
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n3 $n4 orient right
$ns duplex-link-op $n1 $n3 color "Red"
$ns duplex-link-op $n2 $n3 color "Red"
$ns duplex-link-op $n3 $n4 color "Green"
$ns duplex-link-op $n3 $n4 queuePos 0.5
set tcp0 [new Agent/TCP/Reno]
$ns attach-agent $n0 $tcp0
set sink0 [new Agent/TCPSink]
$ns attach-agent $n4 $sink0
$ns connect $tcp0 $sink0
$tcp0 set fid_ 0
$tcp0 set packetSize_ 1000
set udp1 [new Agent/UDP]
$ns attach-agent $n1 $udp1
set null1 [new Agent/Null]
$ns attach-agent $n4 $null1
$ns connect $udp1 $null1
$udp1 set fid_ 1
set udp2 [new Agent/UDP]
$ns attach-agent $n2 $udp2
set null2 [new Agent/Null]
$ns attach-agent $n4 $null2
$ns connect $udp2 $null2
$udp2 set fid_ 2
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ftp0 set type_ FTP
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1
$cbr1 set type_ CBR
$cbr1 set packet_size_ 500
$cbr1 set rate_ $attack1
$cbr1 set random_ false
set cbr2 [new Application/Traffic/CBR]
$cbr2 attach-agent $udp2
$cbr2 set type_ CBR
$cbr2 set packet_size_ 500
$cbr2 set rate_ $attack2
$cbr2 set random_ false
$ns at 0.1 "$ftp0 start"
$ns at 0.2 "$cbr2 start"
$ns at 0.3 "$cbr1 start"
$ns at 35 "$cbr1 stop"
$ns at 40 "$cbr2 stop"
$ns at 45 "$ftp0 stop"
$ns at 50 "finish"
#Print CBR packet size and interval
puts "tcp packet size = [$tcp0 set packetSize_]"
puts "window = [$tcp0 set window_]"
puts "attack1 =$attack1"
puts "attack2 =$attack2"
puts "bandwith = $bandwith"
#Run the simulation
$ns run

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -