Can I redirect STDIN to a socket?

  • Thread starter Edward A Thompson
  • Start date
J

John C. Bollinger

Edward said:
so that a server in another JVM can read it?

You can use System.setIn(InputStream), but that makes the VM you invoke
it in _read_ its standard input from the socket. To export it to
another VM you must install some kind of object that copies the data
from the source VM through the socket to the receiving VM, with the
receiving VM doing the setIn(). This can be done in a dedicated thread
in order to simultaneously do other useful work in the source VM.


John Bollinger
(e-mail address removed)
 
E

Ed

John said:
You can use System.setIn(InputStream), but that makes the VM you invoke
it in _read_ its standard input from the socket. To export it to
another VM you must install some kind of object that copies the data
from the source VM through the socket to the receiving VM, with the
receiving VM doing the setIn(). This can be done in a dedicated thread
in order to simultaneously do other useful work in the source VM.


John Bollinger
(e-mail address removed)
The source VM just needs to reroute its STDIN (through socket). The
server VM processes the stream, then responds to the source VM )through
a socket), which pipes the output to STDOUT. Just not clear on how to
do the redirecting.
 
J

John C. Bollinger

Ed said:
The source VM just needs to reroute its STDIN (through socket). The
server VM processes the stream, then responds to the source VM )through
a socket), which pipes the output to STDOUT. Just not clear on how to
do the redirecting.

Very well. That's a bit different than what the term "redirect"
normally means to me, but it's certainly doable. You want something to
forward data from standard input into your socket, and something to
forward the input from your socket to your standard output. Two
instances of something like this might do the trick:

public class StreamForwarder implements Runnable {
private static final int BUFFER_SIZE = 2048;
private boolean shouldStop = false;
private IOException terminationCause = null;
private final Object lock = new Object();
private final InputStream in;
private final OutputStream out;

public StreamForwarder(InputStream from, OutputStream to) {
in = from;
out = to;
}

public void run() {
try {
byte[] buffer = new byte[BUFFER_SIZE];

copyData:
for (;;) {
synchronized(lock) {
if (shouldStop) {
break copyData;
}
}
int numRead = in.read(buffer);

if (numRead <= 0) {
break copyData;
}
out.write(buffer, 0, numRead);
}
} catch (IOException ioe) {
synchronized(lock) {
terminationCause = ioe;
}
}
}

public void stopForwarding() {
synchronized(lock) {
shouldStop = true;
}
}

public IOException getTerminationCause() {
synchronized(lock) {
return terminationCause;
}
}
}


Each one should run in its own thread; it might make sense to make those
daemon threads if you are going to do other useful work in the source
JVM for duration of the forwarding session. You probably do want to do
that, so as to be able close / flush output streams at the appropriate
point, etc..


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top