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("连接超时。");
}
- 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("连接超时。");
- }
转载自:http://hi.baidu.com/baby_66_/item/24c3f0ce96263936e90f2ece
libcurl网络连接使用tcp/ip的更多相关文章
- iOS网络协议 HTTP/TCP/IP浅析
一.TCP/IP协议 话说两台电脑要通讯就必须遵守共同的规则,就好比两个人要沟通就必须使用共同的语言一样.一个只懂英语的人,和一个只懂中文的人由于没有共同的语言(规则)就没办法沟通.两台电 ...
- 嵌入式linux的网络编程(1)--TCP/IP协议概述
嵌入式linux的网络编程(1)--TCP/IP协议概述 1.OSI参考模型及TCP/IP参考模型 通信协议用于协调不同网络设备之间的信息交换,它们建立了设备之间互相识别的信息机制.大家一定都听说过著 ...
- 第12章 网络基础(1)_网络分层和TCP/IP协议族
1. 协议的概念 (1)计算机网络中实现通信必须有一些约定.如对速率.传输代码.代码结构.传输控制步骤和出错控制等约定,这些约定即被称为通信协议 (2)在两个节点之间要成功地进行通信,两个节点之间必须 ...
- 网络编程之TCP/IP各层详解
网络编程之TCP/IP各层详解 我们将应用层,表示层,会话层并作应用层,从TCP/IP五层协议的角度来阐述每层的由来与功能,搞清楚了每层的主要协议,就理解了整个物联网通信的原理. 首先,用户感知到的只 ...
- 网络协议: TCP/IP 和UDP/IP
网络协议: TCP/IP 和UDP/IP TCP/IP TCP/IP(Transmission Control Protocol/Internet Protocol)是一种可靠的网络数据传输控制协议. ...
- TCP/IP概述(网络互联与TCP/IP)
TCP/IP概述(网络互联与TCP/IP) 用IP实现异构网络互联 从用户角度如何实现异构网络互联: 从用户角度看,实现异构网络互联的关键点就是使各种网络类型之间的差异对自己透明.在TCP/IP协议中 ...
- Sqlserver 远程连接的 TCP/IP 和 Named Pipes的区别
TCP/IP: TCP/IP是 Internet 上广泛使用的通用协议.它与互连网络中硬件结构和操作系统各异的计算机进行通信.TCP/IP包括路由网络流量的标准,并能够提供高级安全功能.它是目前在商 ...
- 【转载】[基础知识]【网络编程】TCP/IP
转自http://mc.dfrobot.com.cn/forum.php?mod=viewthread&tid=27043 [基础知识][网络编程]TCP/IP iooops 胖友们楼主我又 ...
- 【网络编程】TCP/IP、UDP、网络概…
计算机刚刚发明出来的时候,两台计算机之间是无法通信的,为了使计算机之间能够进行数据的交流,制定了OSI(Open SystemInterconnection)开放系统互联模型,而TCP/IP(我们所使 ...
随机推荐
- ethereum(以太坊)(六)--整型(int)
pragma solidity ^0.4.20; /* uint8 uint16 ...uint256 int8 int16 int24 ..int256 uint => uint256 int ...
- python__基础 : 类的__new__方法与实现一个单例
__new__ : 这个方法的作用主要是创建一个实例,在创建实例时首先会调用 __new__方法 ,然后调用__init__对实例进行初始化, 如果想修改 __new__ 这个方法,那么最后要 ret ...
- Linux CentOS 7的图形界面安装
1.首先安装X(X Window System) yum groupinstall "X Window System" 2.由于这个软件组比较大,安装过程会比较慢,安装完成后会出现 ...
- 裸机——DDR
1.DDR介绍 DDR,是SDRAM的改进,是双通道的SDRAM, SDRAM是同步动态随机访问存储器. SDRAM与SRAM相对于,二者的特点是: SDRAM 需要初始化,使用时许访问,价格便宜. ...
- git rebase -i
git rebase -i 作用: 合并提交 示例: 如图所示: 原因: 出现了两个第十一章的提交信息, 其实提交内容是一样的, 但是提交概述不一样. 这就让我很不爽. 我想把两次的概述信息合并为一个 ...
- Git-Git库管理
对象和引用哪里去了? 从GitHub上克隆一个示例版本库,这个版本库在"历史穿梭"一章就已经克隆过一次了,现在要重新克隆一份.为了和原来的克隆相区别,克隆到另外的目录.执行下面的命 ...
- 20145202马超 《Java程序设计》第三周学习总结
************************http://git.oschina.net/tuolemi/java这是git的那个网址********************* 函数的重载:在同一 ...
- 11.1,nginx集群概念
集群介绍 为什么要用集群
- 单例解决存储微信Token信息保留两小时
采用单例模式可以存储初始化数据,比如第一次对/api/test接口进行了访问,传入的信息为“123”,则在两个小时之内返回的信息依然是“123”,无论传入的参数是什么,如果有效时间过了两个小时,比如传 ...
- Spark Streaming实例
Spark Streaming实例分析 2015-02-02 21:00 4343人阅读 评论(0) 收藏 举报 分类: spark(11) 转载地址:http://www.aboutyun.co ...