3

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. 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.

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.

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.

If it matters, I'm using BufferedInputStreams and BufferedOutputStreams.

Any information regarding this problem is greatly appreciated, thanks!

Tim Harkin
  • 31
  • 2
  • have a look at my answer on http://stackoverflow.com/a/2514456/267482, the simplest way to wire output to input is to have a thread per junction reading input and immediately writing to the respective output, the smarter way is to have an NIO selector and stay single threaded – bobah Mar 02 '13 at 22:53

1 Answers1

-1

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

Similarly, explore the System.setErr() and System.setIn() for error and input streams respectively!

Hamza Zafar
  • 1,320
  • 12
  • 17