官方文档请参考这里 http://curl.haxx.se/libcurl/c/curl_easy_setopt.html
刚接触 libcurl的时候, 自然的想到按照常规的思路来连接云端: 先连接Web, 再发送请求, 然后等待接受返回数据.
这样思路的代码如下
点击(此处)折叠或打开
-
/*----------------------------------------------------------------------------------------------------
-
名称: http_cloud_curl_simple.c
-
功能: 利用libcurl的API顺序实现云端通讯, 未利用回调机制
-
-----------------------------------------------------------------------------------------------------*/
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <curl/curl.h>
-
#include <assert.h>
-
-
#include "../http_cloud.h"
-
-
#define DBG printf
-
-
//-----------------------------------------------------------------------------------------
-
static void get_local_time(char *pc_str)
-
{
-
time_t now;
-
struct tm *timenow;
-
-
assert(pc_str != NULL);
-
-
time(&now);
-
timenow = localtime(&now);
-
sprintf(pc_str, "%04d-%02d-%02dT%02d:%02d:%02d", timenow->tm_year+1900, timenow->tm_mon+1, timenow->tm_mday,
-
timenow->tm_hour, timenow->tm_min, timenow->tm_sec);
-
}
-
-
/* Auxiliary function that waits on the socket. */
-
static int wait_on_socket(curl_socket_t sockfd, int for_recv, long timeout_ms)
-
{
-
struct timeval tv;
-
fd_set infd, outfd, errfd;
-
int res;
-
-
tv.tv_sec = timeout_ms / 1000;
-
tv.tv_usec= (timeout_ms % 1000) * 1000;
-
-
FD_ZERO(&infd);
-
FD_ZERO(&outfd);
-
FD_ZERO(&errfd);
-
-
FD_SET(sockfd, &errfd); /* always check for error */
-
-
if(for_recv) {
-
FD_SET(sockfd, &infd);
-
}
-
else {
-
FD_SET(sockfd, &outfd);
-
}
-
-
/* select() returns the number of signalled sockets or -1 */
-
res = select(sockfd + 1, &infd, &outfd, &errfd, &tv);
-
return res;
-
}
-
-
static char connect_cloud(char *pc_ret, const char *host_addr, const int portno, const char *request)
-
{
-
CURL *curl;
-
CURLcode res;
-
/* Minimalistic http request */
-
//const char *request = "GET / HTTP/1.0
Host: example.com
";
-
//char request[1024] = "";
-
curl_socket_t sockfd; /* socket */
-
long sockextr;
-
size_t iolen;
-
curl_off_t nread;
-
int iLen = 0;
-
char cRet = 0;
-
-
assert((pc_ret != NULL) && (host_addr != NULL) && (request != NULL));
-
-
//curl_global_init(CURL_GLOBAL_DEFAULT);
-
curl = curl_easy_init();
-
if (!curl) return 0;
-
-
//curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
-
curl_easy_setopt(curl, CURLOPT_URL, host_addr);
-
/* Do not do the transfer - only connect to host */
-
curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
-
res = curl_easy_perform(curl);
-
if(CURLE_OK != res) {
-
printf("Error: %s
", strerror(res));
-
return -1;
-
}
-
-
/* Extract the socket from the curl handle - we'll need it for waiting.
-
* Note that this API takes a pointer to a 'long' while we use
-
* curl_socket_t for sockets otherwise.
-
*/
-
res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockextr);
-
if(CURLE_OK != res) {
-
printf("Error: %s
", curl_easy_strerror(res));
-
return -2;
-
}
-
-
sockfd = sockextr;
-
/* wait for the socket to become ready for sending */
-
if(!wait_on_socket(sockfd, 0, 60000L)) {
-
printf("Error: timeout.
");
-
return -3;
-
}
-
-
/* Send the request. Real applications should check the iolen
-
* to see if all the request has been sent */
-
res = curl_easy_send(curl, request, strlen(request), &iolen);
-
if(CURLE_OK != res) {
-
printf("Error: %s
", curl_easy_strerror(res));
-
return -4;
-
}
-
-
/* read the response */
-
char buf[1024];
-
while(1) {
-
wait_on_socket(sockfd, 1, 60000L);
-
res = curl_easy_recv(curl, buf, 1024, &iolen);
-
-
if(CURLE_OK != res)
-
break;
-
-
nread = (curl_off_t)iolen;
-
}
- buf[nread] = '