[java] ObjectInputStreams

Started by
6 comments, last by ManiacMac 19 years, 9 months ago
A bit of information about what im doing first I suppose; im working a "one server/many clients" program. Ive done some before without any trouble. But when I try to do the following:

ObjectOutputStream oos = new ObjectOutputStream(
                                 new BufferedOutputStream(socket.getOutputStream()));
ObjectInputStream ois = new ObjectInputStream(
                                 new BufferedInputStream(socket.getInputStream()));
The program stops working when I try to create the ObjectInputStream. I switched the two over to a BufferedReader and a PrintWriter and it worked fine. Of course, those arent what I need for my program. Can anyone offer an idea as to what may be wrong? edit: fixed spelling mistake with socket edit: fixed other spelling mistake with Stream names [Edited by - ManiacMac on July 4, 2004 9:38:31 AM]
Advertisement
if "socket" is refering to a socket itself, that might give you some problems. try socket.getOutputStream() and socket.getInputStream() instead.


new ObjectOutputStream( new BufferedOutputStream( socket.getOutputStream() ) ); etc..

If that's not your problem, please elaborate on the specifics of your problem. Error messages, where it crashes, exactly what you changed, etc.
Development blog, The Omega Sector -- http://www.omegasector.org
Sorry about that, typing that from memory lead me to forget that rather important part.

I put two printlns in, one after the first Stream, and one after the second. Only the first one printed though, leading me to believe something is causing it to hang when I try to create the InputStream. It gives no errors at all though. I have catch() and prinlns all around to make sure things are going as I plan, but they arent catching any errors
Can you post your complete code? It's pretty hard to guess on what the problem is :)
Development blog, The Omega Sector -- http://www.omegasector.org
Its a couple classes, so I just stuck the code into a zip for you.
Here
it is for you to check, Thanks.
Are you flush()'ing the streams whenever you write an object? Otherwise, it could be sitting in a buffer at the sending end of the connection.

Edit: Just noticed that you posted your code. You are indeed not flushing the streams. Flush them, and things will start happening :).

The point of using BufferedOutputStream and BufferedInputStream is to send/recieve data in larger chunks, since this requires less overhead to get the same amount of data. If you're doing something like writing to a file, the stream will automatically be flushed whenever it is closed or garbage-collected. But if you want realtime communication, you can't rely on that. You might still want BufferedOutputStreams so that you don't send an object in multiple small packets, but you should definitely try to get each message out as soon as possible. There's probably no need for BufferedInputStreams.
Hey

I did something similar to this recently, I did:

ObjectOutputStream netOut = new ObjectOutputStream(
new DataOutputStream(
socket.getOutputStream()));

ObjectInputStream netIn = new ObjectOutputStream(
new DataInputStream(
socket.getInputStream()));

That worked for me. A question: Why are your output and input streams named the same, i.e oos?

LizardCPP
Calling flush() fixed up the stalls, thanks a lot Matei.

And uh, theyre named the same because I was typing that from memory instead of copy/pasting like an sane person would.

This topic is closed to new replies.

Advertisement