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();
}
File transfer is slow This is because file copy speeds are limited by storage speed. File copies sometimes start fast and then slow down.
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.
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.
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”.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With