如何修复TCL ns2中的错误

如何修复TCL ns2中的错误,tcl,ns2,Tcl,Ns2,我对NS2的使用非常幼稚,并试图在NS2和TCP中通过UDP实现CBR 当我试图编译它时,它会给我以下错误: ns: _o28 start: (_o28 cmd line 1) invoked from within "_o28 cmd start" invoked from within "catch "$self cmd $args" ret" invoked from within "if [catch "$self cmd $args" ret] {

我对NS2的使用非常幼稚,并试图在NS2和TCP中通过UDP实现CBR

当我试图编译它时,它会给我以下错误:

ns: _o28 start: 
    (_o28 cmd line 1)
    invoked from within
"_o28 cmd start"
    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 "_o28" line 2)
    (SplitObject unknown line 2)
    invoked from within
"_o28 start"
这是我的代码。我试图通过上网来修复它,但没有得到任何有用的东西

        # Création du simulateur
set ns [new Simulator]
# Création des fichiers de traces NS-2
set nf [open out.nam w]
$ns namtrace-all $nf
$ns trace-all [open out1.tr w]

# Création des noeuds
set N1 [$ns node]
$N1 label "source cbr"
set N2 [$ns node]
$N2 label "source tcp"
set R1 [$ns node]
set R2 [$ns node]
set N3 [$ns node]
$N3 label "destination tcp"
set N4 [$ns node]
$N4 label "destination cbr"


#Setup a TCP connection
set tcp [new Agent/TCP]
$tcp set class_ 2
$ns attach-agent $N2 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $N3 $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 $N4 $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


# Création des liens, tous en 1Mbps/10ms de TR/file d'attente DropTail
$ns duplex-link $N1 $R1 10Mb 15ms DropTail
$ns duplex-link $N2 $R1 10Mb 15ms DropTail
$ns duplex-link $R1 $R2 5Mb  40ms DropTail
$ns duplex-link $R2 $N3 12Mb 15ms DropTail
$ns duplex-link $R2 $N4 6Mb 15ms DropTail

    #capacité file d'attente R1-R2
    $ns queue-limit $R1 $R2 50



# gestion du layout de la topologie

$ns duplex-link-op $R2 $N4 orient right-down
$ns duplex-link-op $R2 $N3 orient right-up
$ns duplex-link-op $R1 $R2 orient right
$ns duplex-link-op $N1 $R1 orient left-up
$ns duplex-link-op $N2 $R1 orient left-down

#$ns duplex-link-op $R1 $R2 queuePos 0.5 # voir l’état de la file





# Création d'un agent vide, destiné à recevoir les paquets implantés dans n3
set null0 [new Agent/Null]
$ns attach-agent $N3 $null0

# Création d'un agent vide, destiné à recevoir les paquets implantés dans n3
set tcpsink [new Agent/TCPSink]
$ns attach-agent $N4 $tcpsink

# Le traffic issu des agents udp0 et udp1 est envoyé vers null0
$ns connect $udp $null0
$ns connect $tcp $tcpsink




# Scénario de début et de fin de génération des paquets par cbr0
$ns at 0.0 "$cbr start"
$ns at 0.2 "$tcp start"
$ns at 4 "$tcp stop"

$ns at 5.0 "$cbr stop"
# Définition de classes pour la coloration



# Coloration des classes : bleu pour udp (classe 1) et rouge pour tcp (classe 2)
$ns color 1 Blue
$ns color 2 Red


# Procédure de fin de simulation, qui écrit les données dans le fichier
# et lance NAM pour la visualisation
proc fin {} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
} 
# La simulation va durer 15 secondes et appelle la procédure finish
$ns at 6.0 "fin"
# Début de la simulation
$ns run

NS2构建在一个用Tcl编写的名为OTcl的对象系统之上。现在这是一个相当古老的系统;Tcl中的其他人(使用对象系统的人)都使用其他东西。其中一个原因是调试OTcl程序比调试大多数Tcl程序更麻烦

但这并不能解决这里的问题!如果我们查看堆栈跟踪,我们可以从启动堆栈的调用中选择返回的路径:

"_o28 start"
到出现错误的地方:

    (_o28 cmd line 1)
唉,中间的一切都只是OTcl机器。在我看来,这是令人讨厌的,因为我注意到其中有许多不好的做法,但这不应该与您有关:它不在您的代码中,而是您正在使用的库中

不幸的是,无论是什么原因造成的错误都没有给我们一条错误消息!那太可怕了。但是,我们可以从方法名称猜测调用它的是这些行之一(延迟到模拟器引擎到达该时间戳):

是哪一个?
Application/Traffic/CBR
类或
Agent/TCP
类的
start
方法?我真的不知道!堆栈跟踪巧妙地忽略了这一点。方法定义没有显示在代码示例中,实际上可能是C或C++。我们根本没有这些信息。我们确实知道它看起来像是来自start方法主体的第一行,但仅此而已

由于您使用的是全局变量,您可以(可能)更改这两个回调设置行以执行此操作:

$ns at 0.0 {$cbr start}
$ns at 0.2 {$tcp start}
这会将变量的替换推迟到调用时,并且可能会使堆栈跟踪提供足够的信息,以确定哪个类的
start
方法存在问题。它不会解决问题,但可能会让你更容易找到它


一般来说,您的代码似乎是合理的。它使用的是NS2,所以它也使用OTcl(唉),但除此之外,这不是一个问题。现代Tcl编程更喜欢使用
list
命令来构造回调,而不是字符串替换,因此您可以这样做:

$ns at 0.0 [list $cbr start]
$ns at 0.2 [list $tcp start]
与此相反:

$ns at 0.0 "$cbr start"
$ns at 0.2 "$tcp start"
但对象名很简单,而且是一次性回调,因此不会对安全性或性能产生太大影响

我无法告诉您是否正确使用了NS2。我使用完全不同的库编写非常不同的Tcl程序。


如果您注释掉这些“tcp”行,则表明您的文件工作正常:

# $ns at 0.2 "$tcp start"
# $ns at 4 "$tcp stop"
在所有~2000 ns2模拟文件上运行搜索时,会发现从未使用过
“$tcp start”
$cd*all examples/&&grep“tcp start”*
,这可能意味着
“$tcp start”
不是一种有效的编码方式

~2000 ns2仿真文件:all_tcl-examples-2.tar.gz(41.8MB)

查找tcp文件:
$cd*所有示例/&&ls| grep-i tcp



我真的很想你,我不知道问题出在哪里..但是当你说问题出在那一行时,我就明白了。。。。。。。。。。事实上,我的Tcp协议是通过FTP。。。因此,我将start tcp和stop tcp的两行代码改为$ftp,如下所示:$ns在0.0“$ftp start”和$ns在4“$ftp stop”
# $ns at 0.2 "$tcp start"
# $ns at 4 "$tcp stop"