libcurl网络连接使用tcp/ip

CURL *curl;

CURLcode res;

const char *request = "GETas.xxxxE测试发送";

  curl_socket_t sockfd; /* socket */

  long sockextr;

  size_t iolen;

  curl = curl_easy_init();

  if(curl) {

  curl_easy_setopt(curl, CURLOPT_URL, "127.0.0.1");

  curl_easy_setopt(curl, CURLOPT_PORT, 7102);

    /* Do not do the transfer - only connect to host */

    curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);

    res = curl_easy_perform(curl);

    if(CURLE_OK != res)

    {

      printf("Error: %s\n", strerror(res));

      return 1;

    }

    /* Extract the socket from the curl handle - we'll need it for waiting.

     * Note that this API takes a pointer to a 'long' while we use

     * curl_socket_t for sockets otherwise.

     */

    res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockextr);

    if(CURLE_OK != res)

    {

      printf("Error: %s\n", curl_easy_strerror(res));

      return 1;

    }

    sockfd = sockextr;

    /* wait for the socket to become ready for sending */

    //if(!wait_on_socket(sockfd, 0, 60000L))

    //{

    //  printf("Error: timeout.\n");

    //  return 1;

    //}

    puts("Sending request.");

    /* Send the request. Real applications should check the iolen

     * to see if all the request has been sent */

    res = curl_easy_send(curl, request, strlen(request), &iolen);

    if(CURLE_OK != res)

    {

      printf("Error: %s\n", curl_easy_strerror(res));

      return 1;

    }

    puts("Reading response.");

    /* read the response */

    for(;;)

    {

      char buf[1024];

     // wait_on_socket(sockfd, 1, 60000L);

      res = curl_easy_recv(curl, buf, 1024, &iolen);

      if(CURLE_OK == res)

{

      printf("Received %d bytes.\n", iolen);

}

    }

    /* always cleanup */

    curl_easy_cleanup(curl);

  }

对于错误的处理

if( res == CURLE_OK && iolen > 0 )

        {

            //处理数据

             printf("Received %lu bytes.\n", iolen);

        }

        elseif( res == CURLE_RECV_ERROR)

        {

            CCAssert("Client Miss Connect",NULL);

            printf( "socket state error #0 (%d)", res );

            //重连

            

        }

        elseif (res == CURLE_AGAIN )

        {

        }

        elseif(res == CURLE_UNSUPPORTED_PROTOCOL)

        {

            //重连

        }

        elseif(res == CURLE_OPERATION_TIMEDOUT)

        {

            //超时

            printf("连接超时。");

 

        }

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

 

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

posted on 2016-04-23 08:32  Earvin  阅读(3923)  评论(0编辑  收藏  举报

导航