CURLINFO_ACTIVESOCKET.3 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. .\" generated by cd2nroff 0.1 from CURLINFO_ACTIVESOCKET.md
  2. .TH CURLINFO_ACTIVESOCKET 3 "2025-01-17" libcurl
  3. .SH NAME
  4. CURLINFO_ACTIVESOCKET \- get the active socket
  5. .SH SYNOPSIS
  6. .nf
  7. #include <curl/curl.h>
  8. CURLcode curl_easy_getinfo(CURL *handle, CURLINFO_ACTIVESOCKET,
  9. curl_socket_t *socket);
  10. .fi
  11. .SH DESCRIPTION
  12. Pass a pointer to a curl_socket_t to receive the most recently active socket
  13. used for the transfer connection by this curl session. If the socket is no
  14. longer valid, \fICURL_SOCKET_BAD\fP is returned. When you are finished working
  15. with the socket, you must call \fIcurl_easy_cleanup(3)\fP as usual on the easy
  16. handle and let libcurl close the socket and cleanup other resources associated
  17. with the handle. This option returns the active socket only after the transfer
  18. is complete, and is typically used in combination with
  19. \fICURLOPT_CONNECT_ONLY(3)\fP, which skips the transfer phase.
  20. \fICURLINFO_ACTIVESOCKET(3)\fP was added as a replacement for
  21. \fICURLINFO_LASTSOCKET(3)\fP since that one is not working on all platforms.
  22. .SH PROTOCOLS
  23. This functionality affects all supported protocols
  24. .SH EXAMPLE
  25. .nf
  26. int main(void)
  27. {
  28. CURL *curl = curl_easy_init();
  29. if(curl) {
  30. CURLcode res;
  31. curl_socket_t sockfd;
  32. curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
  33. /* Do not do the transfer - only connect to host */
  34. curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
  35. res = curl_easy_perform(curl);
  36. if(res != CURLE_OK) {
  37. printf("Error: %s\\n", curl_easy_strerror(res));
  38. curl_easy_cleanup(curl);
  39. return 1;
  40. }
  41. /* Extract the socket from the curl handle */
  42. res = curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sockfd);
  43. if(!res && sockfd != CURL_SOCKET_BAD) {
  44. /* operate on sockfd */
  45. }
  46. curl_easy_cleanup(curl);
  47. }
  48. }
  49. .fi
  50. .SH AVAILABILITY
  51. Added in curl 7.45.0
  52. .SH RETURN VALUE
  53. Returns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION if not.
  54. .SH SEE ALSO
  55. .BR CURLINFO_LASTSOCKET (3),
  56. .BR CURLOPT_CONNECT_ONLY (3),
  57. .BR curl_easy_getinfo (3),
  58. .BR curl_easy_setopt (3)