ObjectOutputStream problem.

J

Jonn

Hi,

I'm building a server/client program and have problems using
ObjectOutputStreams (OOS). I'm trying to writeObject() a custom object into
an OOS, but for some reason the program hangs. If I write a String into it,
using writeObject(), all works fine. But my own object hangs using the very
same method.

First when I create an OOS from a Socket.getOutputStream(), writeObject()
some objects into it, flush() all goes well. Then I start a new Thread for
the input-loop, receive objects from it, and try to return those objects
through OOS (that was working just fine a moment earlier) but it won't work
anymore. The objects are serialized, I've called flush() after write, and
tried also OOS.reset() before writing into OOS, but nothing seems to help.
Also no Exceptions are generated.

Any ideas where I'm going wrong?


Jonn
 
G

Gordon Beaton

I'm building a server/client program and have problems using
ObjectOutputStreams (OOS). I'm trying to writeObject() a custom
object into an OOS, but for some reason the program hangs. If I
write a String into it, using writeObject(), all works fine. But my
own object hangs using the very same method.

First when I create an OOS from a Socket.getOutputStream(),
writeObject() some objects into it, flush() all goes well. Then I
start a new Thread for the input-loop, receive objects from it,

You start the reader *after* you've finished writing the object?
and try to return those objects through OOS (that was working just
fine a moment earlier) but it won't work anymore. The objects are
serialized, I've called flush() after write, and tried also
OOS.reset() before writing into OOS, but nothing seems to help. Also
no Exceptions are generated.

Any ideas where I'm going wrong?

It sounds like you are trying to do this from a single process. There
are two synchronization issues to be aware of:

- when you create an OOS/OIS pair, the OIS constructor waits for a
stream header sent by the OOS before returning.

- as you write objects to the OOS, you can't get too far ahead of
the reader. Otherwise some buffers will fill, causing the write
operation to block until the reader catches up.

If you write (too much) data to the OOS before there is a reader (or
even before the corresponding OIS has been created), the write
operation will block. But if you had intended to start the reader
after writing, your process will deadlock at this point. Note that
this can work for small amounts of data, which explains why you
managed to send the String.

To avoid deadlock, make sure the reader has been started *before* you
attempt to write anything to the OOS.

/gordon
 
P

perry

are all your classes serialized? as in do they all contain the
Serializable interface?

- perry
 

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
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top