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