You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

UNIX 14.04环境下首次编写NS2三点对点双工链路网络程序运行报错:无法读取变量ns_o3的技术求助

Fixing Your NS2 Simulation Script & Packet Loss Statistics

First, let’s break down the errors in your prgrm1.tcl script — most are Tcl syntax mistakes (whitespace rules are strict in Tcl!) and typos that caused the "unable to read variable" errors. Here’s how to fix them, plus adjustments to meet your original goal of a bottlenecked duplex network (with 4 nodes forming a hub-and-spoke structure through n2) for queue/bandwidth tuning and packet loss stats:

Key Errors in Your Original Script

  • Missing whitespace in variable assignments: Tcl requires spaces between command names and arguments. For example, set ns[new Simulator] is invalid — it tries to run a non-existent command named ns[new. Correct to set ns [new Simulator].
  • Typo in node creation: set n2[$ns mode] should be set n2 [$ns node] (you wrote "mode" instead of "node").
  • Undefined variables: udp1 was used before being declared, and upd2 is a typo for udp2 (this agent is also unnecessary for your simulation goal).
  • Misattached applications: Your cbr0 was trying to attach to udp1 (which didn’t exist) instead of its paired udp0.
  • Spelling mistake in finish proc: gobal → global (critical for accessing variables outside the procedure’s scope).

Corrected prgrm1.tcl Script

# Initialize simulator
set ns [new Simulator]

# Open trace files for NAM and raw stats
set nf [open prg1.nam w]
$ns namtrace-all $nf
set tf [open lab1.tr w]
$ns trace-all $tf

# Cleanup procedure to end simulation
proc finish {} {
    global ns nf tf
    $ns flush-trace
    close $nf
    close $tf
    exec nam prg1.nam &
    exit 0
}

# Create network nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

# Set up duplex links (bottleneck at n2-n3 to force packet loss)
$ns duplex-link $n0 $n2 200Mb 10ms DropTail
$ns duplex-link $n1 $n2 100Mb 5ms DropTail
$ns duplex-link $n2 $n3 1Mb 1000ms DropTail

# Define queue limits for incoming links to hub node n2
$ns queue-limit $n0 $n2 10
$ns queue-limit $n1 $n2 10

# Create UDP agents for traffic sources (n0, n1)
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0

set udp1 [new Agent/UDP]
$ns attach-agent $n1 $udp1

# Create Null sink agent at destination n3
set null0 [new Agent/Null]
$ns attach-agent $n3 $null0

# Connect UDP sources to sink
$ns connect $udp0 $null0
$ns connect $udp1 $null0

# Configure CBR traffic generators
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0

set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1

# Schedule simulation events
$ns at 0.1 "$cbr0 start"
$ns at 0.2 "$cbr1 start"
$ns at 1.0 "finish"

# Start the simulation
$ns run

Adjusted lab1.awk Script (More Robust Parsing)

NS2 trace files follow a fixed format — your original script referenced $11, but drop reasons and source info are in earlier fields. This version logs drop times, source nodes, and total drops clearly:

BEGIN {
    print "Drop Time\tSource Node"
    drop_count = 0
}

# Match drop records (first field is "d" for dropped)
$1 == "d" {
    drop_count++
    printf("%.2f\t\t%s\n", $2, $3)
}

END {
    printf("\nTotal packets dropped: %d\n", drop_count)
}

How to Run

  1. Save the corrected prgrm1.tcl and lab1.awk files in the same directory.
  2. Launch the simulation:
    ns prgrm1.tcl
    
  3. After the NAM window closes, run the awk script to generate loss stats:
    awk -f lab1.awk lab1.tr
    

Why This Works

  • The 1Mb bottleneck link between n2 and n3 will overflow the queues at n2 when high-volume CBR traffic from n0 and n1 arrives, triggering DropTail queue drops as intended.
  • Fixed Tcl syntax ensures the simulator initializes properly without variable resolution errors.
  • The awk script parses the trace file correctly to count and log dropped packets for analysis.

内容的提问来源于stack exchange,提问作者Karthik KK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 17:52:42