title: How to give a sign and exit infinitive loop when sending file via socket
body: <p>I am trying to send a text file from cleint and to write it on server side, but I am not sure how exit the infinitive while(true) loop. Here is my code which is very simple.</p>
  
  <p>Client</p>
  
  <pre><code>byte[] msg = File.ReadAllBytes(@"C:\TCPIP\test_big.txt");
  int bytesSent = sender.Send(msg);
  </code></pre>
  
  <p>Server</p>
  
  <pre><code>while (true) 
  {
      Console.WriteLine("Waiting for a connection...");
      // Program is suspended while waiting for an incoming connection.
      Socket handler = listener.Accept();
      data = null;
      string outputPath = @"C:\output\2.txt";
  
      while (true) 
      {
          bytes = new byte[1024];
          int bytesRec = handler.Receive(bytes);
          data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
         //I need a "break;" here...
      }
      File.WriteAllText(outputPath, data);
      Console.WriteLine(File.Exists(outputPath) ? "File received." : "File not received.");
  
      handler.Shutdown(SocketShutdown.Both);
      handler.Close();
  
  }
  </code></pre>
  
  <p>I used example from <a href="https://msdn.microsoft.com/en-us/library/6y0e13d3(v=vs.110).aspx" rel="nofollow">MSDN</a>... </p>
  
  <p>So, my question is, how to exit the while loop when I receive all the bytes?
  Thanks.</p>