CURLOPT_TIMEOUT and SIGALRM

168 篇文章 0 订阅

http://curl.haxx.se/mail/lib-2003-01/0197.html

extern "C" 
{ 
#include "curl/curl.h" 
#include "curl/easy.h" 
#include <stdlib.h> 

#include <iostream.h> 
#include <pthread.h> 
#include <signal.h> 

struct MemoryStruct 
{ 
      char* memory; 
      size_t size; 
}; 

size_t writeChunk(void* ptr, size_t size, size_t nmemb, void* data) 
{ 
    
   register int realsize = size * nmemb; 
   struct MemoryStruct* mem = (struct MemoryStruct*)data; 

   mem->memory = (char*)realloc(mem->memory, mem->size + realsize + 1); 
   if (mem->memory) 
   { 
      memcpy(&(mem->memory[mem->size]), ptr, realsize); 
      mem->size += realsize; 
      mem->memory[mem->size] = 0; 
   } 
   return realsize; 

extern "C" 
{ 
void* work(void* pTimeout) 
{ 
   sigset_t sigset; 
   sigemptyset(&sigset); 
   sigaddset(&sigset, SIGALRM); 

   pthread_sigmask(SIG_BLOCK, &sigset, NULL); 

   int timeout = *(int*)pTimeout; 
   char url[500]; 

   strcpy(url, "http://**.*.**.**/GET /NAP?RIC=RTR.L&FORMAT=XML HTTP/1.1"); 
    
   struct MemoryStruct chunk; 
   chunk.memory = NULL; 
   chunk.size = 0; 

   CURL* pCurl = curl_easy_init(); 
   if (!pCurl) 
   { 
      cout << "Error : Failed to initialise curl" << endl; 
      return NULL; 
   } 

   CURLcode cCode; 
   cCode = curl_easy_setopt(pCurl, CURLOPT_URL, url); 
   cCode = curl_easy_setopt(pCurl, CURLOPT_VERBOSE, true); 
   cCode = curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS, true); 
   cCode = curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, writeChunk); 
   cCode = curl_easy_setopt(pCurl, CURLOPT_FILE, (void*)&chunk); 
   cCode = curl_easy_setopt(pCurl, CURLOPT_HEADER, true); 
   cout << "Setting timeout " << timeout << endl; 
   cCode = curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, timeout); 
   char errorBuffer[300]; 
   cCode = curl_easy_setopt(pCurl, CURLOPT_ERRORBUFFER, errorBuffer); 
    
   cCode = curl_easy_perform(pCurl); 

   curl_easy_cleanup(pCurl); 

   if (cCode != 0) 
   { 
      cout << "Timeout " << timeout << " Error : curl : " 
           << errorBuffer << endl; 
   } 
   else 
   { 
      cout << "curl success : timeout " << timeout << endl; 
   } 

   return NULL; 


int main(int argc, char *argv[]) 
{ 
   pthread_t thr; 
   pthread_t thr1; 
   pthread_t thr2; 
   pthread_t thr3; 
   pthread_t thr4; 
   int timeout = 30; 
   int timeout1 = 60; 
   int timeout2 = 90; 
   int timeout3 = 120; 
   int timeout4 = 150; 

   sigset_t sigset; 
   sigemptyset(&sigset); 
   sigaddset(&sigset, SIGALRM); 

   pthread_sigmask(SIG_BLOCK, &sigset, NULL); 

   cout << "Creating thread 3" << endl; 
   pthread_create(&thr3, 
                  NULL, 
                  work, 
                  (void*)&timeout3); 
    
   cout << "Creating thread " << endl; 
   pthread_create(&thr, 
                  NULL, 
                  work, 
                  (void*)&timeout); 

   cout << "Creating thread 4" << endl; 
   pthread_create(&thr4, 
                  NULL, 
                  work, 
                  (void*)&timeout4); 

   cout << "Creating thread 2" << endl; 
   pthread_create(&thr2, 
                  NULL, 
                  work, 
                  (void*)&timeout2); 

   cout << "Creating thread 1" << endl; 
   pthread_create(&thr1, 
                  NULL, 
                  work, 
                  (void*)&timeout1); 

   cout << "joining thread 3" << endl; 
   pthread_join(thr3, NULL); 
   cout << "joining thread " << endl; 
   pthread_join(thr, NULL); 
   cout << "joining thread 4" << endl; 
   pthread_join(thr4, NULL); 
   cout << "joining thread 2" << endl; 
   pthread_join(thr2, NULL); 
   cout << "joining thread 1" << endl; 
   pthread_join(thr1, NULL); 
  
   return 0; 

------------------------------------------------------- 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值