LINUX下安装NS2的步骤

LINUX下安装NS2的步骤:

(1)下载NS2:

     从官网上下载NS2的allinone包,我下载的是ns-allinone-2.35.tar.gz。将下载好的压缩包放到/home目录下的一个文件夹(我自己创建了一个文件夹ns)中,并解压缩,生成文件夹ns-allinone-2.35。

(2)安装前下载一些编译必备的包:

# sudo apt-get install build-essential

# sudo apt-get install tcl8.5 tcl8.5-dev tk8.5 tk8.5-dev

# sduo apt-get install libxmu-dev libxmu-headers

(3)安装NS2:

     进入ns-allinone-2.35文件夹,输入命令sudo ./install安装NS2,直到安装成功。安装成功之后会提示要配置环境变量。

(4)配置环境变量:

     要配置的环境变量即为提示中的You MUST put后的路径。

输入命令sudo vim /home/.bashrc,在文件的末尾加入下面语句:

export PATH=$PATH:/home/ns/ns-allinone-2.35/bin:/home/ns/ns-allinone-2.35/tcl8.5.10/unix:/home/ns/ns-allinone-2.35/tk8.5.10/unix

export     LD_LIBRARY_PATH=/home/ns/ns-allinone-2.35/otcl-1.14:/home/ns/ns-allinone-2.35/lib

export      TCL_LIBRARY=/home/ns/ns-allinone-2.35/tcl8.5.10/library

保存后重启终端。

(5)测试NS2:

     中终端输入ns,出现%则说明安装成功。

NS2使用概述:

set ns [new Simulator]   #产生一个模拟器

$ns color 0 blue        #针对不同的资料流定义不同的颜色,这是给NAM用的

$ns color 1 red

$ns color 2 white

set n0 [$ns node]       #产生四个网络节点

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

set f [open out.tr w]     #开启一个trace file,用来记录封包传送的过程

$ns trace-all $f

set nf [open out.nam w]  #开启一个NAM trace file

$ns namtrace-all $nf

$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

$ns duplex-link-op $n0 $n2 orient right-up

$ns duplex-link-op $n1 $n2 orientright-down

$ns duplex-link-op $n2 $n3 orient right

$ns duplex-link-op $n2 $n3 queuePos 0.5

set udp0 [new Agent/UDP]   #建立两条UDP的连线

$ns attach-agent $n0 $udp0

set cbr0 [new Application/Traffic/CBR]

$cbr0 attach-agent $udp0

set udp1 [new Agent/UDP]

$ns attach-agent $n3 $udp1

$udp1 set class_ 1

set cbr1 [new Application/Traffic/CBR]

$cbr1 attach-agent $udp1

set null0 [new Agent/Null]

$ns attach-agent $n3 $null0

set null1 [new Agent/Null]

$ns attach-agent $n1 $null1

$ns connect $udp0 $null0

$ns connect $udp1 $null1

$ns at 1.0 "$cbr0 start"   #设定CBR资料传送开始时间

$ns at 1.1 "$cbr1 start

set tcp [new Agent/TCP]  #建立一条TCP的连线

$tcp set class_ 2

set sink [new Agent/TCPSink]

$ns attach-agent $n0 $tcp

$ns attach-agent $n3 $sink

$ns connect $tcp $sink

set ftp [new Application/FTP]

$ftp attach-agent $tcp

$ns at 1.2 "$ftp start"  #设定FTP传送开始时间

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

puts [$cbr0 set packetSize_]

puts [$cbr0 set interval_]

$ns at 3.0 "finish"

proc finish {} {   #定义一个结束的程序

       globalns f nf

       $nsflush-trace

       close$f

       close$nf

       puts"running nam..."

       execnam out.nam &

       exit0

}

$ns run #执行模拟

模拟截图:

猜你喜欢

转载自blog.csdn.net/b0207191/article/details/80733599