简体   繁体   English

如何通过TCP套接字连接重定向进程的输入和输出? (java)

[英]How to redirect input and output of a Process over a TCP socket connection? (java)

I'd like to know how I can redirect the input and output of a system process over a TCP connection from the server-side. 我想知道如何从服务器端通过TCP连接重定向系统进程的输入和输出。 This would result in the client being able to interact with said process. 这将导致客户端能够与所述过程进行交互。

So far I know how to actually establish a simple TCP connection, and I can get the input and output streams of the connection socket using the getInputStream() and getOutputStream() methods. 到目前为止,我知道如何实际建立一个简单的TCP连接,并且可以使用getInputStream()getOutputStream()方法获取连接套接字的输入和输出流。

I can also get the output and input streams of a Process (after initializing the process via ProcessBuilder.start() method) using its getInputStream() and getOutputStream() methods. 我还可以使用其getInputStream()getOutputStream()方法获取Process的输出和输入流(通过ProcessBuilder.start()方法初始化该过程之后getOutputStream()

So I end up with two sets of input and output streams. 因此,我最终得到了两组输入和输出流。 What I want to know is how to connect (may be using the wrong word here) the output and input streams of the Process with the output stream of the connection socket, so that the client on the other side of the TCP connection can interact with the Process. 我想知道的是如何将Process的输出流和输入流与连接套接字的输出流连接(此处可能使用错误的词),以便TCP连接另一侧的客户端可以与过程。

If it matters, I'm using BufferedInputStream s and BufferedOutputStream s. 如果重要的话,我正在使用BufferedInputStreamBufferedOutputStream

Any information regarding this problem is greatly appreciated, thanks! 非常感谢您提供有关此问题的任何信息,谢谢!

To redirect the output streams of a process to socket, try the following code 要将进程的输出流重定向到套接字,请尝试以下代码

System.setOut( new PrintStream(mySocket.getOutputStream(),true))

Visit http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#setOut%28java.io.PrintStream%29 访问http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#setOut%28java.io.PrintStream%29

Similarly, explore the System.setErr() and System.setIn() for error and input streams respectively! 同样,分别探索System.setErr()和System.setIn()以获得错误流和输入流!

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

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