On Wed, 03 Oct 2007 12:08:38 -0000, JTeagle wrote:
> I guess that means the API is still badly named - if there were
> functions that actually did what they claim in the name, we wouldn't
> be here again.
The methods are poorly named, but they do work as documented.
> Not sure how you found that previous message - even I hadn't
> remembered that - it's been that long since I did any Java. Pity the
> API hasn't improved much since then.
I recognized the question, and the asker...
> The problem with doing a read is that if the socket hasn't closed, you
> end up blocking - it defeats the purpose of a 'test' to see if the
> socket is closed. The problem with a write is that you end up writing
> spurious data out to the socket. If the implementation can tell (when
> you read or write) that the network has been lost, why can't it do so
> to provide a proper test method - hasRemoteSocketClosed(), for
> example?
The socket interface provided by the OS works that way, and in that
respect there is no difference reading from any kind of descriptor,
regardless of the underlying structure (file, pipe, socket, character
device, etc). Not all of these interact with a remote that actively
closes the other end.
Note too that the remote may have closed while you have remaining
unread data, in which case you don't actually reach EOF until you've
read to the end of the data stream.
If you don't want to risk blocking, use a Selector. It provides the
necessary test, and will tell you when you can safely read *without*
blocking.
/gordon
--
|