tcl/tk - 我们可以将 tcl stdout 重定向到套接字吗?
我正在尝试将我的 stdout
和 stderr
输出重定向到文本小部件,我尝试 Memchan
来执行此操作,它确实 不起作用。
我们考虑的下一个选项是使用套接字。我们可以将 tcl
stdout
重定向到套接字吗?如果是,您能提供示例代码来证明这一点吗?
I am trying to redirect my stdout
and stderr
output to a text widget and I tried Memchan
to do it and it did not work.
The next option we are looking at is using sockets. Can we redirect tcl
stdout
to a socket? If yes, can you provide an example code to demonstrate that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以在子进程中运行吗?如果可以的话,这很容易:
这会启动一个 Tcl 子进程,为每个传入的套接字连接执行
realScript.tcl
,并安排 stdin (<@
) stdout (> ;@
) 和 stderr (2>@
) 被重定向到套接字。它还在后台运行子进程(最终&
),以便它不会阻止传入连接。 (在运行子进程之前,您可能需要检查$host
和$port
的可接受性。)更好的是,在子进程中,Tcl 仍然会自动检测它正在处理插座;
fconfigure
命令将能够查看套接字配置(当然,即使它无法更改其通信的端口)。Can you run in a subprocess? It's easy if you can:
This starts a Tcl subprocess executing
realScript.tcl
for each incoming socket connection, and arranges for stdin (<@
) stdout (>@
) and stderr (2>@
) to be redirected to the socket. It also runs the subprocess in the background (final&
) so that it doesn't block incoming connections. (You might want to check$host
and$port
for acceptability before running the subprocess.)What's even better, in the subprocess Tcl will still auto-detect that it's dealing with sockets; the
fconfigure
command will be able to see the socket configuration (even if it can't change what port its talking to, of course).“重定向我的标准输出”到底是什么意思?您的意思是,当您执行
puts foo
时,您希望将其发送到套接字吗?如果是这样,只需将默认的 puts 命令替换为您自己的写入套接字的命令即可。您可以通过将puts
重命名为其他名称,然后创建自己的名为puts
的过程来实现此目的。然后,您的 proc 将使用重命名的命令来执行实际的 I/O,但您可以插入套接字作为参数。What exactly do you mean by "redirect my stdout"? Do you mean, when you do a
puts foo
you want that to go to a socket? If so, simply replace the defaultputs
command with your own which writes to the socket. You do that by renamingputs
to something else, then creating your own proc namedputs
. Then, your proc will use the renamed command to do the actual I/O, but you can interject the socket as an argument.