libcurl网络连接使用tcp/ip

转自:http://blog.csdn.net/zengraoli/article/details/11580565

不多说直接看代码:

  1. CURL *curl;  
  2. CURLcode res;  
  3. const char *request = "GETas.xxxxE测试发送";  
  4.   curl_socket_t sockfd; /* socket */  
  5.   long sockextr;  
  6.   size_t iolen;  
  7.   curl = curl_easy_init();  
  8.   if(curl) {  
  9.   curl_easy_setopt(curl, CURLOPT_URL, "127.0.0.1");  
  10.   curl_easy_setopt(curl, CURLOPT_PORT, 7102);  
  11.     /* Do not do the transfer - only connect to host */  
  12.     curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);  
  13.     res = curl_easy_perform(curl);  
  14.     if(CURLE_OK != res)  
  15.     {  
  16.       printf("Error: %s\n", strerror(res));  
  17.       return 1;  
  18.     }  
  19.     /* Extract the socket from the curl handle - we'll need it for waiting. 
  20.      * Note that this API takes a pointer to a 'long' while we use 
  21.      * curl_socket_t for sockets otherwise. 
  22.      */  
  23.     res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockextr);  
  24.     if(CURLE_OK != res)  
  25.     {  
  26.       printf("Error: %s\n", curl_easy_strerror(res));  
  27.       return 1;  
  28.     }  
  29.     sockfd = sockextr;  
  30.     /* wait for the socket to become ready for sending */  
  31.     //if(!wait_on_socket(sockfd, 0, 60000L))  
  32.     //{  
  33.     //  printf("Error: timeout.\n");  
  34.     //  return 1;  
  35.     //}  
  36.     puts("Sending request.");  
  37.     /* Send the request. Real applications should check the iolen 
  38.      * to see if all the request has been sent */  
  39.     res = curl_easy_send(curl, request, strlen(request), &iolen);  
  40.     if(CURLE_OK != res)  
  41.     {  
  42.       printf("Error: %s\n", curl_easy_strerror(res));  
  43.       return 1;  
  44.     }  
  45.     puts("Reading response.");  
  46.     /* read the response */  
  47.     for(;;)  
  48.     {  
  49.       char buf[1024];  
  50.      // wait_on_socket(sockfd, 1, 60000L);  
  51.       res = curl_easy_recv(curl, buf, 1024, &iolen);  
  52.       if(CURLE_OK == res)  
  53. {  
  54.       printf("Received %d bytes.\n", iolen);  
  55. }  
  56.     }  
  57.     /* always cleanup */  
  58.     curl_easy_cleanup(curl);  
  59.   }  
  60. 对于错误的处理  
  61. if( res == CURLE_OK && iolen > 0 )  
  62.         {  
  63.             //处理数据  
  64.              printf("Received %lu bytes.\n", iolen);  
  65.         }  
  66.         elseif( res == CURLE_RECV_ERROR)  
  67.         {  
  68.             CCAssert("Client Miss Connect",NULL);  
  69.             printf( "socket state error #0 (%d)", res );  
  70.             //重连  
  71.               
  72.         }  
  73.         elseif (res == CURLE_AGAIN )  
  74.         {  
  75.         }  
  76.         elseif(res == CURLE_UNSUPPORTED_PROTOCOL)  
  77.         {  
  78.             //重连  
  79.         }  
  80.         elseif(res == CURLE_OPERATION_TIMEDOUT)  
  81.         {  
  82.             //超时  
  83.             printf("连接超时。");  
  84.         }  


转载自:http://hi.baidu.com/baby_66_/item/24c3f0ce96263936e90f2ece


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值