sendrecv.c 4.5 KB

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