sources: update source headers
[platform/upstream/curl.git] / docs / examples / sendrecv.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2011, 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
68   curl = curl_easy_init();
69   if(curl) {
70     curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
71     /* Do not do the transfer - only connect to host */
72     curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
73     res = curl_easy_perform(curl);
74
75     if(CURLE_OK != res)
76     {
77       printf("Error: %s\n", strerror(res));
78       return 1;
79     }
80
81     /* Extract the socket from the curl handle - we'll need it for waiting.
82      * Note that this API takes a pointer to a 'long' while we use
83      * curl_socket_t for sockets otherwise.
84      */
85     res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockextr);
86
87     if(CURLE_OK != res)
88     {
89       printf("Error: %s\n", curl_easy_strerror(res));
90       return 1;
91     }
92
93     sockfd = sockextr;
94
95     /* wait for the socket to become ready for sending */
96     if(!wait_on_socket(sockfd, 0, 60000L))
97     {
98       printf("Error: timeout.\n");
99       return 1;
100     }
101
102     puts("Sending request.");
103     /* Send the request. Real applications should check the iolen
104      * to see if all the request has been sent */
105     res = curl_easy_send(curl, request, strlen(request), &iolen);
106
107     if(CURLE_OK != res)
108     {
109       printf("Error: %s\n", curl_easy_strerror(res));
110       return 1;
111     }
112     puts("Reading response.");
113
114     /* read the response */
115     for(;;)
116     {
117       char buf[1024];
118
119       wait_on_socket(sockfd, 1, 60000L);
120       res = curl_easy_recv(curl, buf, 1024, &iolen);
121
122       if(CURLE_OK != res)
123         break;
124
125       printf("Received %u bytes.\n", iolen);
126     }
127
128     /* always cleanup */
129     curl_easy_cleanup(curl);
130   }
131   return 0;
132 }