curl 设置超时后出错?

zcb_bbb 2014-10-13 05:06:01

使用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;
}


各位大神,帮帮忙,怎么解决这个问题???
...全文
396 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
mymtom 2014-10-17
  • 打赏
  • 举报
回复
除了什么错,看起来是正常返回的 printf("~~~ out post_data~~\n"); 这句也执行了的。
zcb_bbb 2014-10-14
  • 打赏
  • 举报
回复
怎么没有回应一下

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧