title: How to send large file ( 500MB - 1GB ) over TCP/IP in c#?
body: <p>I am using below code to send sample data into stream of bytes and is working perfectly fine.</p>
  
  <p><strong>CODE Used</strong></p>
  
  <p><strong>CLIENT</strong> </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;
  using System.Net.Sockets;
  using System.Text;
  using System.Threading.Tasks;
  
  class Program
  {
      static void Main(string[] args)
      {
          TcpClient tcpClient = new TcpClient("localHost", 5200);
  
          NetworkStream networkStream = tcpClient.GetStream();
          BufferedStream bs = new BufferedStream(networkStream);
  
  
          //Send data to listener
          byte[] dataToSend = new byte[100];
          new Random().NextBytes(dataToSend);
          for (int i = 0; i &lt; 100; i++)
              networkStream.Write(dataToSend, 0, dataToSend.Length);
  
          //when the network stream is closed, it also shuts down the connection
          networkStream.Close();
  
          Console.WriteLine("Done");
          Console.ReadLine();
      }
  
  }
  </code></pre>
  
  <p><strong>SERVER</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;
  using System.Net;
  using System.Net.Sockets;
  using System.Text;
  using System.Threading.Tasks;
  
  class Program
  {
      static void Main(string[] args)
      {
          IPAddress ip = IPAddress.Parse("127.0.0.1");
          TcpListener tcpListener = new TcpListener(ip, 5200);
          tcpListener.Start();
  
          Console.WriteLine("Waiting for a client to connect...");
  
          //blocks until a client connects
          Socket socketForClient = tcpListener.AcceptSocket();
  
          Console.WriteLine("Client connected");
  
          //Read data sent from client
          NetworkStream networkStream = new NetworkStream(socketForClient);
          int bytesReceived, totalReceived = 0;
          byte[] receivedData = new byte[1000];
          do
          {
              bytesReceived = networkStream.Read
                  (receivedData, 0, receivedData.Length);
              totalReceived += bytesReceived;
          }
          while (bytesReceived != 0);
          Console.WriteLine("Total bytes read: " + totalReceived.ToString());
  
          socketForClient.Close();
          Console.WriteLine("Client disconnected...");
  
          Console.ReadLine();
      }
  }
  </code></pre>
  
  <p>I don't know how to send the data from a file which can be of very large size in the same way.</p>
  
  <p>I tried the below code but it is not working if size of file is 30MB or more.</p>
  
  <pre><code>public void SendTCP(string filePath, string IPA, Int32 PortN)
      {
          byte[] SendingBuffer = null;
          TcpClient client = null;
          lblStatus.Text = "";
          NetworkStream netstream = null;
          try
          {
              client = new TcpClient(IPA, PortN);
              lblStatus.Text = "Connected to the Server...\n";
              netstream = client.GetStream();
              FileStream Fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
              int NoOfPackets = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(Fs.Length) / Convert.ToDouble(BufferSize)));
              progressBar1.Maximum = NoOfPackets;
              int TotalLength = (int)Fs.Length, CurrentPacketLength, counter = 0;
              for (int i = 0; i &lt; NoOfPackets; i++)
              {
                  if (TotalLength &gt; BufferSize)
                  {
                      CurrentPacketLength = BufferSize;
                      TotalLength = TotalLength - CurrentPacketLength;
                  }
                  else
                      CurrentPacketLength = TotalLength;
                  SendingBuffer = new byte[CurrentPacketLength];
                  Fs.Read(SendingBuffer, 0, CurrentPacketLength);
                  netstream.Write(SendingBuffer, 0, (int)SendingBuffer.Length);
                  if (progressBar1.Value &gt;= progressBar1.Maximum)
                      progressBar1.Value = progressBar1.Minimum;
                  progressBar1.PerformStep();
              }
  
              lblStatus.Text = lblStatus.Text + "Sent " + Fs.Length.ToString() + " bytes to the server";
              Fs.Close();
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.Message);
          }
          finally
          {
              netstream.Close();
              client.Close();
  
          }
      }
  </code></pre>