sendrecv.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, 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.haxx.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. CURLcode res;
  55. /* Minimalistic http request */
  56. const char *request = "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n";
  57. size_t request_len = strlen(request);
  58. curl_socket_t sockfd;
  59. size_t nsent_total = 0;
  60. /* A general note of caution here: if you're using curl_easy_recv() or
  61. curl_easy_send() to implement HTTP or _any_ other protocol libcurl
  62. supports "natively", you're doing it wrong and you should stop.
  63. This example uses HTTP only to show how to use this API, it does not
  64. suggest that writing an application doing this is sensible.
  65. */
  66. curl = curl_easy_init();
  67. if(curl) {
  68. curl_easy_setopt(curl, CURLOPT_URL, "http://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'll need it for waiting. */
  77. res = curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sockfd);
  78. if(res != CURLE_OK) {
  79. printf("Error: %s\n", curl_easy_strerror(res));
  80. return 1;
  81. }
  82. printf("Sending request.\n");
  83. do {
  84. /* Warning: This example program may loop indefinitely.
  85. * A production-quality program must define a timeout and exit this loop
  86. * as soon as the timeout has expired. */
  87. size_t nsent;
  88. do {
  89. nsent = 0;
  90. res = curl_easy_send(curl, request + nsent_total,
  91. request_len - nsent_total, &nsent);
  92. nsent_total += nsent;
  93. if(res == CURLE_AGAIN && !wait_on_socket(sockfd, 0, 60000L)) {
  94. printf("Error: timeout.\n");
  95. return 1;
  96. }
  97. } while(res == CURLE_AGAIN);
  98. if(res != CURLE_OK) {
  99. printf("Error: %s\n", curl_easy_strerror(res));
  100. return 1;
  101. }
  102. printf("Sent %" CURL_FORMAT_CURL_OFF_T " bytes.\n",
  103. (curl_off_t)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 %" CURL_FORMAT_CURL_OFF_T " bytes.\n",
  127. (curl_off_t)nread);
  128. }
  129. /* always cleanup */
  130. curl_easy_cleanup(curl);
  131. }
  132. return 0;
  133. }