NS2 has two modes of operation:
1. "Scripting Mode", input command: NS TCLSCRIPL.TCL, where TCLSCRIPL.TCL is the file name of a TCL script;
2 "command line mode", enter command: NS, enter NS2 command line environment, then enter various commands directly to run NS2 interactively. (similar to Python)
There are two ways to record results after running NS2:
1.trace file;
2.nam (Animated demo program)
Tcl Script 1:example1.tcl
1 set NS [new Simulator] #建立一个新的模拟对象simulator2 3Set TRACEF [Open example1.TR W] #变量tracef指向example1. tr file4#ns trace-All $TACEF # Record the simulation process trace data5Set NAMTF [Open Example1.namW] #变量namtf指向example1. nam File6$ns namtrace-All $NAMTF # record Nam's trace data7 8 proc Finish {} {Close two trace files, call Nam program after simulation ends9 Global NS TRACEF NAMTFTen$ns flush-Trace One Close $tracef A Close $NAMTF -exec Nam Example1.nam & -Exit0 the ) - - set N0 [$ns node] #创建节点n0 - set n1 [$ns node] #创建节点n1 + -$ns duplex-link $n 0 $n 1 1Mb 10ms droptail# establish a two-way link between n0 and N1, bandwidth 1mbit/s, delay 10ms, queue type is droptail. + ASet udp0 [New agent/UDP] #创建一个udp0 Agent at$ns attach-Agent $no $udp 0# bind udp0 to N0 - -Set CBR0 [New application/traffic/CBR] #创建一个CBR流量发生器 -$CBR 0 Set Packetsize_ -#分组大小为500B -$CBR 0 Set Interval_0.005#发送间隔为5ms -$CBR 0 attach-agent $udp 0# to bind CBR to Udp0 in -Set null0 [New agent/Null] #创建一个Null agent as a data receiver to$ns attach-Agent $n 1 $null 0# bound to N1 + - Connect two agents $ns connect $UDP 0 $null 0#. the *$ns at0.5 "$CBR 0 Start"#0. Start Cbr0 at 5s $$ns at4.5 "$CBR 0 Stop"#4. Stop Cbr0 at 5sPanax Notoginseng -$ns at5.0 "Finish"#5. 0s when the finish process is called the$ns run# Start Simulation
The format of the TCL call member method is: $ object Name Method name
Tcl Script 2 (Wireless model): WIRELESS.TCL
1Set Val (Chan) channel/wirelesschannel #channel Type,val (chan) as the variable name is a whole, the same as2Set Val (proc) Propagation/tworayground #radio-Propagation Model3Set Val (netif) phy/wirelessphy #network Interface type4Set Val (Mac) mac/802_11 #MAC Type5Set Val (IFQ) queue/droptail/priqueue #interface Queue type6 set Val (ll) ll #link layer type7Set Val (ant) antenna/Omniantenna #antenna Model8Set Val (Ifqlen) -#max PacketinchIFQ9Set Val (NN)2#number of MobilenodesTen set Val (RP) AODV #routing Protocol OneSet Val (x) -#X Dimension of the topography ASet Val (y) -#Y Dimension of the topography - - set NS [New Simulator] the -Set TRACEFD [Open Wireless.TR W] -$ns trace-All $tracefd -Set NAMTRACEFD [New Wireless.namW] +$ns namtrace-all-Wireless $NAMTRACEFD $val (x) $val (y) - + set topo [new topography] #建立一个Topography对象, which guarantees the node to move within the topological boundary A $topo Load_flatgrid $val (x) $val (y) #设定模拟所用场景的大小 at -create-God $val (NN) #建立一个God对象, mainly used for performance evaluation of routing protocols; The Mac object of the node invokes God object, so even if we don't apply God objects here, we still need to create a god object - -$ns Node-config-adhocrouting $val (RP) \ #配置节点参数 --Lltype $val (LL) --Mactype $val (MAC) in-ifqtype $val (IFQ) --Ifqlen $val (Ifqlen) to-anttype $val (ANT) +-propType $val (prop) --phytype $val (netif) the-Channeltype $val (chan) *-topoinstance $topo $-Agenttrace onPanax Notoginseng-Routertrace on --mactrace OFF the-movementtrace OFF + A for{Set I0} {$i <$val (NN)} {INCR i} { the set Node_ ($i) [$ns node] +$node _ ($i) random-motion0#disable Random motion prohibits a node from moving randomly, allowing the node to move in a script-defined way - $$node _ (0) Set X_5.0 #设定节点初始坐标X, Y,z $$node _ (0) Set Y_2.0 -$node _ (0) Set Z_0.0 - the$node _ (1) Set X_390.0 -$node _ (1) Set Y_385.0Wuyi$node _ (1) Set Z_0.0 the -$ns at5.0 "$node _ (1) setdest 25.0 20.0 15.0"#节点1在5s时以15m/s speed-to-coordinate (25.0,20.0) point motion Wu$ns at1.0 "$node _ (0) setdest 20.0 18.0 1.0" - About$ns atTen "$node _ (1) setdest 490.0 480.0 15.0" $ -Set TCP [New agent/TCP] -$tcp Set Class_2 -Set sink [New agent/Tcpsink] A$ns Attach-agent $node _ (0) $tcp +$ns Attach-agent $node _ (1) $sink the $ns Connect $tcp $sink -SetFTP[New appication/FTP] $$FTPattach-Agent $tcp the the$ns at1.0 "$ftp Start" the for{Set I0} {$i <$val (NN)} {INCR i} { the$ns at15.0 "$node _ ($i) Reset" - } in$ns at15.0 "Stop" the the proc stop{} { About Global NS TRACEFD NAMETRACEFD the$ns flush-Trace the Close $TRACEFD the Close $NAMETRACEFD +exec Nam Wireless Nam & -Exit0 the }Bayi the$ns Run
In the case of many nodes, the initial position and movement of these nodes can be configured through files.
SOURCE "path" #读取定义位置和运动方式的文件, path is the pathname of the file, and the contents of the file include node's set method to set the initial coordinates, and simulator's at method to set the motion mode. It is consistent with the method of writing the script directly above.
You can also use this reference file to set the agent for the binding node.
NS2 Study Notes (i)