title: Sending multiple files Client & Server
body: <p>I have a Client and Server code for files sending. For some reason I need to receive at client and send from Server...</p>
  
  <p>Everything work perfectly, in some cases all files sent and received perfectly. In another cases after sending a few files programm crashes. Don't understand where the problem...</p>
  
  <p>Errors:</p>
  
  <p><a href="https://docs.google.com/file/d/0BwlO7eJMYW_4ajFla1dPczZjTEE/edit" rel="nofollow">client colsole</a></p>
  
  <p><a href="https://docs.google.com/file/d/0BwlO7eJMYW_4ZXh3aGo3eENpN2c/edit" rel="nofollow">server console</a></p>
  
  <p>client</p>
  
  <pre><code>// client code
  using System;
  using System.IO;
  using System.Collections.Generic;
  using System.Linq;
  
  using System.Net;
  using System.Net.Sockets;
  using System.Text;
  
  class Client003
  {
      const string destFilePath = @"..\..\..\";
      const int BufferSize = 1024;
  
      public static void StartReceiving()
      {
          // Data buffer for sending data.
          byte[] buffer;
  
          // FileStream to read data
          FileStream fileStream;
          int fileNameLen = 0;
          string fileName = "";
          long fileLen = 0;
          int NoOfPackets = 0;
          int receivedBytes = 0;
          int i, j;
  
          // Connect to a remote device.
          try
          {
              // Establish the remote endpoint for the socket.
              // This example uses port 11000 on the local computer.
              IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
              IPAddress ipAddress = ipHostInfo.AddressList[0];
              IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);
  
              // Create a TCP/IP  socket.
              Socket receiver = new Socket(AddressFamily.InterNetwork,
                  SocketType.Stream, ProtocolType.Tcp);
  
              // Connect the socket to the remote endpoint. Catch any errors.
              try
              {
                  receiver.Connect(remoteEP);
  
                  buffer = new byte[4];
                  receiver.Receive(buffer, 4, 0);
                  int filesNumber = BitConverter.ToInt32(buffer, 0);
  
                  for (i = 0; i &lt; filesNumber; i++)
                  {
                      buffer = new byte[4];
                      receiver.Receive(buffer, 4, 0);
                      fileNameLen = BitConverter.ToInt32(buffer, 0);
                      // --
                      buffer = new byte[fileNameLen];
                      receiver.Receive(buffer, fileNameLen, 0);
                      fileName = Encoding.UTF8.GetString(buffer);
                      // --
                      buffer = new byte[8];
                      receiver.Receive(buffer, 8, 0);
                      fileLen = BitConverter.ToInt64(buffer, 0);
                      // --
                      NoOfPackets = Convert.ToInt32(Math.Ceiling(
                          Convert.ToDouble(fileLen) / Convert.ToDouble(BufferSize) ));
                      fileStream = new FileStream(destFilePath + fileName, FileMode.OpenOrCreate, FileAccess.Write);
                      receivedBytes = 0;
                      // --
                      for (j = 0; j &lt; NoOfPackets; j++)
                      {
                          if (fileLen &gt; BufferSize)
                          {
                              buffer = new byte[BufferSize];
                              receivedBytes = receiver.Receive(buffer, BufferSize, 0);
                              fileStream.Write(buffer, 0, receivedBytes);
                              fileLen -= BufferSize;
                          }
                          else
                          {
                              buffer = new byte[fileLen];
                              receivedBytes = receiver.Receive(buffer, (int)fileLen, 0);
                              fileStream.Write(buffer, 0, receivedBytes);
                          }
                      }
                      fileStream.Close();
                  }
                  // Release the socket.
                  receiver.Shutdown(SocketShutdown.Both);
                  receiver.Close();
  
              }
              catch (ArgumentNullException ane)
              {
                  Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
              }
              catch (SocketException se)
              {
                  Console.WriteLine("SocketException : {0}", se.ToString());
              }
              catch (Exception e)
              {
                  Console.WriteLine("Unexpected exception : {0}", e.ToString());
              }
  
          }
          catch (Exception e)
          {
              Console.WriteLine(e.ToString());
          }
      }
  
      public static int Main(String[] args)
      {
          StartReceiving();
          return 0;
      }
  }
  </code></pre>
  
  <p>server</p>
  
  <pre><code>//server code
  using System;
  using System.IO;
  
  using System.Net;
  using System.Net.Sockets;
  using System.Text;
  
  using System.Collections.Generic;
  
  class Server003
  {
      public static void StartListening()
      {
          // Data buffer for incoming data.
          byte[] buffer;
          byte[] fileNameByte;
          byte[] fileNameLenByte;
          byte[] fileLenByte;
  
          // FileStream to write data
          FileStream fileStream;
          Int64 fileLen = 0;
          int NoOfPackets = 0;
          int readBytes = 0;
          int i;
  
          // Establish the local endpoint for the socket.
          // Dns.GetHostName returns the name of the 
          // host running the application.
          IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
          IPAddress ipAddress = ipHostInfo.AddressList[0];
          IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
  
          // Create a TCP/IP socket.
          Socket listener = new Socket(AddressFamily.InterNetwork,
              SocketType.Stream, ProtocolType.Tcp);
  
          // Bind the socket to the local endpoint and 
          // listen for incoming connections.
          try
          {
              listener.Bind(localEndPoint);
              listener.Listen(10);
  
              // Start listening for connections.
              Console.WriteLine("Waiting for a connection...");
              // Program is suspended while waiting for an incoming connection.
              Socket handler = listener.Accept();
  
              Int32 filesNumber = binFilesNames.Count;
              byte[] filesNumberByte = BitConverter.GetBytes(filesNumber);
              handler.Send(filesNumberByte);
              // --
              foreach (string binName in binFilesNames)
              {
                  fileNameByte = Encoding.UTF8.GetBytes(binName);
                  fileNameLenByte = BitConverter.GetBytes(fileNameByte.Length);
                  handler.Send(fileNameLenByte);
                  handler.Send(fileNameByte);
                  // --
                  fileStream = new FileStream(sourceFilePath + binName, FileMode.Open, FileAccess.Read);
                  fileLen = fileStream.Length;
                  fileLenByte = BitConverter.GetBytes(fileLen);
                  handler.Send(fileLenByte);
                  // --
                  NoOfPackets = Convert.ToInt32(Math.Ceiling(
                      Convert.ToDouble(fileLen) / Convert.ToDouble(BufferSize)));
                  for (i = 0; i &lt; NoOfPackets; i++)
                  {
                      if (fileLen &gt; BufferSize)
                      {
                          buffer = new byte[BufferSize];
                          // reeding data from file and writing it to the bytes "buffer"
                          readBytes = fileStream.Read(buffer, 0, BufferSize);
                          // send bytes from "buffer"
                          handler.Send(buffer, readBytes, SocketFlags.None);
                          fileLen -= BufferSize;
                      }
                      else
                      {
                          buffer = new byte[fileLen];
                          // reeding data from file and writing it to the bytes "buffer"
                          readBytes = fileStream.Read(buffer, 0, (int)fileLen);
                          // send bytes from "buffer"
                          handler.Send(buffer, readBytes, SocketFlags.None);
                      }
                  }
                  fileStream.Close();
              }
              // Release the socket.
              handler.Shutdown(SocketShutdown.Both);
              handler.Close();
  
          }
          catch (Exception e)
          {
              Console.WriteLine(e.ToString());
          }
  
          Console.WriteLine("\nPress ENTER to continue...");
          Console.Read();
      }
  
  
      public static List&lt;string&gt; GetFiles()
      {
          var dir = new DirectoryInfo(sourceFilePath);          // folder with files 
          var files = new List&lt;string&gt;();                 // List with file names 
  
          foreach (FileInfo file in dir.GetFiles("T*260000.bin"))
          {
              files.Add(Path.GetFileName(file.FullName));
          }
  
          return files;
      }
  
      public static int Main(String[] args)
      {
          binFilesNames = GetFiles();
          StartListening();
  
          return 0;
      }
  
      const string sourceFilePath = @"..\..\..\Binaries\";
      static List&lt;string&gt; binFilesNames;
      const int BufferSize = 1024;
  }
  </code></pre>
  
  <p><strong>UPD:</strong>
  I took into account the moments that pointed LB2. Here is the receive part and it works as it need:</p>
  
  <pre><code>while ((receivedBytes = receiver.Receive(buffer)) &gt; 0)    // receive bytes to "buffer"
  {
      var tmpBuff = buffer.Take(receivedBytes);   // takes first receivedBytes elements
      bufferList.AddRange(tmpBuff);
  }
  </code></pre>
  
  <p>But I don't understand how sending work. When I send whole data at once - all ok, but when i trying to send partially it crashes:</p>
  
  <p>This works and whole data sent:</p>
  
  <pre><code>handler.Send(buffer);
  </code></pre>
  
  <p>This one crashes:</p>
  
  <pre><code>int sentBytes = 0;
  int sumSentBytes = 0;
  do
  {
      // send bytes from "buffer"
      sentBytes = handler.Send(buffer, sumSentBytes, BufferSize, SocketFlags.None);
      sumSentBytes += sentBytes;
  }
  while (sentBytes &gt; 0);
  </code></pre>
  
  <p>So what is the best way to construct sending of large amounts of data (in my case about 20Mb, but it depends)?</p>