Jump to content
  • Advertisement
Sign in to follow this  
Andrew Russell

[.net] TcpClient.GetStream().Read() keeps blocking on server disconnect (solved, own fault)

This topic is 4483 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The title says it all. I'm using TcpClient to connect to a server. When the server disconnects, NetworkStream.Read keeps on blocking. I can trick it into getting the idea that it's disconnected by sending some data, but I really need a way to do it that doesn't involve sending anything. Any ideas on what is happening and how to fix it? [Edited by - Andrew Russell on May 6, 2006 8:37:42 PM]

Share this post


Link to post
Share on other sites
Advertisement
This is a problem with TCP – the socket doesn't know it's been closed until you try to use it. You can poll the socket instead using Socket.Available (or NetworkStream.DataAvailable), which will return the amount of data present or throw an exception if the socket is closed.

Share this post


Link to post
Share on other sites
OK, I tried doing this - I'm afraid it didn't work.

I also saw the Socket.Poll method in there - this didn't do anything either.

[Edited by - Andrew Russell on May 6, 2006 9:15:31 AM]

Share this post


Link to post
Share on other sites
This is Microsoft's answer..

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemnetsocketsnetworkstreamclassreadtopic.asp


theTroll

Share this post


Link to post
Share on other sites
What do you mean, 'didn't work'? It should never block, and it also should tell you if data is waiting. Which part didn't do what it should? (Remember you don't call Read unless there is data available!)

Share this post


Link to post
Share on other sites
Damn.

It turns out it was my own fault. I was losing the return value from Read - so the loop didn't end up exiting if the return value was zero.

Normally an infinate loop like that would be easy to detect - except that Read() would finally throw an exception and cause the loop to exit if data was sent to the disconnected socket.

Thanks for your help, guys.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!