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. elseif (res == CURLE_AGAIN )
  72. {
  73. }
  74. elseif(res == CURLE_UNSUPPORTED_PROTOCOL)
  75. {
  76. //重连
  77. }
  78. elseif(res == CURLE_OPERATION_TIMEDOUT)
  79. {
  80. //超时
  81. printf("连接超时。");
  82. }

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

libcurl网络连接使用tcp/ip的更多相关文章

  1. iOS网络协议 HTTP/TCP/IP浅析

    一.TCP/IP协议       话说两台电脑要通讯就必须遵守共同的规则,就好比两个人要沟通就必须使用共同的语言一样.一个只懂英语的人,和一个只懂中文的人由于没有共同的语言(规则)就没办法沟通.两台电 ...

  2. 嵌入式linux的网络编程(1)--TCP/IP协议概述

    嵌入式linux的网络编程(1)--TCP/IP协议概述 1.OSI参考模型及TCP/IP参考模型 通信协议用于协调不同网络设备之间的信息交换,它们建立了设备之间互相识别的信息机制.大家一定都听说过著 ...

  3. 第12章 网络基础(1)_网络分层和TCP/IP协议族

    1. 协议的概念 (1)计算机网络中实现通信必须有一些约定.如对速率.传输代码.代码结构.传输控制步骤和出错控制等约定,这些约定即被称为通信协议 (2)在两个节点之间要成功地进行通信,两个节点之间必须 ...

  4. 网络编程之TCP/IP各层详解

    网络编程之TCP/IP各层详解 我们将应用层,表示层,会话层并作应用层,从TCP/IP五层协议的角度来阐述每层的由来与功能,搞清楚了每层的主要协议,就理解了整个物联网通信的原理. 首先,用户感知到的只 ...

  5. 网络协议: TCP/IP 和UDP/IP

    网络协议: TCP/IP 和UDP/IP TCP/IP TCP/IP(Transmission Control Protocol/Internet Protocol)是一种可靠的网络数据传输控制协议. ...

  6. TCP/IP概述(网络互联与TCP/IP)

    TCP/IP概述(网络互联与TCP/IP) 用IP实现异构网络互联 从用户角度如何实现异构网络互联: 从用户角度看,实现异构网络互联的关键点就是使各种网络类型之间的差异对自己透明.在TCP/IP协议中 ...

  7. Sqlserver 远程连接的 TCP/IP 和 Named Pipes的区别

    TCP/IP:  TCP/IP是 Internet 上广泛使用的通用协议.它与互连网络中硬件结构和操作系统各异的计算机进行通信.TCP/IP包括路由网络流量的标准,并能够提供高级安全功能.它是目前在商 ...

  8. 【转载】[基础知识]【网络编程】TCP/IP

    转自http://mc.dfrobot.com.cn/forum.php?mod=viewthread&tid=27043 [基础知识][网络编程]TCP/IP iooops  胖友们楼主我又 ...

  9. 【网络编程】TCP/IP、UDP、网络概…

    计算机刚刚发明出来的时候,两台计算机之间是无法通信的,为了使计算机之间能够进行数据的交流,制定了OSI(Open SystemInterconnection)开放系统互联模型,而TCP/IP(我们所使 ...

随机推荐

  1. spring-开发需要jar包

    需要的开发包 Spring核心开发包: Commons-logging.jar Spring-beans.jar Spring-context.jar Spring-core.jar Spring-e ...

  2. MySQL5.6基于mysql-proxy实现读写分离

    已经搭建好MySQL主从架构 10.205.22.185 #mysql-proxy 10.205.22.186 #master 10.205.22.187 #slave 1.安装mysql-proxy ...

  3. JavaScript---ECMA对象

    1.对象的概念及分类 1.1 ECMAScript中没有类,但定义了“对象”,逻辑上等价于其他程序设计语言中的类. var o = new Object(); 1.2 本地对象(native obje ...

  4. Shell学习——数组

    1.普通数组:只能用整数作为索引1.1.赋值[root@client02 ~]# array[0]=test1[root@client02 ~]# array[1]=test2[root@client ...

  5. MySQL Limit 限定查询记录数

    MySQL Limit 限定查询记录数 MySQL LIMIT MySQL 中 LIMIT 关键字用于限定查询记录返回最大数目. 语法: ... LIMIT offset , rows 该语法中,of ...

  6. chroot: cannot run command `/bin/bash': No such file&nbs

    最近在使用chroot去重新的挂载一个根目录,总是出现上面的问题,很烦,好久了没有解决, 然后自己就写了一个复制依赖库的脚本,然后发现可以切换了,然后就重新试着去挂载根目录 终于发现了原因. ---- ...

  7. C语言实例解析精粹学习笔记——29

    题目: 将字符行内单字之间的空格平均分配插入到单字之间,以实现字符行排版.也就是输入一个英文句子,单词之间的空格数目不同,将这些空格数平均分配到单词之间,重新输出. 代码如下(是原书中配套的代码,只是 ...

  8. 深度学习 GPU环境 Ubuntu 16.04 + Nvidia GTX 1080 + Python 3.6 + CUDA 9.0 + cuDNN 7.1 + TensorFlow 1.6 环境配置

    本节详细说明一下深度学习环境配置,Ubuntu 16.04 + Nvidia GTX 1080 + Python 3.6 + CUDA 9.0 + cuDNN 7.1 + TensorFlow 1.6 ...

  9. 1-Linux运维人员要求

    linux 运维: 1.linux基础操作命令2.linux基础服务搭建3.文本处理命令4.shell脚本编程 python perl php5.数据库 mysql oracle6.lvs集群 热备 ...

  10. ListView Viewholder的坑 线性布局的坑

    1.ListView Viewholder的坑 /** * 默认带图片的menu adapter */ public static class MenuImageAdapter extends Bas ...