I apologize in advance for the length of the note, and the source code
included.
I posted earlier under the title "Double streams", where i wanted to
use an RMI object as a sort of proxy for two clients to exchange data
via streams. I kinda failed, but now i'm trying something new. This
doesn't work either, but this time i think someone might be able to
solve it, since i'm not adept at using threads and i think that's
where the problem is. :)
The source for ByteHolder and RemoteByteHolder is included below. A
server is run, making a ByteHolder, BH, available via RMI.
Client A obtains the reference to BH, and passes this on to its
FooOutputStream, which extends OutputStream and implements the
write(int b) method by calling putByte(b) on BH.
Client B also gets BH, passes this to FooInputStream, which extends
InputStream and implements read() by calling getByte() on BH.
What i did to test was:
- run the server
- client A wraps the FooOutputStream in a PrintWriter, and
println("foo") is called on it
- client B wraps FooInputStream in a InputStreamReader, which is
then wrapped in a BufferedReader, and readLine() is called on that
The data then goes, one byte at the time, from client A to client B.
Client A stops running, but Client B just sorta hangs around... in a
deadlock i presume. I can make it return and print out the read line
by doing one of two things:
- kill the server
- manually send a -1 to client B
When i've used streams before, i didn't have to send a -1 in order to
get the reading side to get on with it - it just happened magically. I
also don't understand why killing the server make things better...
Can anyone explain this or, as an alternative, suggest a better way to
exchange bytes via RMI. I just really wanted to make streams work. :)
Oh, another thing. To make the server available, i've made it Runnable
and in the run i have a while(true) loop, that makes the thread sleep
for a chunk of time. Maybe that is a problem, too?
Regards,
Carsten H. Pedersen
----------- SOURCE BELOW -------------
package streams.holder;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface RemoteByteHolder extends Remote {
int getByte() throws RemoteException;
void putByte(int b) throws RemoteException;
}
...
package streams.holder;
import java.rmi.RemoteException;
public class ByteHolder implements RemoteByteHolder {
private int theByte;
private boolean stored = false;
public synchronized int getByte() throws RemoteException {
while(!stored) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("hb: get: "+theByte);
int i = theByte;
stored = false;
notifyAll();
return i;
}
public synchronized void putByte(int b) throws RemoteException {
while(stored) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("bh: put: "+b);
stored = true;
theByte = b;
notifyAll();
}
}
Oliver Wong - 20 Feb 2006 16:06 GMT
> I posted earlier under the title "Double streams", where i wanted to use
> an RMI object as a sort of proxy for two clients to exchange data via
> streams. I kinda failed, but now i'm trying something new. This doesn't
> work either, but this time i think someone might be able to solve it,
> since i'm not adept at using threads and i think that's where the problem
> is. :)
If you don't get any answers for 1 or 2 days, try posting this to
comp.lang.java.programmer. There's a lot of people there who are familiar
with threading who don't read this newsgroup.
If you post there too early though, you get accused of multiposting.
- Oliver
Godspeed - 01 Mar 2006 12:38 GMT
Hi Carsten,
What does your readLine code look like?
Are you doing something like:
while ((line = readLine())!=null) ...
>I apologize in advance for the length of the note, and the source code
>included.
[quoted text clipped - 100 lines]
>
> }