0

I have win socket apps which are described here. So far, I was using ReadAllBytes method and read everything from my big file into memory. I made a new method which will read my file in chunks which will help my memory. Here is the method:

public static void ChunksToSend(string path)
{
   int chunkSize = 1024;
   byte[] chunk = new byte[chunkSize];

   using (FileStream fileReader = new FileStream(path, FileMode.Open, FileAccess.Read))
   {
        BinaryReader binaryReader = new BinaryReader(fileReader);
        int bytesToRead = (int)fileReader.Length;
        do
        {
            chunk = binaryReader.ReadBytes(chunkSize);
            bytesToRead -= chunkSize;
        } while (bytesToRead > 0);

        binaryReader.Close();
   }
}

Now, I need to connect my new method with this line from the link at Client side:

int bytesSent = sender.Send(result);

It looks easy, but I have a problem with TCP header which I need to send to in order to get file size and extension code. Lines at client side are:

//join arrays, file size info, TCP header
...
//file extension info, TCP Header

So, I need to join everything and to send it in iteration like I was doing it before chunks. The question might look easy, but I would not post it if I could do it. Thanks.

UPDATE:

For the first 4 bytes where I am sending file size info, I was using information from ReadAllBytes, but now I can use:

long length = new System.IO.FileInfo(path).Length;
Community
  • 1
  • 1
Ferid Š. Sejdović
  • 998
  • 4
  • 18
  • 48

1 Answers1

0

I managed to solve this problem:

        public static void StartClient()
        {
            byte[] msg;
            try
            {
                string inputFile = @"C:\TCPIP\test_big.iso";
                IPAddress ipAd = IPAddress.Parse("192.168.137.71");
                IPEndPoint remoteEP = new IPEndPoint(ipAd, 1234);
                Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                sender.Connect(remoteEP);

                Console.WriteLine("Client connected to {0}", sender.RemoteEndPoint.ToString());
                Console.WriteLine("Sending file...");

                //8 bytes, file lenght info, TCP Header


                long length = new System.IO.FileInfo(inputFile).Length;
                byte[] fileLenght = BitConverter.GetBytes(length);

                //1 byte, file extension info, TCP Header
                byte extension = 2; //file extension code
                byte[] newArray = new byte[fileLenght.Length + 1];
                fileLenght.CopyTo(newArray, 1);
                newArray[0] = extension;
                fileLenght = newArray;

                //send TCP Header
                int bytesSent = sender.Send(fileLenght);

                //send file in chunks
                int chunkSize = 1024;
                byte[] chunk = new byte[chunkSize];
                int SendPackage;
                using (FileStream fileReader = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
                {
                    BinaryReader binaryReader = new BinaryReader(fileReader);
                    int bytesToRead = (int)fileReader.Length;
                    do
                    {
                        chunk = binaryReader.ReadBytes(chunkSize);
                        bytesToRead -= chunkSize;
                        SendPackage = sender.Send(chunk);
                    } while (bytesToRead > 0);

                binaryReader.Close();
                }

                sender.Shutdown(SocketShutdown.Both);
                sender.Close();

                Console.WriteLine("\nPress ENTER to continue...");
                Console.Read();

            }
            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());
            }
        }
Ferid Š. Sejdović
  • 998
  • 4
  • 18
  • 48