1CURLMOPT_SOCKETFUNCTION(3) libcurl CURLMOPT_SOCKETFUNCTION(3)
2
3
4
6 CURLMOPT_SOCKETFUNCTION - callback informed about what to wait for
7
9 #include <curl/curl.h>
10
11 int socket_callback(CURL *easy, /* easy handle */
12 curl_socket_t s, /* socket */
13 int what, /* describes the socket */
14 void *clientp, /* private callback pointer */
15 void *socketp); /* private socket pointer */
16
17 CURLMcode curl_multi_setopt(CURLM *handle, CURLMOPT_SOCKETFUNCTION, socket_callback);
18
20 Pass a pointer to your callback function, which should match the proto‐
21 type shown above.
22
23 When the curl_multi_socket_action(3) function is called, it uses this
24 callback to inform the application about updates in the socket (file
25 descriptor) status by doing none, one, or multiple calls to the
26 socket_callback. The callback function gets status updates with changes
27 since the previous time the callback was called. If the given callback
28 pointer is set to NULL, no callback will be called.
29
30 libcurl then expects the application to monitor the sockets for the
31 specific activities and tell libcurl again when something happens on
32 one of them. Tell libcurl by calling curl_multi_socket_action(3).
33
35 easy identifies the specific transfer for which this update is related.
36
37 s is the specific socket this function invocation concerns. If the what
38 argument is not CURL_POLL_REMOVE then it holds information about what
39 activity on this socket the application is supposed to monitor. Subse‐
40 quent calls to this callback might update the what bits for a socket
41 that is already monitored.
42
43 The socket callback should return 0 on success, and -1 on error. If
44 this callback returns error, all transfers currently in progress in
45 this multi handle will be aborted and fail.
46
47 clientp is set with CURLMOPT_SOCKETDATA(3).
48
49 socketp is set with curl_multi_assign(3) or will be NULL.
50
51 The what parameter informs the callback on the status of the given
52 socket. It can hold one of these values:
53
54 CURL_POLL_IN
55 Wait for incoming data. For the socket to become readable.
56
57 CURL_POLL_OUT
58 Wait for outgoing data. For the socket to become writable.
59
60 CURL_POLL_INOUT
61 Wait for incoming and outgoing data. For the socket to become
62 readable or writable.
63
64 CURL_POLL_REMOVE
65 The specified socket/file descriptor is no longer used by
66 libcurl for any active transfer. It might soon be added again.
67
69 NULL (no callback)
70
72 All
73
75 static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
76 {
77 GlobalInfo *g = cbp;
78 SockInfo *fdp = sockp;
79
80 if(what == CURL_POLL_REMOVE) {
81 remsock(fdp);
82 }
83 else {
84 if(!fdp) {
85 addsock(s, e, what, g);
86 }
87 else {
88 setsock(fdp, s, e, what, g);
89 }
90 }
91 return 0;
92 }
93
94 main()
95 {
96 GlobalInfo setup;
97 /* ... use socket callback and custom pointer */
98 curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
99 curl_multi_setopt(multi, CURLMOPT_SOCKETDATA, &setup);
100 }
101
103 Added in 7.15.4
104
106 Returns CURLM_OK.
107
109 CURLMOPT_SOCKETDATA(3), curl_multi_socket_action(3), CURLMOPT_TIMER‐
110 FUNCTION(3)
111
112
113
114ibcurl 8.2.1 April 26, 2023 CURLMOPT_SOCKETFUNCTION(3)