This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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?
Life is good on the Ponderosa,<br />but mind where you step.
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.
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?
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).