sendrecv.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /* <DESC>
  25. * Demonstrate curl_easy_send() and curl_easy_recv() usage.
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <curl/curl.h>
  31. /* Auxiliary function that waits on the socket. */
  32. static int wait_on_socket(curl_socket_t sockfd, int for_recv, long timeout_ms)
  33. {
  34. struct timeval tv;
  35. fd_set infd, outfd, errfd;
  36. int res;
  37. #if defined(MSDOS) || defined(__AMIGA__)
  38. tv.tv_sec = (time_t)(timeout_ms / 1000);
  39. tv.tv_usec = (time_t)(timeout_ms % 1000) * 1000;
  40. #else
  41. tv.tv_sec = timeout_ms / 1000;
  42. tv.tv_usec = (int)(timeout_ms % 1000) * 1000;
  43. #endif
  44. FD_ZERO(&infd);
  45. FD_ZERO(&outfd);
  46. FD_ZERO(&errfd);
  47. /* Avoid this warning with pre-2020 Cygwin/MSYS releases:
  48. * warning: conversion to 'long unsigned int' from 'curl_socket_t' {aka 'int'}
  49. * may change the sign of the result [-Wsign-conversion]
  50. */
  51. #if defined(__GNUC__)
  52. #pragma GCC diagnostic push
  53. #pragma GCC diagnostic ignored "-Wsign-conversion"
  54. #if defined(__DJGPP__)
  55. #pragma GCC diagnostic ignored "-Warith-conversion"
  56. #endif
  57. #elif defined(_MSC_VER)
  58. #pragma warning(push)
  59. #pragma warning(disable:4127) /* conditional expression is constant */
  60. #endif
  61. FD_SET(sockfd, &errfd); /* always check for error */
  62. if(for_recv) {
  63. FD_SET(sockfd, &infd);
  64. }
  65. else {
  66. FD_SET(sockfd, &outfd);
  67. }
  68. #if defined(__GNUC__)
  69. #pragma GCC diagnostic pop
  70. #elif defined(_MSC_VER)
  71. #pragma warning(pop)
  72. #endif
  73. /* select() returns the number of signalled sockets or -1 */
  74. res = select((int)sockfd + 1, &infd, &outfd, &errfd, &tv);
  75. return res;
  76. }
  77. int main(void)
  78. {
  79. CURL *curl;
  80. /* Minimalistic http request */
  81. const char *request = "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n";
  82. size_t request_len = strlen(request);
  83. /* A general note of caution here: if you are using curl_easy_recv() or
  84. curl_easy_send() to implement HTTP or _any_ other protocol libcurl
  85. supports "natively", you are doing it wrong and you should stop.
  86. This example uses HTTP only to show how to use this API, it does not
  87. suggest that writing an application doing this is sensible.
  88. */
  89. curl = curl_easy_init();
  90. if(curl) {
  91. CURLcode res;
  92. curl_socket_t sockfd;
  93. size_t nsent_total = 0;
  94. curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
  95. /* Do not do the transfer - only connect to host */
  96. curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
  97. res = curl_easy_perform(curl);
  98. if(res != CURLE_OK) {
  99. printf("Error: %s\n", curl_easy_strerror(res));
  100. return 1;
  101. }
  102. /* Extract the socket from the curl handle - we need it for waiting. */
  103. res = curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sockfd);
  104. if(res != CURLE_OK) {
  105. printf("Error: %s\n", curl_easy_strerror(res));
  106. return 1;
  107. }
  108. printf("Sending request.\n");
  109. do {
  110. /* Warning: This example program may loop indefinitely.
  111. * A production-quality program must define a timeout and exit this loop
  112. * as soon as the timeout has expired. */
  113. size_t nsent;
  114. do {
  115. nsent = 0;
  116. res = curl_easy_send(curl, request + nsent_total,
  117. request_len - nsent_total, &nsent);
  118. nsent_total += nsent;
  119. if(res == CURLE_AGAIN && !wait_on_socket(sockfd, 0, 60000L)) {
  120. printf("Error: timeout.\n");
  121. return 1;
  122. }
  123. } while(res == CURLE_AGAIN);
  124. if(res != CURLE_OK) {
  125. printf("Error: %s\n", curl_easy_strerror(res));
  126. return 1;
  127. }
  128. printf("Sent %lu bytes.\n", (unsigned long)nsent);
  129. } while(nsent_total < request_len);
  130. printf("Reading response.\n");
  131. for(;;) {
  132. /* Warning: This example program may loop indefinitely (see above). */
  133. char buf[1024];
  134. size_t nread;
  135. do {
  136. nread = 0;
  137. res = curl_easy_recv(curl, buf, sizeof(buf), &nread);
  138. if(res == CURLE_AGAIN && !wait_on_socket(sockfd, 1, 60000L)) {
  139. printf("Error: timeout.\n");
  140. return 1;
  141. }
  142. } while(res == CURLE_AGAIN);
  143. if(res != CURLE_OK) {
  144. printf("Error: %s\n", curl_easy_strerror(res));
  145. break;
  146. }
  147. if(nread == 0) {
  148. /* end of the response */
  149. break;
  150. }
  151. printf("Received %lu bytes.\n", (unsigned long)nread);
  152. }
  153. /* always cleanup */
  154. curl_easy_cleanup(curl);
  155. }
  156. return 0;
  157. }