Joining two (remote) process streams an intermediary

I'm sure the answer to this is out there somewhere but I've searched Google, scanned the java socket tutorials & faqs, and searched the forums here but haven't found it yet.

I have a java application that communicates with a c program using a custom IPC class (not written by me). This works perfectly fine. Now I want to make this work remotely and one idea I had was to modify the IPC initialization so that it sets up a socket and sends an init msg to the server. The server (a java class) starts the c-prog process remotely and (hopefully) directly connects the client and c-prog streams together. Is this even possible?

I have the client and the send msg working. And I can get the server to start the other process, get output from it, "manually" send the data back, and it arrives fine. It's just that when I connect the streams, things hang. Do I have to terminate the sends/receives somehow (I believe this is already being done) or flush the streams or .... ?

The data being sent consists of strings, chars, numbers (int/dbl/lng), boolean, and single & double dimensioned arrays of those.

[1135 byte] By [ShaneDavisa] at [2007-10-2 22:12:09]
# 1

If you have buffering streams of some sort you probably need to flush().

If you are using DataInputStream and DataOutputStream you must open them in the right order, see their javadoc. But this probably isn't it since you are talking with a C program.

Hit control-backslash (Unix) or control-break (Windows) in the command prompt window where you start the Java program to get a stack trace. Where are things hanging (show us the stack trace)? When is it hanging; while connecting a socket, when writing, while reading, ...?

Post the bit of code that is hanging. When posting code use [code]...[/code] tags or the "code" button on the forum posting page.

sjasjaa at 2007-7-14 1:29:03 > top of Java-index,Archived Forums,Socket Programming...
# 2
You need to flush() the output before every read.There's nothing about the creation order of DataInput/OutputStreams in their Javadoc. sjasja is thinking of ObjectInput/OutputStreams.
ejpa at 2007-7-14 1:29:03 > top of Java-index,Archived Forums,Socket Programming...
# 3

Thanks for the replies. I checked everything and things were getting flushed. What I was hoping to do was something like...

ServerSocket ss = new ServerSocket(1234);

Socket s = ss.accept();

is = s.getInputStream();

os = s.getOutputStream();

IPC.init();

IPC.procIn = is;

IPC.procOut = os;

But after thinking about it for a bit (while waiting for the forum maintenance to end :), I came up with this:

while (true) {

while (is.available() > 0) {

b = (byte)is.read();

IPC.procIn.write(b);

IPC.procIn.flush();

}

while (IPC.procOut.available() > 0) {

b = (byte)IPC.procOut.read();

os.write(b);

os.flush();

}

}

It works but it's a little slow (over DSL). Is there a better way?

ShaneDavisa at 2007-7-14 1:29:03 > top of Java-index,Archived Forums,Socket Programming...
# 4

Yes. Use two threads, one in each direction, and get rid of the available() calls.

Also, don't read and write one byte at a time. Use a large read buffer, and remember to write out only the count that you read.

When you get EOF when reading in one thread, pass it on the target by calling Socket.shutdownOutput() and exiting that thread. When you have done this in both directions, close the socket.

ejpa at 2007-7-14 1:29:03 > top of Java-index,Archived Forums,Socket Programming...
# 5
Cool, thanks - this is good info and what I was hoping to find with my previous searches but wasn't successful.
ShaneDavisa at 2007-7-14 1:29:03 > top of Java-index,Archived Forums,Socket Programming...