Tcl开放通道
Tcl open channel
如何在 tcl 中打开一个不是文件名的通道?我已经阅读了文档,但我不是程序员,所以我一定不理解 open 和 chan 命令,因为当我尝试打开一个新的自定义频道时
open customchannel1 RDWR
我收到诸如
之类的错误
couldn't execute "customchannel1": no such file or directory
而且我完全知道我没有正确执行此操作:
chan create read customchannel1
invalid command name "customchannel1" ...and... invalid command name "initialize"
我想要的只是两个能够相互对话的 tcl 脚本。我以为我可以使用渠道来做到这一点。
但是,我已经成功创建了我想要的套接字测试版本:
proc accept {chan addr port} {
puts "$addr:$port says [gets $chan]"
puts $chan goodbye
close $chan
}
puts -nonewline "master or slave? "
flush stdout
set name [gets stdin]
if {$name eq "master"} {
puts -nonewline "Whats the port? "
flush stdout
set port [gets stdin]
socket -server accept $port
vwait forever
} else {
puts "slave then."
puts -nonewline "Whats the id? "
flush stdout
set myid [gets stdin]
set chan [socket 127.0.0.1 $myid]
puts $chan hello
flush $chan
puts "127.0.0.1:$myid says [gets $chan]"
close $chan
}
在上面的示例中,我可以 运行 程序的 3 个实例:2 个 'masters' 具有不同的端口号,以及一个 'slave' 可以根据端口与任何一个通信/'id' 它选择。
如果我知道如何使用 open 命令而不是 socket 命令打开通道,我可以在不使用套接字的情况下实现上述代码,或者 jimmy-rigging 端口以用作 uniq id,但每个示例我都可以find 打开文件并写出到文件或标准输出,您不必首先创建这些文件。
感谢您帮助我理解这些概念以及如何更好地实施它们!
通道只是一种高级方法,用于处理已打开的文件或套接字。
来自手册页:
This command provides several operations for reading from, writing to and otherwise manipulating open channels (such as have been created with the open and socket commands, or the default named channels stdin, stdout or stderr which correspond to the process's standard input, output and error streams respectively).
所以你对套接字所做的是正确的。可以使用chan
命令配置打开的socket。
将两个脚本连接在一起时,您可能会考虑使用管道。例如,您可以 运行 一个脚本作为另一个脚本的从属进程。大师这样做:
set pipe [open |[list [info nameofexecutable] $thescriptfile] "r+"]
获得双向(因为 r+
)管道与 child 对话,这又可以正常使用标准输出和标准输入。
在一个进程中,chan pipe
可用,returns 一对通过 OS 匿名管道连接的通道。
使用这些时,如果您记得使用 fconfigure
将 -buffering
转换为 none
,那将非常有帮助。否则,当管道的输出位于某处的缓冲区中时,您可能会遇到死锁,这是您不想要的。最终的答案是使用 Expect,它使用 Unix ptys 而不是管道,但如果您记得调整缓冲,您的工作效率会很高。
如何在 tcl 中打开一个不是文件名的通道?我已经阅读了文档,但我不是程序员,所以我一定不理解 open 和 chan 命令,因为当我尝试打开一个新的自定义频道时
open customchannel1 RDWR
我收到诸如
之类的错误couldn't execute "customchannel1": no such file or directory
而且我完全知道我没有正确执行此操作:
chan create read customchannel1
invalid command name "customchannel1" ...and... invalid command name "initialize"
我想要的只是两个能够相互对话的 tcl 脚本。我以为我可以使用渠道来做到这一点。
但是,我已经成功创建了我想要的套接字测试版本:
proc accept {chan addr port} {
puts "$addr:$port says [gets $chan]"
puts $chan goodbye
close $chan
}
puts -nonewline "master or slave? "
flush stdout
set name [gets stdin]
if {$name eq "master"} {
puts -nonewline "Whats the port? "
flush stdout
set port [gets stdin]
socket -server accept $port
vwait forever
} else {
puts "slave then."
puts -nonewline "Whats the id? "
flush stdout
set myid [gets stdin]
set chan [socket 127.0.0.1 $myid]
puts $chan hello
flush $chan
puts "127.0.0.1:$myid says [gets $chan]"
close $chan
}
在上面的示例中,我可以 运行 程序的 3 个实例:2 个 'masters' 具有不同的端口号,以及一个 'slave' 可以根据端口与任何一个通信/'id' 它选择。
如果我知道如何使用 open 命令而不是 socket 命令打开通道,我可以在不使用套接字的情况下实现上述代码,或者 jimmy-rigging 端口以用作 uniq id,但每个示例我都可以find 打开文件并写出到文件或标准输出,您不必首先创建这些文件。
感谢您帮助我理解这些概念以及如何更好地实施它们!
通道只是一种高级方法,用于处理已打开的文件或套接字。
来自手册页:
This command provides several operations for reading from, writing to and otherwise manipulating open channels (such as have been created with the open and socket commands, or the default named channels stdin, stdout or stderr which correspond to the process's standard input, output and error streams respectively).
所以你对套接字所做的是正确的。可以使用chan
命令配置打开的socket。
将两个脚本连接在一起时,您可能会考虑使用管道。例如,您可以 运行 一个脚本作为另一个脚本的从属进程。大师这样做:
set pipe [open |[list [info nameofexecutable] $thescriptfile] "r+"]
获得双向(因为 r+
)管道与 child 对话,这又可以正常使用标准输出和标准输入。
在一个进程中,chan pipe
可用,returns 一对通过 OS 匿名管道连接的通道。
使用这些时,如果您记得使用 fconfigure
将 -buffering
转换为 none
,那将非常有帮助。否则,当管道的输出位于某处的缓冲区中时,您可能会遇到死锁,这是您不想要的。最终的答案是使用 Expect,它使用 Unix ptys 而不是管道,但如果您记得调整缓冲,您的工作效率会很高。