A
A
Anton Baryshev2021-07-24 12:29:51
.NET
Anton Baryshev, 2021-07-24 12:29:51

How to transfer multiple large files over TCP using .NET?

Hello. I need to write a program that will send video files to given addresses. Since the files must arrive intact, it was decided to use the TCP protocol. At the moment I'm doing something like this:

_server.Connect(endPoint);
_server.SendFile(filePath);
_server.Close();

_server is an object of the Socket class.

On the client, I receive the file like this:
using (FileStream fs = new FileStream(
                    @$"D:\Repositories\VideoSenderClient\VideoSenderClient\ReceivedFiles\{System.Guid.NewGuid()}.txt",
                    FileMode.Create))
                {
                    NetworkStream ns = new NetworkStream(handler);
                    while (handler.Available > 0)
                    {
                        ns.Read(buffer, 0, buffer.Length);
                        fs.Write(buffer);
                    }
                }


The problem is that I don't understand how to send multiple files one by one and how to send their size and name. I would be very grateful for help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2021-07-24
@AVollane

Send the file size first, then the file. Subtract the size, subtract the specified number of bytes and subtract the size again, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question