title: C# client server application, BinaryReader throws an exception
body: <p>I've got task to make a server application, which checks if any file on the server (which name must be sent by the client) exists and if it exists to give back a response (message - "This file exists."). I've got problem with this. The server is making the right thing but when I try to use my BinaryReader to read the response it gives me back this exception:</p>
  
  <pre><code>Unable to read beyond the end of the stream
  </code></pre>
  
  <p>Here is some code from the client:</p>
  
  <pre><code>    private void OnRequest()
      {
          try
          {
              IPEndPoint serverIp = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 55555);
              TcpClient client = new TcpClient();
              client.Connect(serverIp);
              if (client.Connected)
              {
                  AddLog("Connected!");
  
                  writer = new BinaryWriter(client.GetStream());
  
                  writer.Write("request");
                  GetFileText();
                  writer.Write(fileText);
                  writer.Flush();
  
                  AddServerResponse(GetResponse(client.Client));
  
                  writer.Close();
                  client.Close();
                  AddLog("Disconnected!");
              }
  
          }
          catch (Exception e)
          {
              MessageBox.Show(e.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
          }
      }
  </code></pre>
  
  <p>And that's the GetResponse method:</p>
  
  <pre><code>    private string GetResponse(Socket server)
      {
          NetworkStream stream = new NetworkStream(server);
          reader = new BinaryReader(stream);
          string message;
          do
          {
              message = reader.ReadString();
          }
          while (message != null);
          reader.Close();
          return message;
      }
  </code></pre>
  
  <p>And here how I'm sending data to the client:</p>
  
  <pre><code>    private void SendDataToClient(string data, Socket client)
      {
          if (client != null &amp;&amp; client.Connected)
          {
              NetworkStream stream = new NetworkStream(client);
              BinaryWriter writer = new BinaryWriter(stream);
              writer.Write(data);
              writer.Flush();
              writer.Close();
              stream.Close();
          }
      }
  </code></pre>
  
  <p>Could you help me, please?</p>