'TCP Client becomes unreadable after server restart

Code:

          while(true)
          {
                Int32 port = Convert.ToInt32(Port);
                TcpClient client = new TcpClient(server, port);
                Byte[] data;
                NetworkStream stream = client.GetStream();
                data = new Byte[256];
                String responseData = String.Empty;
                //stream.ReadTimeout = 10000;
                Int32 bytes = stream.Read(data, 0, data.Length);
                responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                Console.WriteLine(responseData);
                stream.Close();
                client.Close();
          }

I am reading data from remote server as TCP client.The following line of code waits. Until the data arrives.But it is still waiting when the after server is down.

 Int32 bytes = stream.Read(data, 0, data.Length);

When I add the following line of code, it stops reading after a while and constantly crashes.(Server open)

stream.ReadTimeout = 10000;

What I want is for the server to continue reading when it restarts.but it Doesn't keep reading. What could be the reason.
Solved: stream.ReadTimeout = 10000; after reconnected.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source