NS2 Keywords
1. attach-agent:
• Attaches an agent (e.g., TCP, UDP, CBR traffic source) to a node.
• Example: $ns attach-agent $node0 $tcpAgent
2. trace-all:
• Enables tracing of all events in the simulation to a trace file.
• Example: $ns trace-all $traceFile
3. namtrace-all:
• Enables NAM animation tracing for all events, capturing node movement and
packet flow.
• Example: $ns namtrace-all $namFile
4. flush-trace:
• Flushes remaining trace data to the trace file, ensuring complete data collection.
• Example: $ns flush-trace
5. namtrace-all-wireless:
• Specific for wireless simulations, enables NAM animation tracing for wireless
events.
• Example: $ns namtrace-all-wireless $namFile $width $height
6. node-config:
• Configures common settings for multiple nodes, such as routing protocol, MAC
type, queue type, etc.
• Example: $ns node-config -adhocRouting DSDV
7. setdest:
• Schedules a node's movement to a new destination at a specified time.
• Example: $ns at 10 "$node1 setdest 500 500 10"
8. DropTail:
• A simple queue type that drops packets when the queue is full.
• Used in links and nodes to manage congestion.
9. duplex-link:
• Creates a bidirectional link between two nodes, with specified bandwidth and
delay.
• Example: $ns duplex-link $node0 $node1 1Mb 20ms DropTail
10. queue-limit:
• Sets the maximum queue length for a link or node, limiting the amount of
buffered data.
• Example: $ns queue-limit $link 50
11. label:
• Assigns a text label to a node for identification in NAM animation and trace files.
• Example: $node0 label "source"
12. Application/Traffic/CBR:
• Creates a Constant Bit Rate (CBR) traffic source, generating packets at a fixed
rate.
• Example: $cbr0 set packetSize_ 500
13. connect:
• Connects two agents (e.g., TCP sender and receiver) to establish a
communication path.
• Example: $ns connect $tcpSource $tcpSink
14. packetSize_:
• Sets the packet size in bytes for a traffic source or application.
• Example: $cbr0 set packetSize_ 1000
15. interval_:
• Sets the inter-packet interval in seconds for a CBR traffic source.
• Example: $cbr0 set interval_ 0.01
Commands And Components Commonly Used In NS2
I. Commands:
1. Node Creation:
• set n0 [$ns node]: Creates a node named "n0".
• create-god $numNodes: Creates multiple nodes at once.
2. Link Creation:
• duplex-link $n0 $n1 1Mb 20ms DropTail: Creates a bidirectional link between
nodes with specified bandwidth, delay, and queue type.
• simplex-link: Creates a unidirectional link.
3. Agent Creation:
• set tcp0 [new Agent/TCP]: Creates a TCP agent.
• set udp0 [new Agent/UDP]: Creates a UDP agent.
• set cbr0 [new Application/Traffic/CBR]: Creates a CBR traffic source.
4. Traffic Configuration:
• attach-agent $node $agent: Attaches an agent to a node.
• connect $tcpSrc $tcpSink: Establishes a connection between agents.
• set packetSize_: Sets packet size for a traffic source.
• set interval_: Sets inter-packet interval for CBR traffic.
5. Event Scheduling:
• $ns at 5 "$cbr0 start": Schedules an event to start CBR traffic at 5 seconds.
• $ns at 10 "$node1 setdest 500 500 20": Schedules node movement.
6. Tracing and Animation:
• trace-all $traceFile: Logs events for analysis.
• namtrace-all $namFile: Captures animation for visualization.
7. Simulation Control:
• $ns run: Starts the simulation.
• $ns flush-trace: Ensures complete data collection.
II. Components:
1. Nodes:
• Represent devices in the network.
• Have properties like position, address, and agents.
2. Links:
• Represent connections between nodes.
• Have properties like bandwidth, delay, and queue type.
3. Agents:
• Generate and receive traffic.
• Examples: TCP agents, UDP agents, CBR sources, FTP applications.
4. Sinks:
• Act as packet receivers, often used for analysis.
• Examples: TCPSink, NullAgent, application-specific sinks.
5. Queues:
• Manage packet buffering in nodes and links.
• Examples: DropTail, RED, CBQ.
6. Routing Protocols:
• Determine packet forwarding paths.
• Examples: DSDV, AODV, DSR.
7. MAC Protocols:
• Control access to shared wireless media.
• Examples: IEEE 802.11, CSMA/CA.
8. Traffic Generators:
• Create traffic patterns for simulations.
• Examples: CBR, FTP, Telnet.