NS2运行TCL脚本报sendmsg() for UDP should not be -1错误
问题描述
运行环境为Ubuntu 20.04,使用NS2与NAM组件编写TCL网络仿真脚本,仿真需求如下:
- 创建8个网络节点,通过带宽可自定义的
duplex-link完成节点链路互联 - 在节点0、1部署TCP Agent,节点2部署UDP Agent
- 为TCP Agent绑定CBR流量源,为UDP Agent绑定FTP流量源
执行ns file.tcl命令运行脚本时,触发Error: sendmsg() for UDP should not be -1报错,以下为完整脚本代码与报错调用栈信息,需定位问题诱因并提供修复方案。
问题复现TCL脚本
set val(stop) 10.0; set ns [new Simulator] #Open the NS trace file set tracefile [open out.tr w] $ns trace-all $tracefile #Open the NAM trace file set namfile [open out.nam w] $ns namtrace-all $namfile #=================================== # Nodes Definition #=================================== #Create 8 nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] set n4 [$ns node] set n5 [$ns node] set n6 [$ns node] set n7 [$ns node] $ns duplex-link $n0 $n3 20.0Mb 10ms DropTail $ns duplex-link $n1 $n3 20.0Mb 10ms DropTail $ns duplex-link $n3 $n2 20.0Mb 10ms DropTail $ns duplex-link $n3 $n4 20.0Mb 10ms DropTail $ns duplex-link $n4 $n6 20.0Mb 10ms DropTail $ns duplex-link $n4 $n5 20.0Mb 10ms DropTail $ns duplex-link $n4 $n7 20.0Mb 10ms DropTail $ns duplex-link-op $n0 $n3 orient right-down $ns duplex-link-op $n1 $n3 orient right $ns duplex-link-op $n3 $n2 orient left-down $ns duplex-link-op $n3 $n4 orient right $ns duplex-link-op $n4 $n6 orient right $ns duplex-link-op $n4 $n5 orient right-up $ns duplex-link-op $n4 $n7 orient right-down #=================================== # Agents Definition #=================================== set tcp [new Agent/TCP] $ns attach-agent $n0 $tcp $ns attach-agent $n1 $tcp set udp [new Agent/UDP] $ns attach-agent $n2 $udp set cbr [new Application/Traffic/CBR] $cbr attach-agent $tcp $cbr set packet_size_ 1500 set ftp [new Application/FTP] $ftp attach-agent $udp $ftp set type_ FTP # null & sink set null [new Agent/Null] $ns attach-agent $n6 $null $ns connect $udp $null set sink [new Agent/TCPSink] $ns attach-agent $n6 $sink $ns connect $tcp $sink proc finish {} { global ns tracefile namfile $ns flush-trace close $tracefile close $namfile exec nam out.nam & exit 0 } $ns at 0.5 "$ftp start" $ns at 5.0 "$ftp stop" $ns at 6.0 "finish" $ns run
报错调用栈信息
Error: sendmsg() for UDP should not be -1 ns: _o180 stop: (_o177 cmd line 1) invoked from within "_o177 cmd advance 0" invoked from within "catch "$self cmd $args" ret" invoked from within "if [catch "$self cmd $args" ret] { set cls [$self info class] global errorInfo set savedInfo $errorInfo error "error when calling class $cls: $args" $..." (procedure "_o177" line 2) (SplitObject unknown line 2) invoked from within "[$self agent] advance 0" (procedure "_o180" line 2) (Application/FTP stop line 2) invoked from within "_o180 stop"
问题诱因
报错由3处违反NS2运行规则的编写错误共同导致:
- Agent实例重复挂载:单个TCP Agent实例被先后绑定到n0、n1两个节点,NS2中单个Agent实例仅能归属一个节点,第二次
attach-agent会覆盖前序绑定关系,造成Agent状态异常。 - 流量源与Agent类型不匹配:FTP是基于TCP连接的应用层流量,无法在无连接的UDP Agent上运行;CBR为恒定速率无连接流量,通常搭配UDP Agent使用,原脚本反向绑定直接触发协议栈逻辑错误。
- 流量调度逻辑缺失:原脚本仅启动了错误绑定到UDP的FTP流量,绑定到TCP的CBR流量未配置启停调度,同时双TCP节点的接收端配置不符合要求。
修复方案
按照NS2语法规则修正对应逻辑即可:
- 为n0、n1分别创建独立的TCP Agent实例,各自绑定对应的TCPSink接收端
- 调整流量源绑定关系:CBR绑定到UDP Agent,FTP绑定到TCP Agent
- 补全所有流量的启停调度命令,保证Agent与节点一一对应
修正后的核心代码段参考:
# 为n0、n1分别创建独立TCP实例 set tcp0 [new Agent/TCP] $ns attach-agent $n0 $tcp0 set tcp1 [new Agent/TCP] $ns attach-agent $n1 $tcp1 set udp [new Agent/UDP] $ns attach-agent $n2 $udp # 修正流量源与Agent的绑定匹配关系 set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp $cbr set packet_size_ 1500 set ftp0 [new Application/FTP] $ftp0 attach-agent $tcp0 set ftp1 [new Application/FTP] $ftp1 attach-agent $tcp1 # 配置各协议对应的接收端 set null [new Agent/Null] $ns attach-agent $n6 $null $ns connect $udp $null set sink0 [new Agent/TCPSink] $ns attach-agent $n5 $sink0 $ns connect $tcp0 $sink0 set sink1 [new Agent/TCPSink] $ns attach-agent $n7 $sink1 $ns connect $tcp1 $sink1 # 补全所有流量的启停调度 $ns at 0.5 "$ftp0 start" $ns at 0.5 "$ftp1 start" $ns at 0.5 "$cbr start" $ns at 5.0 "$ftp0 stop" $ns at 5.0 "$ftp1 stop" $ns at 5.0 "$cbr stop"
内容的提问来源于stack exchange,提问作者VenoM
相关产品推荐
相关产品推荐

