curl 设置超时后出错?
使用curl开源库,在ipcam侦测到图像移动时,就推动一条消息和一幅抓怕到服务器上。设置CURLOPT_TIMEOUT超时后,如果拔了网线会导致程序错误退出,不设置超时就不会出错。
是调试信息和代码:
使用CURLOPT_TIMEOUT,并且拔了网线,调试信息:
~~~ in post_data~~
-----0-----
-----1-----
curl_easy_perform() failed!
-----2-----
-----3-----
-----4-----
~~~ out post_data~~
~~~ in post_data~~
-----0-----
-----1-----
curl_easy_perform() failed!
-----2-----
-----3-----
-----4-----
~~~ out post_data~~
Obtained 2 stack frames
./recorder [0xed10] //recorder是主程序
/lib/libc.so.6(__default_sa_restorer_v2+0) [0xb69b2580]
代码:
/********************************
推送功能:
*********************************/
int post_data(char *image_path, char *p_message)
{
printf("~~~ in post_data~~\n");
CURL *curl;
CURLcode res = 0;
struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;
struct curl_slist *headerlist=NULL;
static const char buf[] = "Expect:";
curl_global_init(CURL_GLOBAL_ALL);
/* Fill in the file upload field */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "sendfile",
CURLFORM_FILE, image_path,
CURLFORM_END);
/* Fill in the message field */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "message",
CURLFORM_COPYCONTENTS, p_message,
CURLFORM_END);
/* Fill in the submit field too, even if this is rarely needed */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "submit",
CURLFORM_COPYCONTENTS, "Submit",
CURLFORM_END);
curl = curl_easy_init();
/* initalize custom header list (stating that Expect: 100-continue is not
wanted */
headerlist = curl_slist_append(headerlist, buf);
if(curl)
{
printf("-----0-----\n");
/* what URL that receives this POST */
curl_easy_setopt(curl, CURLOPT_URL, "http://alarm1.nvdvr.cn:8888/GetAlarmMsg/AlarmUploadServlet");
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5);//超时设置
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
printf("-----1-----\n");
/* Check for errors */
if(res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed!\n");
//fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
printf("-----2-----\n");
/* always cleanup */
curl_easy_cleanup(curl);
printf("-----3-----\n");
/* then cleanup the formpost chain */
curl_formfree(formpost);
printf("-----4-----\n");
/* free slist */
curl_slist_free_all (headerlist);
}
printf("~~~ out post_data~~\n");
return res;
}
各位大神,帮帮忙,怎么解决这个问题???