cannot receive over ObjectInputStream... what is going on?

consider this code:

client

DatagramSocket dgs =new DatagramSocket(9999);

byte[] data =newbyte[1024];

DatagramPacket dgp =new DatagramPacket(data, data.length);

dgs.receive(dgp);

Socket sok =new Socket(dgp.getAddress(), 8888);

ObjectOutputStream oos =new ObjectOutputStream(sok.getOutputStream());

oos.writeObject("message from client");

oos.flush();

server

// do a UDP broadcast on port 9999

......

ServerSocket servSok =new ServerSocket(8888);

Socket sok = servSok.accept();

ObjectInputStream ois =new ObjectInputStream(sok.getInputStream());

// .... blocks .... blocks ....

System.out.println("this is never displayed....");

String message = (String) ois.readObject();

what is going on? why does blocking happen

here:

ObjectInputStream ois = new ObjectInputStream(sok.getInputStream());

instead of

String message = (String) ois.readObject();

i've openedObjectInputStreams this way

hundreds of times.

what are some issues for me to investigate?

i don't know how to debug such a fundamental

process.

thanks.

[1664 byte] By [dew2hirooa] at [2007-11-27 5:26:32]
# 1
ObjectInputStream's constructor reads a header from the stream that is written by ObjectOutputStream's constructor. However the flush() in the client should take care of all that. You could try a flush after constructing the ObjectOutputStream.
ejpa at 2007-7-12 14:47:30 > top of Java-index,Core,Core APIs...
# 2
oos.flush()did seem to work.i never will understand that method.the only time i am 100% certain to call it is when i am writinga binary stream to a file.i guess it can never hurt to envoke it, so i'll start to do so moreliberally.
dew2hirooa at 2007-7-12 14:47:30 > top of Java-index,Core,Core APIs...
# 3

There are only two times you need to call it in association with ObjectOutputStreams over sockets:

- immediately after construction.

- whenever you want the target to see the last thing you wrote. Usually this means just before you read from the corresponding ObjectInputStream of the same socket.

Flushing at any other time is really a waste of time, and a performance drag as well.

ejpa at 2007-7-12 14:47:30 > top of Java-index,Core,Core APIs...
# 4
Are you sure it's blocking at the getOutputStream() and not the accept()?When you call accept() on a ServerSocket it blocks until an incoming connection is made.Thanks,b2s
born2snipea at 2007-7-12 14:47:30 > top of Java-index,Core,Core APIs...
# 5
> Are you sure it's blocking at the getOutputStream()> and not the accept()?no. i am not sure.things got so strange, i made the flush() change, did a complete reset of all pcs in the house, and took a shower.i came back and there was no more
dew2hirooa at 2007-7-12 14:47:30 > top of Java-index,Core,Core APIs...
# 6

> > Are you sure it's blocking at the

> getOutputStream()

> > and not the accept()?

>

> no. i am not sure.

> things got so strange, i made the flush()

> change, did a complete reset of all pcs in the

> house, and took a shower.

>

> i came back and there was no more problem.

Ah. The old dirty socket problem.

cotton.ma at 2007-7-12 14:47:30 > top of Java-index,Core,Core APIs...