Git init
[external/curl.git] / docs / examples / sendrecv.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * An example of curl_easy_send() and curl_easy_recv() usage.
9  *
10  */
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <curl/curl.h>
15
16 /* Auxiliary function that waits on the socket. */
17 static int wait_on_socket(int sockfd, int for_recv, long timeout_ms)
18 {
19   struct timeval tv;
20   fd_set infd, outfd, errfd;
21   int res;
22
23   tv.tv_sec = timeout_ms / 1000;
24   tv.tv_usec= (timeout_ms % 1000) * 1000;
25
26   FD_ZERO(&infd);
27   FD_ZERO(&outfd);
28   FD_ZERO(&errfd);
29
30   FD_SET(sockfd, &errfd); /* always check for error */
31
32   if(for_recv)
33   {
34     FD_SET(sockfd, &infd);
35   }
36   else
37   {
38     FD_SET(sockfd, &outfd);
39   }
40
41   /* select() returns the number of signalled sockets or -1 */
42   res = select(sockfd + 1, &infd, &outfd, &errfd, &tv);
43   return res;
44 }
45
46 int main(void)
47 {
48   CURL *curl;
49   CURLcode res;
50   /* Minimalistic http request */
51   const char *request = "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n";
52   int sockfd; /* socket */
53   size_t iolen;
54
55   curl = curl_easy_init();
56   if(curl) {
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);
61
62     if(CURLE_OK != res)
63     {
64       printf("Error: %s\n", strerror(res));
65       return 1;
66     }
67
68     /* Extract the socket from the curl handle - we'll need it
69      * for waiting */
70     res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockfd);
71
72     if(CURLE_OK != res)
73     {
74       printf("Error: %s\n", curl_easy_strerror(res));
75       return 1;
76     }
77
78     /* wait for the socket to become ready for sending */
79     if(!wait_on_socket(sockfd, 0, 60000L))
80     {
81       printf("Error: timeout.\n");
82       return 1;
83     }
84
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);
89
90     if(CURLE_OK != res)
91     {
92       printf("Error: %s\n", curl_easy_strerror(res));
93       return 1;
94     }
95     puts("Reading response.");
96
97     /* read the response */
98     for(;;)
99     {
100       char buf[1024];
101
102       wait_on_socket(sockfd, 1, 60000L);
103       res = curl_easy_recv(curl, buf, 1024, &iolen);
104
105       if(CURLE_OK != res)
106         break;
107
108       printf("Received %u bytes.\n", iolen);
109     }
110
111     /* always cleanup */
112     curl_easy_cleanup(curl);
113   }
114   return 0;
115 }