[英]How to check network connection with cURL
我有一個基於Linux的嵌入式系統,並使用cURL庫從IP攝像機拍攝快照。
我的相機提供了一些CGI功能,其中之一是用於拍攝圖像快照。
調用專用的CGI功能后,攝像機會成功發送拍攝的圖片作為響應。
我將相機響應另存為jpg文件,到目前為止一切正常。
但是,當網絡連接斷開時,我的代碼將生成一個零尺寸的jpg文件。
我刪除了零尺寸的圖像,但是最好不要保存圖像,以防curl_easy_perform(curl)調用返回錯誤。
如果cURL錯誤,是否可以禁用保存?
const int maxNumberOfImages = 20;
CURL *curl;
CURLcode result;
FILE *CURLOPT_WRITEFUNCTION_FILE;
bool flag = true;
string url = "http://admin:1234@";//needs refactory
url.append(ip);
url.append(":80/snapshot.jpg");
DEBUG_PRINT(url);
char actionDate[20];
time_t now = time(NULL);
strftime(actionDate, 20, "%Y-%m-%d %H:%M:%S", localtime(&now));
char *directory = "/root/rootfs_frequent_rw/snapshots/";
string fileName = directory;
fileName.append(cam_name);
fileName.append("___");
fileName.append(actionDate);
fileName.append(".jpg");
createDirectoryIfNotExist(directory);
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
CURLOPT_WRITEFUNCTION_FILE = fopen(fileName.c_str(), "wb");
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEDATA, CURLOPT_WRITEFUNCTION_FILE);
result = curl_easy_perform(curl);
if(result != CURLE_OK){
DEBUG_PRINT("ERROR\n");
flag = false;
}
else
DEBUG_PRINT("SUCCESS\n");
curl_easy_cleanup(curl);
}
fclose(CURLOPT_WRITEFUNCTION_FILE);
cx based embedded system and taking snapshots from an ip camera. url_global_cleanup();
isFileEmpty(fileName.c_str()); //if file is zero sized delete it.
if(flag)
return ReturnValue::return_success;
return ReturnValue::return_operation_failed;
我通過使用CURLOPT_WRITEFUNCTION選項解決了這個問題。 從cURL文檔更改示例代碼(getinmemory)后,我得到了以下代碼。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
struct MemoryStruct {
char *memory;
size_t size;
};
static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
mem->memory = realloc(mem->memory, mem->size + realsize + 1);
if(mem->memory == NULL) {
/* out of memory! */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
int main(void)
{
CURL *curl_handle;
CURLcode res;
FILE *pFile;
struct MemoryStruct chunk;
chunk.memory = malloc(1); /* will be grown as needed by the realloc above */
chunk.size = 0; /* no data at this point */
curl_global_init(CURL_GLOBAL_ALL);
/* init the curl session */
curl_handle = curl_easy_init();
/* specify URL to get */
curl_easy_setopt(curl_handle, CURLOPT_URL, "http://admin:1234@10.108.67.235:80/snapshot.jpg");
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
/* some servers don't like requests that are made without a user-agent
field, so we provide one */
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 1L);
/* get it! */
res = curl_easy_perform(curl_handle);
/* check for errors */
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
else {
/*
* Now, our chunk.memory points to a memory block that is chunk.size
* bytes big and contains the remote file.
*
* Do something nice with it!
*/
printf("%lu bytes retrieved\n", (long)chunk.size);
pFile = fopen("test.jpg", "wb");
fwrite(chunk.memory, sizeof(char), (long)chunk.size, pFile);
fclose(pFile);
}
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
free(chunk.memory);
/* we're done with libcurl, so clean it up */
curl_global_cleanup();
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.