2
0

sendrecv.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. tv.tv_sec = timeout_ms / 1000;
  38. tv.tv_usec = (int)(timeout_ms % 1000) * 1000;
  39. FD_ZERO(&infd);
  40. FD_ZERO(&outfd);
  41. FD_ZERO(&errfd);
  42. /* Avoid this warning with pre-2020 Cygwin/MSYS releases:
  43. * warning: conversion to 'long unsigned int' from 'curl_socket_t' {aka 'int'}
  44. * may change the sign of the result [-Wsign-conversion]
  45. */
  46. #if defined(__GNUC__)
  47. #pragma GCC diagnostic push
  48. #pragma GCC diagnostic ignored "-Wsign-conversion"
  49. #elif defined(_MSC_VER)
  50. #pragma warning(push)
  51. #pragma warning(disable:4127) /* conditional expression is constant */
  52. #endif
  53. FD_SET(sockfd, &errfd); /* always check for error */
  54. if(for_recv) {
  55. FD_SET(sockfd, &infd);
  56. }
  57. else {
  58. FD_SET(sockfd, &outfd);
  59. }
  60. #if defined(__GNUC__)
  61. #pragma GCC diagnostic pop
  62. #elif defined(_MSC_VER)
  63. #pragma warning(pop)
  64. #endif
  65. /* select() returns the number of signalled sockets or -1 */
  66. res = select((int)sockfd + 1, &infd, &outfd, &errfd, &tv);
  67. return res;
  68. }
  69. int main(void)
  70. {
  71. CURL *curl;
  72. /* Minimalistic http request */
  73. const char *request = "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n";
  74. size_t request_len = strlen(request);
  75. /* A general note of caution here: if you are using curl_easy_recv() or
  76. curl_easy_send() to implement HTTP or _any_ other protocol libcurl
  77. supports "natively", you are doing it wrong and you should stop.
  78. This example uses HTTP only to show how to use this API, it does not
  79. suggest that writing an application doing this is sensible.
  80. */
  81. curl = curl_easy_init();
  82. if(curl) {
  83. CURLcode res;
  84. curl_socket_t sockfd;
  85. size_t nsent_total = 0;
  86. curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
  87. /* Do not do the transfer - only connect to host */
  88. curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
  89. res = curl_easy_perform(curl);
  90. if(res != CURLE_OK) {
  91. printf("Error: %s\n", curl_easy_strerror(res));
  92. return 1;
  93. }
  94. /* Extract the socket from the curl handle - we need it for waiting. */
  95. res = curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sockfd);
  96. if(res != CURLE_OK) {
  97. printf("Error: %s\n", curl_easy_strerror(res));
  98. return 1;
  99. }
  100. printf("Sending request.\n");
  101. do {
  102. /* Warning: This example program may loop indefinitely.
  103. * A production-quality program must define a timeout and exit this loop
  104. * as soon as the timeout has expired. */
  105. size_t nsent;
  106. do {
  107. nsent = 0;
  108. res = curl_easy_send(curl, request + nsent_total,
  109. request_len - nsent_total, &nsent);
  110. nsent_total += nsent;
  111. if(res == CURLE_AGAIN && !wait_on_socket(sockfd, 0, 60000L)) {
  112. printf("Error: timeout.\n");
  113. return 1;
  114. }
  115. } while(res == CURLE_AGAIN);
  116. if(res != CURLE_OK) {
  117. printf("Error: %s\n", curl_easy_strerror(res));
  118. return 1;
  119. }
  120. printf("Sent %lu bytes.\n", (unsigned long)nsent);
  121. } while(nsent_total < request_len);
  122. printf("Reading response.\n");
  123. for(;;) {
  124. /* Warning: This example program may loop indefinitely (see above). */
  125. char buf[1024];
  126. size_t nread;
  127. do {
  128. nread = 0;
  129. res = curl_easy_recv(curl, buf, sizeof(buf), &nread);
  130. if(res == CURLE_AGAIN && !wait_on_socket(sockfd, 1, 60000L)) {
  131. printf("Error: timeout.\n");
  132. return 1;
  133. }
  134. } while(res == CURLE_AGAIN);
  135. if(res != CURLE_OK) {
  136. printf("Error: %s\n", curl_easy_strerror(res));
  137. break;
  138. }
  139. if(nread == 0) {
  140. /* end of the response */
  141. break;
  142. }
  143. printf("Received %lu bytes.\n", (unsigned long)nread);
  144. }
  145. /* always cleanup */
  146. curl_easy_cleanup(curl);
  147. }
  148. return 0;
  149. }