xref: /curl/docs/libcurl/curl_easy_send.md (revision e3fe0200)
1---
2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3SPDX-License-Identifier: curl
4Title: curl_easy_send
5Section: 3
6Source: libcurl
7See-also:
8  - curl_easy_getinfo (3)
9  - curl_easy_perform (3)
10  - curl_easy_recv (3)
11  - curl_easy_setopt (3)
12Protocol:
13  - All
14---
15
16# NAME
17
18curl_easy_send - sends raw data over an "easy" connection
19
20# SYNOPSIS
21
22~~~c
23#include <curl/curl.h>
24
25CURLcode curl_easy_send(CURL *curl, const void *buffer,
26                        size_t buflen, size_t *n);
27~~~
28
29# DESCRIPTION
30
31This function sends arbitrary data over the established connection. You may
32use it together with curl_easy_recv(3) to implement custom protocols
33using libcurl. This functionality can be particularly useful if you use
34proxies and/or SSL encryption: libcurl takes care of proxy negotiation and
35connection setup.
36
37**buffer** is a pointer to the data of length **buflen** that you want
38sent. The variable **n** points to receives the number of sent bytes.
39
40To establish the connection, set CURLOPT_CONNECT_ONLY(3) option before
41calling curl_easy_perform(3) or curl_multi_perform(3). Note that
42curl_easy_send(3) does not work on connections that were created without
43this option.
44
45The call returns **CURLE_AGAIN** if it is not possible to send data right now
46- the socket is used in non-blocking mode internally. When **CURLE_AGAIN**
47is returned, use your operating system facilities like *select(2)* to wait
48until the socket is writable. The socket may be obtained using
49curl_easy_getinfo(3) with CURLINFO_ACTIVESOCKET(3).
50
51Furthermore if you wait on the socket and it tells you it is writable,
52curl_easy_send(3) may return **CURLE_AGAIN** if the only data that was sent
53was for internal SSL processing, and no other data could be sent.
54
55# EXAMPLE
56
57~~~c
58int main(void)
59{
60  CURL *curl = curl_easy_init();
61  if(curl) {
62    CURLcode res;
63    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
64    /* Do not do the transfer - only connect to host */
65    curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
66    res = curl_easy_perform(curl);
67
68    if(res == CURLE_OK) {
69      long sockfd;
70      size_t sent;
71      /* Extract the socket from the curl handle - we need it for waiting. */
72      res = curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sockfd);
73
74      /* send data */
75      res = curl_easy_send(curl, "hello", 5, &sent);
76    }
77  }
78}
79~~~
80
81# AVAILABILITY
82
83Added in 7.18.2.
84
85# RETURN VALUE
86
87On success, returns **CURLE_OK** and stores the number of bytes actually
88sent into ***n**. Note that this may be less than the amount you wanted to
89send.
90
91On failure, returns the appropriate error code.
92
93This function may return **CURLE_AGAIN**. In this case, use your operating
94system facilities to wait until the socket is writable, and retry.
95
96If there is no socket available to use from the previous transfer, this function
97returns **CURLE_UNSUPPORTED_PROTOCOL**.
98