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