By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,284 Members | 1,246 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,284 IT Pros & Developers. It's quick & easy.

Problem determining when a remote host closes a socket. using TcpClient

P: n/a
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?

Nov 16 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.