简体   繁体   English

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

[英]What is the simplest way to execute arbitrary process with stdin, stdout forwarded to a socket?

I'm interested in two situations: 我对以下两种情况感兴趣:

  • How to do it from C++? 如何用C ++做到这一点?
  • How to do it from system's shell? 如何从系统的外壳做?

Answers for Linux, Windows and OSX are welcome. 欢迎回答Linux,Windows和OSX。

Linux/OSX (actually POSIX), programming (any language that have POSIX calls), general scheme: Linux / OSX(实际上是POSIX),编程(具有POSIX调用的任何语言),一般方案:

  1. setup a socket... 设置一个插座...
  2. fork()
  3. close(0) , close(1) (not necessary, dup2 will close it too... but added for clarity) close(0)close(1) (不是必须的, dup2也会将其关闭...但是为了清楚起见添加了它)
  4. dup2(socket, 0) , dup2(socket, 1) dup2(socket, 0)dup2(socket, 1)
  5. exec()

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

I also wonder whether xinetd isn't helpful in this situation. 我也想知道xinetd在这种情况下是否没有帮助。 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. 它使您可以编写一个程序,该程序仅从stdin读取并写入stdout,然后xinetd处理监听某些套接字,分离出必要的I / O套接字并将它们附加到您的进程中。

For unix, from a shell: http://www.askdavetaylor.com/how_do_i_reredirect_stdin_in_a_unix_or_linux_shell_script.html 对于UNIX,可以从外壳程序访问: http : //www.askdavetaylor.com/how_do_i_reredirectdirect_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 有关更多常规信息: 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 对于Windows,连接至套接字: 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 这是有关unix重定向的说明: 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. 现在,此程序将仅重定向程序中的任何内容到stdin / out / err。

I like the fact that the last link also restores stdin/out/err before the program exits. 我喜欢最后一个链接在程序退出之前也可以还原stdin / out / err的事实。 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. 可以使用多种方法来进行操作,例如,可以使用管道并将管道转到套接字或文件中,以便将stdin / out重定向到管道。 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. 如果您希望能够切换最终目的地的位置,或者在此过程中要进行一些处理,因为每个管道都可以进行某些处理,则这很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM