1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * An example of curl_easy_send() and curl_easy_recv() usage.
14 #include <curl/curl.h>
16 /* Auxiliary function that waits on the socket. */
17 static int wait_on_socket(int sockfd, int for_recv, long timeout_ms)
20 fd_set infd, outfd, errfd;
23 tv.tv_sec = timeout_ms / 1000;
24 tv.tv_usec= (timeout_ms % 1000) * 1000;
30 FD_SET(sockfd, &errfd); /* always check for error */
34 FD_SET(sockfd, &infd);
38 FD_SET(sockfd, &outfd);
41 /* select() returns the number of signalled sockets or -1 */
42 res = select(sockfd + 1, &infd, &outfd, &errfd, &tv);
50 /* Minimalistic http request */
51 const char *request = "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n";
52 int sockfd; /* socket */
55 curl = curl_easy_init();
57 curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
58 /* Do not do the transfer - only connect to host */
59 curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
60 res = curl_easy_perform(curl);
64 printf("Error: %s\n", strerror(res));
68 /* Extract the socket from the curl handle - we'll need it
70 res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockfd);
74 printf("Error: %s\n", curl_easy_strerror(res));
78 /* wait for the socket to become ready for sending */
79 if(!wait_on_socket(sockfd, 0, 60000L))
81 printf("Error: timeout.\n");
85 puts("Sending request.");
86 /* Send the request. Real applications should check the iolen
87 * to see if all the request has been sent */
88 res = curl_easy_send(curl, request, strlen(request), &iolen);
92 printf("Error: %s\n", curl_easy_strerror(res));
95 puts("Reading response.");
97 /* read the response */
102 wait_on_socket(sockfd, 1, 60000L);
103 res = curl_easy_recv(curl, buf, 1024, &iolen);
108 printf("Received %u bytes.\n", iolen);
112 curl_easy_cleanup(curl);