sendrecv.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * An example of curl_easy_send() and curl_easy_recv() usage.
  9. *
  10. * $Id$
  11. */
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <curl/curl.h>
  15. /* Auxiliary function that waits on the socket. */
  16. static int wait_on_socket(int sockfd, int for_recv, long timeout_ms)
  17. {
  18. struct timeval tv;
  19. fd_set infd, outfd, errfd;
  20. int res;
  21. tv.tv_sec = timeout_ms / 1000;
  22. tv.tv_usec= (timeout_ms % 1000) * 1000;
  23. FD_ZERO(&infd);
  24. FD_ZERO(&outfd);
  25. FD_ZERO(&errfd);
  26. FD_SET(sockfd, &errfd); /* always check for error */
  27. if(for_recv)
  28. {
  29. FD_SET(sockfd, &infd);
  30. }
  31. else
  32. {
  33. FD_SET(sockfd, &outfd);
  34. }
  35. /* select() returns the number of signalled sockets or -1 */
  36. res = select(sockfd + 1, &infd, &outfd, &errfd, &tv);
  37. return res;
  38. }
  39. int main(void)
  40. {
  41. CURL *curl;
  42. CURLcode res;
  43. /* Minimalistic http request */
  44. const char *request = "GET / HTTP/1.0\r\nHost: curl.haxx.se\r\n\r\n";
  45. int sockfd; /* socket */
  46. size_t iolen;
  47. curl = curl_easy_init();
  48. if(curl) {
  49. curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se");
  50. /* Do not do the transfer - only connect to host */
  51. curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
  52. res = curl_easy_perform(curl);
  53. if(CURLE_OK != res)
  54. {
  55. printf("Error: %s\n", strerror(res));
  56. return 1;
  57. }
  58. /* Extract the socket from the curl handle - we'll need it
  59. * for waiting */
  60. res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockfd);
  61. if(CURLE_OK != res)
  62. {
  63. printf("Error: %s\n", strerror(res));
  64. return 1;
  65. }
  66. /* wait for the socket to become ready for sending */
  67. if(!wait_on_socket(sockfd, 0, 60000L))
  68. {
  69. printf("Error: timeout.\n");
  70. return 1;
  71. }
  72. puts("Sending request.");
  73. /* Send the request. Real applications should check the iolen
  74. * to see if all the request has been sent */
  75. res = curl_easy_send(curl, request, strlen(request), &iolen);
  76. if(CURLE_OK != res)
  77. {
  78. printf("Error: %s\n", strerror(res));
  79. return 1;
  80. }
  81. puts("Reading response.");
  82. /* read the response */
  83. for(;;)
  84. {
  85. char buf[1024];
  86. wait_on_socket(sockfd, 1, 60000L);
  87. res = curl_easy_recv(curl, buf, 1024, &iolen);
  88. if(CURLE_OK != res)
  89. break;
  90. printf("Received %u bytes.\n", iolen);
  91. }
  92. /* always cleanup */
  93. curl_easy_cleanup(curl);
  94. }
  95. return 0;
  96. }