使用 stdin、stdout 转发到套接字执行任意进程的最简单方法是什么?

发布于 2024-08-08 06:28:55 字数 116 浏览 3 评论 0原文

我对两种情况感兴趣:

  • 如何从 C++ 中做到这一点?
  • 如何从系统外壳执行此操作?

欢迎针对 Linux、Windows 和 OSX 提供答案。

I'm interested in two situations:

  • How to do it from C++?
  • How to do it from system's shell?

Answers for Linux, Windows and OSX are welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

香橙ぽ 2024-08-15 06:28:55

Linux/OSX(实际上是 POSIX),编程(任何具有 POSIX 调用的语言),一般方案:

  1. 设置套接字...
  2. fork()
  3. close(0), < code>close(1) (不是必需的,dup2 也会关闭它...但为了清楚起见而添加)
  4. dup2(socket, 0), dup2(socket, 1)
  5. exec()

Shell:使用nc。我的其他答案中的示例: https://stackoverflow.com/questions/1269400/is-this-a-fair-question-to-ask-in-a-software-engineering-interview-phase-1/1269577#1269577< /a>

Linux/OSX (actually POSIX), programming (any language that have POSIX calls), general scheme:

  1. setup a socket...
  2. fork()
  3. close(0), close(1) (not necessary, dup2 will close it too... but added for clarity)
  4. dup2(socket, 0), dup2(socket, 1)
  5. exec()

Shell: use nc. Example in my other answer: https://stackoverflow.com/questions/1269400/is-this-a-fair-question-to-ask-in-a-software-engineering-interview-phase-1/1269577#1269577

ぺ禁宫浮华殁 2024-08-15 06:28:55

我还想知道 xinetd 在这种情况下是否没有帮助。它允许您编写一个程序,只需从 stdin 读取并写入 stdout,然后 xinetd 处理监听某个套接字,分离必要的 i/o 套接字,并将它们附加到您的进程。

I also wonder whether xinetd isn't helpful in this situation. It lets you write a program that simply reads from stdin and writes to stdout, and then xinetd handles listening on some socket, splitting off the necessary i/o sockets, and attaching them to your process.

孤凫 2024-08-15 06:28:55

对于 unix,从 shell:
http://www.askdavetaylor.com/how_do_i_reredirect_stdin_in_a_unix_or_linux_shell_script.html

有关更多一般信息:
http:// www.mathinfo.u-picardie.fr/asch/f/MeCS/courseware/users/help/general/unix/redirection.html

对于 Windows,到套接字:
http://www.unix.com/ high-level-programming/79439-redirect-stdin-out-sockets.html

这是关于 unix 的重定向说明:
http://www.rtems.com/ml/rtems- users/2007/october/msg00063.html

现在,这个只会重定向来自程序的任何进入 stdin/out/err 的内容。

我喜欢最后一个链接还在程序退出之前恢复 stdin/out/err 的事实。如果您进行任何此类更改,请记住进行恢复。

有多种方法可以做到这一点,例如,您可以使用管道,并将管道转到套接字或文件,这样 stdin/out 将被重定向到管道。如果您希望能够切换最终目的地的位置,或者如果您想沿途进行一些处理,那么这很好,因为每个管道都可以进行一些处理。

For unix, from a shell:
http://www.askdavetaylor.com/how_do_i_reredirect_stdin_in_a_unix_or_linux_shell_script.html

For more general info:
http://www.mathinfo.u-picardie.fr/asch/f/MeCS/courseware/users/help/general/unix/redirection.html

For windows, to a socket:
http://www.unix.com/high-level-programming/79439-redirect-stdin-out-sockets.html

Here is an explanation on unix, for redirecting:
http://www.rtems.com/ml/rtems-users/2007/october/msg00063.html

Now, this one will only be redirecting anything going to stdin/out/err that comes from the program.

I like the fact that the last link also restores stdin/out/err before the program exits. If you make any change like this, restoring is a good thing to remember to do.

There are several ways to do it, you can use pipes, and have the pipe go to a socket or file, for example, so stdin/out would be redirected to a pipe. This is nice if you want to be able to switch where the final destination is going, or if you want to do some processing along the way, as each pipe can do some processing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文