title: Use StreamSocket to transfer files and receive wrong data (UWP)
body: <p>In my project, I use <strong>StreamSocket</strong> to send files and <strong>StreamSocketListener</strong> to receive files in LAN. But when I tested, my received file size is less than the file I sent.(For example, my sent file size are about 100M and received file size are just 2-3M)</p>
  
  <p>I am very very confused, here are my code, tks.</p>
  
  <p>Send Code (file is <strong>StorageFile</strong>, item is my custom class)</p>
  
  <pre><code>var fileStream = await file.OpenStreamForReadAsync();
  var fileInputStream = fileStream.AsInputStream();
  
  StreamSocket socket = new StreamSocket();
  HostName serverHost = new HostName(item.Address);
  string serverPort = item.Port;
  await socket.ConnectAsync(serverHost, serverPort);
  
  uint capacity = 10000000;
  Windows.Storage.Streams.Buffer buffer = new Windows.Storage.Streams.Buffer(capacity);
  
  // Send some heads
  StreamWriter streamWriter = new StreamWriter(socket.OutputStream.AsStreamForWrite());
  await streamWriter.WriteLineAsync("file");
  await streamWriter.WriteLineAsync(file.Name);
  await streamWriter.FlushAsync();
  
  var outputStream = socket.OutputStream;
  
  while (true)
  {
      await fileInputStream.ReadAsync(buffer, capacity, InputStreamOptions.None);
  
      if (buffer.Length &gt; 0)
      {
          await outputStream.WriteAsync(buffer);
          await outputStream.FlushAsync();
      }
      else
      {
          break;
      }
  }
  
  streamWriter.Dispose();
  outputStream.Dispose();
  socket.Dispose();
  </code></pre>
  
  <p>Receive Code (Event is <strong>StreamSocketListener.ConnectionReceived</strong>)</p>
  
  <pre><code>StreamReader streamReader = new StreamReader(args.Socket.InputStream.AsStreamForRead());
  string head = await streamReader.ReadLineAsync();
  
  string fileName = await streamReader.ReadLineAsync();
  
  StorageFile file = await DownloadsFolder.CreateFileAsync(fileName, CreationCollisionOption.GenerateUniqueName);
  var inputStream = args.Socket.InputStream;
  
  uint capacity = 10000000;
  Windows.Storage.Streams.Buffer buffer = new Windows.Storage.Streams.Buffer(capacity);
  
  if (file != null)
  {
      CachedFileManager.DeferUpdates(file);
  
      while (true)
      {
          await inputStream.ReadAsync(buffer, capacity, InputStreamOptions.None);
  
          if (buffer.Length &gt; 0)
          {
              await FileIO.WriteBufferAsync(file, buffer);
          }
          else
          {
              break;
          }
      }
  
      FileUpdateStatus updateStatus = await CachedFileManager.CompleteUpdatesAsync(file);
  }
  </code></pre>