sendrecv.c 4.5 KB

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