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. CentOS 7.4使用yum源安装MySQL5.7

    从CentOS 7.0发布以来,yum源中开始使用Mariadb来代替MySQL的安装.即使你输入的是yum install -y mysql , 显示的也是Mariadb的安装内容.使用源代码进行编 ...

  2. (转)神舟飞船上的计算机使用什么操作系统,为什么是自研发不是 Linux?

    中国航天用的SpaceOS主要内容是仿造美国风河系统公司的VxWorks653(653是产品名,并非版本号).先解释为什么用这个系统不用Linux:航天器的内存和CPU都非常弱,弱到什么程度呢:天宫一 ...

  3. scrapy--matplotlib

    昨天晚上看了一些关于保存文件的相关资料,早早的睡了,白天根据网上查找的资料,自己再捡起来.弄了一上午就爬取出来了,开心!!!好吧,让我们开始 老规矩,先上图.大家也赶快行动起来 分类文件: 文件内co ...

  4. 【nginx】root alias 区别,以及server root , location root 区别

    nginx-root-alias-详解 最近在研究前后端分离站点配置在同一域名下,发现root,alias有区别,而且所有的root如果都放置在location下面访问无效的问题,才有此总结,本文只是 ...

  5. 对Neural Machine Translation by Jointly Learning to Align and Translate论文的详解

    读论文 Neural Machine Translation by Jointly Learning to Align and Translate 这个论文是在NLP中第一个使用attention机制 ...

  6. rootfls(根操作系统)

    rootfs根文件系统,linux下的任何目录都是rootfs的一个路径 Files 文件 Directory   目录 FHS(文件层级标准)规定了linux发行标准,也就是一些固定的文件存储 文件 ...

  7. 移动端的拖拽排序在react中实现 了解一下

    最近做一个拖拽排序的功能找了好几个有一个步骤简单,结合redux最好不过了,话不多说上代码 第一步: npm install react-draggable-tags --save 第二步 sort. ...

  8. NPM安装vue-cli,并创建vue+webpack项目模板

    1.安装npm npm 是node.js 的包管理工具, 安装流程地址:https://docs.npmjs.com/cli/install  估计会非常慢,我们可以使用淘宝NPM镜像下载安装:htt ...

  9. centos使用--supervisor使用

    目录 1 下载程序并安装 2 编辑配置文件 3 supervisor的使用 4 配置文件详细解析 参考资料 supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变 ...

  10. equals和toString

    Object的equals方法默认比较地址值.所以当需要比较两个对象的内容时需要重写equals方法.