1---
2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3SPDX-License-Identifier: curl
4Title: CURLINFO_ACTIVESOCKET
5Section: 3
6Source: libcurl
7See-also:
8  - CURLINFO_LASTSOCKET (3)
9  - CURLOPT_CONNECT_ONLY (3)
10  - curl_easy_getinfo (3)
11  - curl_easy_setopt (3)
12Protocol:
13  - All
14Added-in: 7.45.0
15---
16
17# NAME
18
19CURLINFO_ACTIVESOCKET - get the active socket
20
21# SYNOPSIS
22
23~~~c
24#include <curl/curl.h>
25
26CURLcode curl_easy_getinfo(CURL *handle, CURLINFO_ACTIVESOCKET,
27                           curl_socket_t *socket);
28~~~
29
30# DESCRIPTION
31
32Pass a pointer to a curl_socket_t to receive the most recently active socket
33used for the transfer connection by this curl session. If the socket is no
34longer valid, *CURL_SOCKET_BAD* is returned. When you are finished working
35with the socket, you must call curl_easy_cleanup(3) as usual on the easy
36handle and let libcurl close the socket and cleanup other resources associated
37with the handle. This option returns the active socket only after the transfer
38is complete, and is typically used in combination with
39CURLOPT_CONNECT_ONLY(3), which skips the transfer phase.
40
41CURLINFO_ACTIVESOCKET(3) was added as a replacement for
42CURLINFO_LASTSOCKET(3) since that one is not working on all platforms.
43
44# %PROTOCOLS%
45
46# EXAMPLE
47
48~~~c
49int main(void)
50{
51  CURL *curl = curl_easy_init();
52  if(curl) {
53    CURLcode res;
54    curl_socket_t sockfd;
55    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
56
57    /* Do not do the transfer - only connect to host */
58    curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
59    res = curl_easy_perform(curl);
60    if(res != CURLE_OK) {
61      printf("Error: %s\n", curl_easy_strerror(res));
62      curl_easy_cleanup(curl);
63      return 1;
64    }
65
66    /* Extract the socket from the curl handle */
67    res = curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sockfd);
68    if(!res && sockfd != CURL_SOCKET_BAD) {
69      /* operate on sockfd */
70    }
71
72    curl_easy_cleanup(curl);
73  }
74}
75~~~
76
77# %AVAILABILITY%
78
79# RETURN VALUE
80
81Returns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION if not.
82