sendrecv.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2012, 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 http://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. /* An example of curl_easy_send() and curl_easy_recv() usage. */
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <curl/curl.h>
  26. /* Auxiliary function that waits on the socket. */
  27. static int wait_on_socket(curl_socket_t sockfd, int for_recv, long timeout_ms)
  28. {
  29. struct timeval tv;
  30. fd_set infd, outfd, errfd;
  31. int res;
  32. tv.tv_sec = timeout_ms / 1000;
  33. tv.tv_usec= (timeout_ms % 1000) * 1000;
  34. FD_ZERO(&infd);
  35. FD_ZERO(&outfd);
  36. FD_ZERO(&errfd);
  37. FD_SET(sockfd, &errfd); /* always check for error */
  38. if(for_recv)
  39. {
  40. FD_SET(sockfd, &infd);
  41. }
  42. else
  43. {
  44. FD_SET(sockfd, &outfd);
  45. }
  46. /* select() returns the number of signalled sockets or -1 */
  47. res = select(sockfd + 1, &infd, &outfd, &errfd, &tv);
  48. return res;
  49. }
  50. int main(void)
  51. {
  52. CURL *curl;
  53. CURLcode res;
  54. /* Minimalistic http request */
  55. const char *request = "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n";
  56. curl_socket_t sockfd; /* socket */
  57. long sockextr;
  58. size_t iolen;
  59. curl_off_t nread;
  60. curl = curl_easy_init();
  61. if(curl) {
  62. curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
  63. /* Do not do the transfer - only connect to host */
  64. curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
  65. res = curl_easy_perform(curl);
  66. if(CURLE_OK != res)
  67. {
  68. printf("Error: %s\n", strerror(res));
  69. return 1;
  70. }
  71. /* Extract the socket from the curl handle - we'll need it for waiting.
  72. * Note that this API takes a pointer to a 'long' while we use
  73. * curl_socket_t for sockets otherwise.
  74. */
  75. res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockextr);
  76. if(CURLE_OK != res)
  77. {
  78. printf("Error: %s\n", curl_easy_strerror(res));
  79. return 1;
  80. }
  81. sockfd = sockextr;
  82. /* wait for the socket to become ready for sending */
  83. if(!wait_on_socket(sockfd, 0, 60000L))
  84. {
  85. printf("Error: timeout.\n");
  86. return 1;
  87. }
  88. puts("Sending request.");
  89. /* Send the request. Real applications should check the iolen
  90. * to see if all the request has been sent */
  91. res = curl_easy_send(curl, request, strlen(request), &iolen);
  92. if(CURLE_OK != res)
  93. {
  94. printf("Error: %s\n", curl_easy_strerror(res));
  95. return 1;
  96. }
  97. puts("Reading response.");
  98. /* read the response */
  99. for(;;)
  100. {
  101. char buf[1024];
  102. wait_on_socket(sockfd, 1, 60000L);
  103. res = curl_easy_recv(curl, buf, 1024, &iolen);
  104. if(CURLE_OK != res)
  105. break;
  106. nread = (curl_off_t)iolen;
  107. printf("Received %" CURL_FORMAT_CURL_OFF_T " bytes.\n", nread);
  108. }
  109. /* always cleanup */
  110. curl_easy_cleanup(curl);
  111. }
  112. return 0;
  113. }