2
0

sendrecv.c 4.8 KB

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