Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Network transfer pauses

Tags:

c#

sockets

I have made a server and a client in C# which transfers files. But when transferring it pauses for some seconds and then continues. I have uploaded a video on YouTube to demonstrate: http://www.youtube.com/watch?v=GGRKRW6ihLo

As you can see it sometime pauses and then continues.

Receiver:

        using (var writer = new StreamWriter(Path.Combine(_localPath, file.FileName))) {
            var buffer = new byte[_bufferSize];
            while (file.Transferred < file.Size && _active) {
                int read = ns.Read(buffer, 0, _bufferSize);
                writer.BaseStream.Write(buffer, 0, read);
                file.Transferred += read;
            }
            writer.Close();
        }

Sender:

            using (var reader = new StreamReader(file.FilePath)) {
                var buffer = new byte[_bufferSize];
                while (file.Transferred < file.Size && _active) {
                    int read = reader.BaseStream.Read(buffer, 0, _bufferSize);
                    ns.Write(buffer, 0, read);
                    file.Transferred += read;
                }
                reader.Close();
            }
like image 514
ErikTJ Avatar asked Feb 19 '10 13:02

ErikTJ


People also ask

Why is file transfer so slow over network?

File transfer is slow This is because file copy speeds are limited by storage speed. File copies sometimes start fast and then slow down.

Why is my transfer so slow?

The most common causes include disk fragmentation, file system errors, outdated drivers, antivirus settings, and some other Windows features. If you need to transfer files frequently and find the slow copy speed in Windows 10 quite frustrating, please try following methods one by one for troubleshooting.

Why data transfer is not working?

The file transfer feature is not enabled on your Android mobile/tablet. Your USB cable is defective. Your Android device or Mac computer is not compatible with Android File Transfer. Your Mac's USB port got damaged.

How do I copy files without interruption?

Go to Windows File Explorer, select the files from the source folder, right-click and choose Copywhiz–>Copy as shown below: Go to the destination folder, right-click and choose Copywhiz–> Paste Advanced. The advanced settings dialogue box will open. Select the “General” tab and enable the option “Run silently”.


2 Answers

Try setting

// Sends data immediately upon calling NetworkStream.Write.
tcpClient.NoDelay = true;

Send

var buffer = new byte[ tcpClient.SendBufferSize ];

Read

var buffer = new byte[ tcpClient.ReceiveBufferSize ];

The TcpClient.SendBufferSize and TcpClient.ReceiveBufferSize can vary depending on platform. I have in some cases made the buffer half the size or twice the size - of the TcpClient Buffer size.

like image 190
Famdam Avatar answered Sep 18 '22 21:09

Famdam


Nice video.... What is your _bufferSize set to? Drop down the size and see if it changes the behavior. My transfers were less lumpy when I cut the size in half. Have fun!

like image 23
sparkkkey Avatar answered Sep 21 '22 21:09

sparkkkey