Trace File in Ns2

       In network, trace file is written through the applications that are deployed to be stored in the whole information. The generation of trace file in Ns2 needs the file that is written through the OTcl script. The network scenario is simulated through the Ns2 users and they required writing a simulation script and saving to invoke the Ns2 interpreter. In addition, there are two types of trace files such as normal trace file and network animation (NAM) trace file. The following is about the process of creating the trace file as the step by step functions for your reference.

Create Trace File

  • Create a file for writing
  • Open command is used to open the file in Tcl and the command is highlighted below
open < filename> < purpose>
  • Tcl statement is opened the file namely “” and that is deployed to write and store the file as mentioned in the below command
set var [ open w ]
  • Record tracing information in open trace file
  • It is about the process of record trace in opened file and it acquired over the utilization of Tcl statement
$ns trace-all $var

Format of Trace String

      In the following, our research respects are enlisted the highlights of required files in the process of formatting the trace string.

  • Type identifier
  • Time
  • Source and destination code
  • Packet name
  • Packet size
  • Flags
  • Flow id
  • Address of source and destination
  • Sequence number
  • Packet unique id

Trace Analysis Example

       The topology of network is about the generation and simulation of scenario and it has been highlighted in the below mentioned process based simple simulation script.

#Create a simulator object
set ns [new Simulator]
#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
#Open the NAM trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Open the Trace file
set tf [open w]
$ns trace-all $tf
#Define a 'finish' procedure
proc finish {} {
global ns nf tf
$ns flush-trace
#Close the NAM trace file
close $nf
#Close the Trace file
close $tf
#Execute NAM on the trace file
exec nam out.nam &
exit 0
#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
#Create links between the nodes
$ns duplex-link $n0 $n1 2Mb 10ms DropTail
#Set Queue Size of link (n0-n1) to 10
$ns queue-limit $n0 $n1 10
#Give node position (for NAM)
$ns duplex-link-op $n0 $n1 orient right-down
#Monitor the queue for link (n0-n1). (for NAM)
$ns duplex-link-op $n0 $n1 queuePos 0.5
#Setup a TCP connection
set tcp [new Agent/TCP]
$tcp set class_ 2
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n1 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
#Setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
#Setup a UDP connection
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
$ns attach-agent $n0 $null
$ns connect $udp $null
$udp set fid_ 2
#Setup a CBR over UDP connection
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set rate_ 1mb
$cbr set random_ false
#Schedule events for the CBR and FTP agents
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 4.0 "$ftp stop"
$ns at 4.5 "$cbr stop"
#Detach tcp and sink agents (not really necessary)
$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Print CBR packet size and interval
puts "CBR packet size = [$cbr set packet_size_]"
puts "CBR interval = [$cbr set interval_]"
#Run the simulation
$ns run

              At the end of this article, we have some satisfaction about the provision of required research guidance in the process of trace file in Ns2. Moreover, we include the step by step processes starting from type identification. Contact us to acquire the research knowledge in this research field.