aspose file tools*
The moose likes I/O and Streams and the fly likes ObjectInputStream timing out when created Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "ObjectInputStream timing out when created" Watch "ObjectInputStream timing out when created" New topic
Author

ObjectInputStream timing out when created

Darrin Cartwright
Ranch Hand

Joined: Dec 27, 2002
Posts: 107
Hello All!

Does anyone know why the following is happening? When creating an ObjectInputStream, the call seems to block forever. If I set a timeout on the socket first, I get a "java.net.SocketTimeoutException: Read timed out" exception thrown.

Here is the code:



If I comment out the setSoTimeout line, the code blocks. Leave it in and I get:
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2213)
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2226)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2694)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:761)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:277)
at orda.suis.HCIClient.setupSocket(HCIClient.java:115)
at orda.suis.HCIClient.run(HCIClient.java:156)

Why is the socket being read during creation?

Thanks!

DC


Life is good on the Ponderosa,<br />but mind where you step.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Object*putStreams read/write a header to the stream in order to establish that the stream contains an object. When you create the ObjectInputStream on your socket, it is attempting to read that header.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Darrin Cartwright
Ranch Hand

Joined: Dec 27, 2002
Posts: 107
Thanks Joe!

I switched my initializations around to create the ObjectOutputStream on the socket first, and all is well. However, this seems a little counterintuitive.

The code that worked looks like this:


The only difference from the first post being the OutputStream is created first. How is the input stream reading (the object header?) from the output when they are both on the same side of the socket? I take it this is not _normal_ socket I/O?

Thanks,

DC
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

The socket IO is normal, the data requires a little knowledge. You probably had a deadlock situation where your client and server both opened ObjectInputStreams and sat there waiting for the other end to send the header (assuming that you have the same kind of code on your client side).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ObjectInputStream timing out when created