Socket.EndRead() is documented as throwing a IOException should the
underlying socket be closed when/during EndRead() is called.

If the NetworkStream object is closed, an ObjectDisposedExceptin will be
thrown.

You could test the socket's Connected property to validate whether the
remote host is still connected; but that isn't going avoid the requirement
of catching the exceptions that EndBegin() is documented to throw as the
socket could be closed after testing Connected and before/during EndBegin
().

http://www.peterRitchie.com/

On Wed, 3 Aug 2005 04:30:20 -0400, Ragnvald Barth [EMAIL PROTECTED]>
wrote:

>I have two applications communicating through a socket.
>In the receiving application I use the NetworkStream class to read data
>from the socket.
>I call
>myNetworkStream.BeginRead(buffer, 0, size, new AsyncCallback
>(MyCallbackMethod), state);
>MyCallbackMethod is then called when data is coming in, or when the
>connection is closed by the sender. I then call myNetworkStream.EndRead(),
>and if the connection is closed I get an exception. Is catching this
>exception the only way to detect that sender has closed the connection?
>I tried to use NetworkStream.DataAvailable, but this property is always
>false when the callback method is called, unless I set a breakpoint or
>call Thread.Sleep(10), then I guess some other thread sets it to true if
>the connection is open. So DataAvailable does not work...
>I guess I am looking for some OnConnectionClosedEvent, or some boolean
>property I could test on...

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to