More .NET components

Transfer speed management

Limiting transfer speed (bandwidth throttling) #

High transfer speed is not always desirable - if you prefer high network throughput, you might wish to limit the transfer speed.

To achieve this, use MaxDownloadSpeed and MaxUploadSpeed properties:

CSharp

// set download and upload speed limits
sftp.MaxDownloadSpeed = 4 * 1024;
sftp.MaxUploadSpeed = 2 * 1024;

// transfer files
// ...

VisualBasic

' set download and upload speed limits
sftp.MaxDownloadSpeed = 4 * 1024
sftp.MaxUploadSpeed = 2 * 1024

' transfer files
' ...

Speed monitoring (transfer speed event) #

To keep informed about the current speed of the transfer (in order to display it to the user, for example), register a TransferProgressChanged event handler. Current transfer speed is available through SftpTransferProgressChangedEventArgs.BytesPerSecond property, moving average calculation is used to smooth the value.

Sample event handler:

CSharp

void client_SpeedMonitoring(object sender, SftpTransferProgressChangedEventArgs e)
{
    // display current speed info
    Console.WriteLine("Current speed: {0} B/s", e.BytesPerSecond);
}

VisualBasic

Sub client_SpeedMonitoring(ByVal sender As Object, ByVal e As SftpTransferProgressChangedEventArgs)
    ' display current speed info
    Console.WriteLine("Current speed: {0} B/s", e.BytesPerSecond)
End Sub

Registering the event handler:

CSharp

// register TransferProgressChanged event handler
sftp.TransferProgressChanged += client_SpeedMonitoring;

// transfer files
// ...

VisualBasic

' register TransferProgressChanged event handler
AddHandler sftp.TransferProgressChanged, AddressOf client_SpeedMonitoring

' transfer files
' ...

Optimized transfer (request queuing) #

In SFTP protocol, files are transferred in blocks of several kilobytes each. For each block, the client needs to send a request and receive a response.

This is how a simple transfer might look like at the protocol level:
  Client request 1: Read 10000 bytes at position 0.
  Server response 1: Success. Data: [data blob]
  Client request 2: Read 10000 block at position 10000.
  Server response 2: Success. Data: [data blob]
  Client request 3: Read 10000 block at position 20000.
  Server response 3: Success. Data: [data blob]
  Client request 4: Read 10000 block at position 30000.
  Server response 4: Success. Data: [data blob]
  ...

However, this simple approach would result in suboptimal transfer speed on high-latency networks because the server has nothing to do while waiting for each subsequent request.

To achieve higher throughput, Rebex SFTP uses a more advanced approach - request queueing:
  Client request 1: Read 10000 bytes at position 0.
  Client request 2: Read 10000 block at position 10000.
  Client request 3: Read 10000 block at position 20000.
  Server response 1: Success. Data: [data blob]
  Client request 4: Read 10000 block at position 30000.
  Server response 2: Success. Data: [data blob]
  Client request 5: Read 10000 block at position 40000.
  Server response 3: Success. Data: [data blob]
  Client request 6: Read 10000 block at position 50000.
  Server response 4: Success. Data: [data blob]
  ...
This eliminates the delay at the server - it no longer has to wait for subsequent requests because they are already in its request queue.

All properly implemented SFTP servers should support this approach and it is enabled by default. To switch it off (not recommended), set Sftp.Settings.DisableTransferQueue to true.

Tip: To tweak transfer queue lengths and buffer sizes, use Sftp.Settings.DownloadQueueLength/Sftp.Settings.UploadQueueLength and Sftp.Settings.DownloadBufferSize/Sftp.Settings.UploadBufferSize properties.

Large buffer mode #

Occasionally, using larger buffers may increase the transfer speed, particularly for downloads. Set Sftp.Settings.UseLargeBuffers to true to use larger buffers.

Internal buffer sizes:

Buffer No option UseLargeBuffers
TCP socket's receive buffer Socket's default 4MB
TCP socket's send buffer Socket's default 256KB
SSH channel's receive buffer 128KB 2MB