title: Sending a file using TCPClient and NetworkStream in C#
body: <p>I've been trying to send a file from a client to a server application using the TCPClient class in C#. Before I send the actual data, I send some additional information like the exact file size and the file name, so the server application knows how much there is to read. The funny thing is everything was fine when I tested it on 127.0.0.1 - as soon as I replaced the IP address with the actual one, the server could only read about 1,5 KByte of the data that was sent. It still gets the filename and the file size, but theres no way it's retrieving the actual data. </p>
  
  <p>For testing purposes, I replaced the image I was going to send with a simple string and the transmission went alright, so I suppose there is a problem with sending and receiving the data chunks, but I'm not getting any exceptions on the client side either.</p>
  
  <p>Anyone got an idea? Cheers!</p>
  
  <p>Edit:</p>
  
  <p>Thanks so far, this is what I have got codewise. For the client:</p>
  
  <pre><code>        IPAddress ipAddress = IPAddress.Parse("xx.xx.xx.xx");
          int port = 3003;
          int bufferSize = 1024;
  
          TcpClient client = new TcpClient();
          NetworkStream netStream;
  
          // Connect to server
          try
          {
              client.Connect(new IPEndPoint(ipAddress, port));
  
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.Message);
          }
  
          netStream = client.GetStream();
  
          // Read bytes from image
          byte[] data = File.ReadAllBytes("C:\\Users\\Dan\\Desktop\\asdf.jpg");
  
          // Build the package
          byte[] dataLength = BitConverter.GetBytes(data.Length);
          byte[] package = new byte[4 + data.Length];
          dataLength.CopyTo(package, 0);
          data.CopyTo(package, 4);
  
          // Send to server
          int bytesSent = 0;
          int bytesLeft = package.Length;
  
          while (bytesLeft &gt; 0)
          {
  
              int nextPacketSize = (bytesLeft &gt; bufferSize) ? bufferSize : bytesLeft;
  
              netStream.Write(package, bytesSent, nextPacketSize);
              bytesSent += nextPacketSize;
              bytesLeft -= nextPacketSize;
  
          }
  
          // Clean up
          netStream.Close();
          client.Close();
  </code></pre>
  
  <p>And the server:</p>
  
  <pre><code>        TcpListener listen = new TcpListener(3003);
          TcpClient client;
          int bufferSize = 1024;
          NetworkStream netStream;
          int bytesRead = 0;
          int allBytesRead = 0;
  
          // Start listening
          listen.Start();
  
          // Accept client
          client = listen.AcceptTcpClient();
          netStream = client.GetStream();
  
          // Read length of incoming data
          byte[] length = new byte[4];
          bytesRead = netStream.Read(length, 0, 4);
          int dataLength = BitConverter.ToInt32(length,0);
  
          // Read the data
          int bytesLeft = dataLength;
          byte[] data = new byte[dataLength];
  
          while (bytesLeft &gt; 0)
          {
  
              int nextPacketSize = (bytesLeft &gt; bufferSize) ? bufferSize : bytesLeft;
  
              bytesRead = netStream.Read(data, allBytesRead, nextPacketSize);
              allBytesRead += bytesRead;
              bytesLeft -= bytesRead;
  
          }
  
          // Save image to desktop
          File.WriteAllBytes("C:\\Users\\Dan\\Desktop\\tcpimage.jpg", data);
  
          // Clean up
          netStream.Close();
          client.Close();
  </code></pre>