views:

68

answers:

2

I'm trying to read items from a socket and I notice that if there is nothing on the stream of the socket it will stay at the read and back up my application. I wanted to know if there was a way to set a read timeout or terminate the connection after a certain amount of time of nothing in the socket.

A: 

Yes, there should be an override of Read() that accepts a timeout value.

Kelly French
And you don't read directly from a socket anyway, you get an `InputStream` from the socket and then read from that. Do what @erikson says, use `setSoTimeout()`.
krock
@Kelly - no there shouldn't. That would make the `SocketInputStream` API different from other streams. There is already a way to do this that doesn't break the `InputStream` substitutability.
Stephen C
+2  A: 

If you write Java, learning to navigate the API documentation is helpful. In the case of a socket read, you can set the timeout option.

erickson