1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * An example of curl_easy_send() and curl_easy_recv() usage.
10 * $Id: sendrecv.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
15 #include <curl/curl.h>
17 /* Auxiliary function that waits on the socket. */
18 static int wait_on_socket(int sockfd
, int for_recv
, long timeout_ms
)
21 fd_set infd
, outfd
, errfd
;
24 tv
.tv_sec
= timeout_ms
/ 1000;
25 tv
.tv_usec
= (timeout_ms
% 1000) * 1000;
31 FD_SET(sockfd
, &errfd
); /* always check for error */
35 FD_SET(sockfd
, &infd
);
39 FD_SET(sockfd
, &outfd
);
42 /* select() returns the number of signalled sockets or -1 */
43 res
= select(sockfd
+ 1, &infd
, &outfd
, &errfd
, &tv
);
51 /* Minimalistic http request */
52 const char *request
= "GET / HTTP/1.0\r\nHost: curl.haxx.se\r\n\r\n";
53 int sockfd
; /* socket */
56 curl
= curl_easy_init();
58 curl_easy_setopt(curl
, CURLOPT_URL
, "curl.haxx.se");
59 /* Do not do the transfer - only connect to host */
60 curl_easy_setopt(curl
, CURLOPT_CONNECT_ONLY
, 1L);
61 res
= curl_easy_perform(curl
);
65 printf("Error: %s\n", strerror(res
));
69 /* Extract the socket from the curl handle - we'll need it
71 res
= curl_easy_getinfo(curl
, CURLINFO_LASTSOCKET
, &sockfd
);
75 printf("Error: %s\n", strerror(res
));
79 /* wait for the socket to become ready for sending */
80 if(!wait_on_socket(sockfd
, 0, 60000L))
82 printf("Error: timeout.\n");
86 puts("Sending request.");
87 /* Send the request. Real applications should check the iolen
88 * to see if all the request has been sent */
89 res
= curl_easy_send(curl
, request
, strlen(request
), &iolen
);
93 printf("Error: %s\n", strerror(res
));
96 puts("Reading response.");
98 /* read the response */
103 wait_on_socket(sockfd
, 1, 60000L);
104 res
= curl_easy_recv(curl
, buf
, 1024, &iolen
);
109 printf("Received %u bytes.\n", iolen
);
113 curl_easy_cleanup(curl
);