Skip to content

Multiple Simultaneous Transfers

Andrew Lambert edited this page Jan 14, 2023 · 12 revisions

The cURLClient class, though convenient, sacrifices one of libcurl's most powerful features: efficiently performing many transfers simultaneously. An instance of cURLClient can only handle one transfer at a time and doesn't share connections or caches with other instances. For this reason, it is recommended that you forgo using the cURLClient class if you need to accommodate large numbers of transfers.

This example downloads one file each over HTTP, HTTPS, and FTP. Notice how the MultiHandle class is used to manage the individual transfers (represented by the EasyHandle class):

  Dim transfer1 As New libcURL.EasyHandle
  Dim transfer2 As New libcURL.EasyHandle
  Dim transfer3 As New libcURL.EasyHandle
  
  ' set up first transfer
  transfer1.URL = "http://www.example.com/file1.zip"
  Dim t1 As BinaryStream = BinaryStream.Create(SpecialFolder.Desktop.Child("file1.zip"), False)
  transfer1.DownloadStream = t1
  ' second
  transfer2.URL = "ftp://ftp.example.net/file2.zip"
  Dim t2 As BinaryStream = BinaryStream.Create(SpecialFolder.Desktop.Child("file2.zip"), False)
  transfer2.DownloadStream = t2
  ' third
  transfer3.URL = "https://secure.example.com/file3.zip"
  Dim t3 As BinaryStream = BinaryStream.Create(SpecialFolder.Desktop.Child("file3.zip"), False)
  transfer3.DownloadStream = t3
  
  ' add the transfers to a MultiHandle
  Dim m As New libcURL.MultiHandle
  m.AddTransfer(transfer1)
  m.AddTransfer(transfer2)
  m.AddTransfer(transfer3)
  
  ' perform the transfers
  Do Until Not m.PerformOnce()
    App.YieldToNextThread
  Loop
  ' clean up
  t1.Close
  t2.Close
  t3.Close

The MultiHandle class can handle any number of instances of EasyHandle (or its subclasses), and using any mix of protocols, hosts, and options. The MultiHandle class also supports pipelining multiple HTTP requests to the same host over one connection.

Clone this wiki locally