Problems w/ DataAvailable and NetworkStream

  • Thread starter Thread starter Jeremy
  • Start date Start date
J

Jeremy

I'm using NetworkStream to read from a TCPClient.

I'm testing DataAvailable before reading to keep by thread from
blocking on read.

The docs say that if the remote end has closed the connection, a call
to DataAvailable should throw a SocketException. Unfortunatly, the
exception isn't getting thrown, so my code keeps spinning on
DataAvailable forever, never knowing that the connection has been
closed. There don't seem to be any other properties or methods in
either TCPClient or NetworkStream that I can use to get the connection
state without potentially blocking.

Am I missing something? Is there another way to determine if the
connection is active without risking a block?

Thanks.

* the reply-to mailbox is not checked
* send replies to: jj AT imtc DOT gatech DOT edu.
 
My original question still stands, however I've found a work-around.

Rather than getting a NetworkStream from TcpClient, I'm now getting a
Socket from the TcpListener and creating a NetworkStream from the
Socket. This allows me to use the Socket.Poll( xxx,
SelectMode.SelectRead) call.

Poll will return true when data is available or when the remote
connection is closed. A following read call will then return -1 if
the socket is closed.
 
Back
Top