NS学习笔记

NS 基本概念
节点(node):相当于一个主机
代理(agent):相当于协议
应用(application):相当于应用程序


使用NS模拟网络时,需要先定义相关的节点set no [$ns node],然后定义节点间的链路带宽:$ns duplex-link $n0 $n1 1Mb 1ms  DropTail
发送数据就是在指定的协议上用指定的应用程序发送,这和网络上计算机之间通信很像

sample:

# The preamble
set ns [new Simulator]			# initialise the simulation;

# Predefine tracing
set f [open out.tr w]
$ns trace-all $f
set nf [open out.nam w]
$ns namtrace-all $nf

\clearpage
# so, we lied.  now, we define the topology
#
#       n0
#         \bs
#      5Mb \bs
#      2ms  \bs
#            \bs
#             n2 --------- n3
#            /     1.5Mb
#      5Mb  /       10ms
#      2ms /
#         /
#       n1
#
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

$ns duplex-link $n0 $n2 5Mb 2ms DropTail
$ns duplex-link $n1 $n2 5Mb 2ms DropTail
$ns duplex-link $n2 $n3 1.5Mb 10ms DropTail

# Some agents.
set udp0 [new Agent/UDP]		# A UDP agent;
$ns attach-agent $n0 $udp0		# on node $n0;
set cbr0 [new Application/Traffic/CBR]		# A CBR traffic generator agent;
$cbr0 attach-agent $udp0		# attached to the UDP agent;
$udp0 set class_ 0			# actually, the default, but\ldots;

set null0 [new Agent/Null]		# Its sink;
$ns attach-agent $n3 $null0		# on node $n3;

$ns connect $udp0 $null0
$ns at 1.0 "$cbr0 start"

puts [$cbr0 set packetSize_]
puts [$cbr0 set interval_]

# A FTP over TCP/Tahoe from $n1 to $n3, flowid 2
set tcp [new Agent/TCP]
$tcp set class_ 1
$ns attach-agent $n1 $tcp

set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink

set ftp [new Application/FTP]	# TCP does not generate its own traffic;
$ftp attach-agent $tcp
$ns at 1.2 "$ftp start"

$ns connect $tcp $sink
$ns at 1.35 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"

\clearpage
# The simulation runs for \(3s\).
# The simulation comes to an end when the scheduler invokes the finish{} procedure below.
# This procedure closes all trace files, and invokes nam visualization on one of the trace files.

$ns at 3.0 "finish"
proc finish {} {
        global ns f nf
        $ns flush-trace
        close $f
        close $nf

        puts "running nam..."
        exec nam out.nam &
        exit 0
}

# Finally, start the simulation.
$ns run



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值