B
B
BadCats2019-12-15 17:38:20
C++ / C#
BadCats, 2019-12-15 17:38:20

Transferring a large file over UDP - in parts?

Need to transfer the saved web page to the client:
Trying to split and transfer the file in parts:
Server:

private static void SendFile()
        {
            // Создаем файловый поток и переводим его в байты
            Byte[] bytes = new Byte[UdpFileServer.fs.Length];
           

            Console.WriteLine("Отправка файла размером " + UdpFileServer.fs.Length + " байт");

            UdpFileServer.fs.Read(bytes, 0, bytes.Length);




            try
            {
                if (bytes.Length > 8192)
                {
                    int offset = 0;
                    int iterations = bytes.Length / 8192;
                    Byte[][] arr = new Byte[iterations][];
                    for (int i = 0; i < iterations; i++)
                    {
                        arr[i] = new Byte[8192];
                        for (int j = 0; j < 8192; j++)
                        {
                            arr[i][j] = bytes[j + offset];
                            
                        }
                        offset += 8192;
                    }

                    for (int i = 0; i < iterations; i++)
                    {
                        Byte[] data = arr[i];
                        UdpFileServer.sender.Send(data, 8192, UdpFileServer.endPoint);
                    }
                }
                //// Отправляем файл

            }
            catch (Exception eR)
            {
                Console.WriteLine(eR.ToString());
            }
            finally
            {
                // Закрываем соединение и очищаем поток
                UdpFileServer.fs.Close();
                UdpFileServer.sender.Close();
            }

            Console.WriteLine("Файл успешно отправлен.");
            Console.Read();
        }

Customer:
public static void ReceiveFile()
        {
            try
            {
                Console.WriteLine("-----------*******Ожидайте получение файла*******-----------");
              int iterations =   (int)fileDet.FILESIZE / 8192;
                Byte[][] data = new Byte[iterations][];
                int k = 0;
                fs = new FileStream("temp." + fileDet.FILETYPE, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
                for (int i = 0; i < iterations; i++)
                {
                    data[i] = new Byte[8192];
                    data[i] = receivingUdpClient.Receive(ref RemoteIpEndPoint);
                    fs.Write(data[i], 0, data.GetLength(0));
                    //k = 8192 * i;
                }
                // Получаем файл
               

                // Преобразуем и отображаем данные
                Console.WriteLine("----Файл получен...Сохраняем...");

                // Создаем временный файл с полученным расширением
                
                

                Console.WriteLine("----Файл сохранен...");

               

             
            }
            catch (Exception eR)
            {
                Console.WriteLine(eR.ToString());
            }
            finally
            {
                fs.Close();
                receivingUdpClient.Close();
                Console.Read();
            }
        }

- in the end, the client - just hangs on the message
Console.WriteLine("-----------*******Wait to receive the file*******------- ----");
- exception - a - no.
Also, there was a question about writing to a file in a loop:
fs.Write(data[i], 0, data.GetLength(0));
                    //k = 8192 * i;

- instead of 0 - indicated the offset k, but went beyond the boundaries of the array.
PS - Unfortunately, for the time being, I have no theoretical knowledge of network operation, and in c # - I have never worked with this, but there was an urgent need to do it "here and now".

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sumor, 2019-12-16
@Sumor

UDP does not guarantee delivery. Packets may get lost. Packets may arrive in a different order.
The length of the file may not be a multiple of 8192. There may be an error in the RemoteIpEndPoint. The firewall may be enabled.
Your software should be ready for this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question