Sending big files in socket programming c#
<p>I want to send big files with socket in c#. File can be transferred but when I want to open it, I see it is damaged. What's the problem?
I broke the file to 2KB in array in client code and send it. Then, in server code, I received it and put it in byte array and convert to file.</p>

<p>server code:</p>

<pre><code>    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace MyServer
{
    class Program
    {
        static void Main(string[] args)
        {
            bool full = false;
            byte[] data = new byte[2049];
            byte[] bsize = new byte[2048];
            List&lt;byte&gt; bytes = new List&lt;byte&gt;();
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);
            Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            newsock.Bind(ipep);
            newsock.Listen(100);
            Console.WriteLine("Wating For Client ...");
            Socket client = newsock.Accept();
            IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
            Console.WriteLine("Connected with {0} at port {1}",clientep.Address,clientep.Port);
            Console.Write("Enter the name of file:  ");
            string Address = Console.ReadLine();
            client.Receive(bsize);
            int size = BitConverter.ToInt32(bsize, 0);
            byte[] bytes2 = new byte[size];
            int k = 0;
            while (true)
            {
                client.Receive(data);
                for(int i = k,j=0; i &lt; k + 2048 &amp;&amp; j&lt;2048; i++,j++)
                {
                    if (i == size)
                    {
                        full = true;
                        break;
                    }
                    bytes2[i] = data[j];
                    //bytes.Insert(i,data[j]);
                }
                k += 2048;
                if (full == true)
                    break;
                /*if (bytes.Count &gt;= size)
                    break;*/
            }
            File.WriteAllBytes(@"G:\"+Address,bytes2 /*bytes.ToArray()*/);
            Console.WriteLine("Disconnected from Client {0}",clientep.Address);
            client.Close();
            newsock.Close();
            Console.ReadKey();
        }
    }
}
</code></pre>

<p>client code:</p>

<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace MyClient
{
    class Program
    {
        static void Main(string[] args)
        {
            int reza = 0;
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("192.168.0.2"), 9050);
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                server.Connect(ipep);
            }
            catch (SocketException e)
            {
                Console.WriteLine("Unable to connect to Server");
                Console.WriteLine(e.ToString());
                Console.ReadKey();
                return;
            }
            Console.Write("Enter Address Of File:");
            string Address = Console.ReadLine();
            FileStream File = new FileStream(Address, FileMode.Open, FileAccess.Read);
            byte[] bytes = System.IO.File.ReadAllBytes(@Address);
            File.Close();
            byte[] data = new byte[2048];
            byte[] Size = BitConverter.GetBytes(bytes.Length);
            server.Send(Size, Size.Length, SocketFlags.None);
            for (int i = 0; i &lt;= bytes.Length; i +=2048)
            {
                int k = i + 2048;
                for(int j=i,d=0 ;j&lt; k &amp;&amp; j&lt;bytes.Length; j++,d++)
                {
                    data[d] = bytes[j];
                    reza++;
                }
                if (reza == 2048)
                {
                    server.Send(data, data.Length, SocketFlags.None);
                    Console.Write("*");
                    reza = 0;
                }
                else
                {
                    server.Send(data, reza, SocketFlags.None);
                    Console.WriteLine("*");
                }
            }
            Console.WriteLine("Disconnecting from server ...");
            server.Shutdown(SocketShutdown.Both);
            server.Close();
            Console.ReadKey();
        }
    }
}
</code></pre>