I am using the TcpClient to connect to a web site. I then open a
NetworkStream and read the contents that are being sent back. The
problem is that I have no idea when the remote host is finished
sending data. It is sending a web page but it doesn't use the
Content-Length header to indicate the size.
While I can use the DataAvailable property, it is well known that you
cannot rely on this to know when no more data is available.
What I really want is to know when the host has closed the underlying
socket. I have a loop where I continually read from the host when the
DataAvailable is true. At some point however, I was hoping that the
remote host would close the socket and I would capture an exception
indicating a closed socket. But my try...catch never gets an exception
and I remain forever in the loop.
I can't be 100% certain that the remote host is closing the
connection, but if I have read all of the data, why wouldn't it?
How can I get my code to capture an exception indicating a closed
socket? Maybe TcpClient is the wrong way to go?
Thanks,
Johann