1libcurl-multi(3) libcurl multi interface libcurl-multi(3)
2
3
4
6 libcurl-multi - how to use the multi interface
7
9 This is an overview on how to use the libcurl multi interface in your C
10 programs. There are specific man pages for each function mentioned in
11 here. There's also the libcurl-tutorial(3) man page for a complete
12 tutorial to programming with libcurl and the libcurl-easy(3) man page
13 for an overview of the libcurl easy interface.
14
15 All functions in the multi interface are prefixed with curl_multi.
16
18 The multi interface offers several abilities that the easy interface
19 doesn't. They are mainly:
20
21 1. Enable a "pull" interface. The application that uses libcurl decides
22 where and when to ask libcurl to get/send data.
23
24 2. Enable multiple simultaneous transfers in the same thread without
25 making it complicated for the application.
26
27 3. Enable the application to wait for action on its own file descrip‐
28 tors and curl's file descriptors simultaneous easily.
29
31 To use the multi interface, you must first create a 'multi handle' with
32 curl_multi_init(3). This handle is then used as input to all further
33 curl_multi_* functions.
34
35 Each single transfer is built up with an easy handle. You must create
36 them, and setup the appropriate options for each easy handle, as out‐
37 lined in the libcurl(3) man page, using curl_easy_setopt(3).
38
39 When the easy handle is setup for a transfer, then instead of using
40 curl_easy_perform(3) (as when using the easy interface for transfers),
41 you should instead add the easy handle to the multi handle using
42 curl_multi_add_handle(3). The multi handle is sometimes referred to as
43 a ´multi stack´ because of the fact that it may hold a large amount of
44 easy handles.
45
46 Should you change your mind, the easy handle is again removed from the
47 multi stack using curl_multi_remove_handle(3). Once removed from the
48 multi handle, you can again use other easy interface functions like
49 curl_easy_perform(3) on the handle or whatever you think is necessary.
50
51 Adding the easy handle to the multi handle does not start the transfer.
52 Remember that one of the main ideas with this interface is to let your
53 application drive. You drive the transfers by invoking curl_multi_per‐
54 form(3). libcurl will then transfer data if there is anything available
55 to transfer. It'll use the callbacks and everything else you have setup
56 in the individual easy handles. It'll transfer data on all current
57 transfers in the multi stack that are ready to transfer anything. It
58 may be all, it may be none.
59
60 Your application can acquire knowledge from libcurl when it would like
61 to get invoked to transfer data, so that you don't have to busy-loop
62 and call that curl_multi_perform(3) like crazy. curl_multi_fdset(3)
63 offers an interface using which you can extract fd_sets from libcurl to
64 use in select() or poll() calls in order to get to know when the trans‐
65 fers in the multi stack might need attention. This also makes it very
66 easy for your program to wait for input on your own private file
67 descriptors at the same time or perhaps timeout every now and then,
68 should you want that.
69
70 curl_multi_perform(3) stores the number of still running transfers in
71 one of its input arguments, and by reading that you can figure out when
72 all the transfers in the multi handles are done. 'done' does not mean
73 successful. One or more of the transfers may have failed. Tracking when
74 this number changes, you know when one or more transfers are done.
75
76 To get information about completed transfers, to figure out success or
77 not and similar, curl_multi_info_read(3) should be called. It can
78 return a message about a current or previous transfer. Repeated invokes
79 of the function get more messages until the message queue is empty. The
80 information you receive there includes an easy handle pointer which you
81 may use to identify which easy handle the information regards.
82
83 When a single transfer is completed, the easy handle is still left
84 added to the multi stack. You need to first remove the easy handle with
85 curl_multi_remove_handle(3) and then close it with
86 curl_easy_cleanup(3), or possibly set new options to it and add it
87 again with curl_multi_add_handle(3) to start another transfer.
88
89 When all transfers in the multi stack are done, cleanup the multi han‐
90 dle with curl_multi_cleanup(3). Be careful and please note that you
91 MUST invoke separate curl_easy_cleanup(3) calls on every single easy
92 handle to clean them up properly.
93
94 If you want to re-use an easy handle that was added to the multi handle
95 for transfer, you must first remove it from the multi stack and then
96 re-add it again (possibly after having altered some options at your own
97 choice).
98
100 curl_multi_socket_action(3) function offers a way for applications to
101 not only avoid being forced to use select(), but it also offers a much
102 more high-performance API that will make a significant difference for
103 applications using large numbers of simultaneous connections.
104
105 curl_multi_socket_action(3) is then used instead of curl_multi_per‐
106 form(3).
107
108 When using this API, you add easy handles to the multi handle just as
109 with the normal multi interface. Then you also set two callbacks with
110 the CURLMOPT_SOCKETFUNCTION and CURLMOPT_TIMERFUNCTION options to
111 curl_multi_setopt(3).
112
113 The API is then designed to inform your application about which sockets
114 libcurl is currently using and for what activities (read and/or write)
115 on those sockets your application is expected to wait for.
116
117 Your application must then make sure to receive all sockets informed
118 about in the CURLMOPT_SOCKETFUNCTION callback and make sure it reacts
119 on the given activity on them. When a socket has the given activity,
120 you call curl_multi_socket_action(3) specifying which socket and action
121 there are.
122
123 The CURLMOPT_TIMERFUNCTION callback is called to set a timeout. When
124 that timeout expires, your application should call the
125 curl_multi_socket_action(3) function saying it was due to a timeout.
126
128 A few areas in the code are still using blocking code, even when used
129 from the multi interface. While we certainly want and intend for these
130 to get fixed in the future, you should be aware of the following cur‐
131 rent restrictions:
132
133 - Name resolves unless the c-ares or threaded-resolver backends are used
134 - NSS SSL connections
135 - HTTP proxy CONNECT operations
136 - SOCKS proxy handshakes
137 - file:// transfers
138 - TELNET transfers
139
140
141
142libcurl 7.16.0 3 Feb 2007 libcurl-multi(3)