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