• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ObjectInputStream timing out when created

 
Darrin Cartwright
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Joe Ess
Bartender
Pie
Posts: 9266
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Darrin Cartwright
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9266
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic