Reputation: 3854
I am using TCP client
Here is my code
do
{
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
bytes = stream.Read(data, 0, data.Length);
} while (bytes != 0);
I got the error here sometimes
bytes = stream.Read(data, 0, data.Length);
The exisiting connection is forcibly closed by the server.
How to handle it.
How to check if the stream can read on not.
On server side i am writing to the stream and closing it. Should i do something on server??
Upvotes: 1
Views: 1100
Reputation: 1062965
That code will work fine for a normal / healthy scenario, where the stream closes normally. What you describe ("the existing connection is forcibly closed by the server") is not the normal / healthy scenario, so it is entirely reasonable that it tells you about this problem, which it does by throwing an exception. The same as you might expect if you are reading from a file, and the file-system becomes unavailable (somebody yanks a disk / pen-drive, etc).
So: catch
the exception and handle it appropriately.
There isn't anything special you can do on the server except close the stream nicely.
Upvotes: 3