tags:

views:

127

answers:

4

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();
            }
+1  A: 

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!

sparkkkey
I think this combined with Famdam solved it.
Erik
+1  A: 

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.

Famdam
I think this made an improvement, but I'm not sure. The pauses are so random so it's hard to tell.
Erik
+1  A: 

You could take a look at the sendfile http://msdn.microsoft.com/en-us/library/sx0a40c2.aspx

It uses the kernel to avoid the copying of data around in memory.

Cine
This did not solve the problem
Erik
A: 

I wrote a blog post on SendFile and it's perf advantages. See http://ferozedaud.blogspot.com/2009/12/socketsendfile-implementing-fast-file.html

feroze