开发者

Network transfer pauses

开发者 https://www.devze.com 2022-12-20 21:07 出处:网络
I have mad开发者_JAVA百科e 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: ht

I have mad开发者_JAVA百科e 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();
            }


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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号