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(我们所使 ...
随机推荐
- 列表,元组的操作,for循环
1.列表 # li = ["wang","jian","wei"] # print(li) # 结果:['wang', 'jian', 'w ...
- 【jQuery】手机验证码倒计时效果
<ul class="ulist"> <li class="group"> <label class="label&qu ...
- LINUX操作系统知识:进程与线程详解
当一个程序开始执行后,在开始执行到执行完毕退出这段时间内,它在内存中的部分就叫称作一个进程. Linux 是一个多任务的操作系统,也就是说,在同一时间内,可以有多个进程同时执行.我们大家常用的单CPU ...
- Python分布式爬虫抓取知乎用户信息并进行数据分析
在以前的文章中,我写过一篇使用selenium来模拟登录知乎的文章,然后在很长一段时间里都没有然后了... 不过在最近,我突然觉得,既然已经模拟登录到了知乎了,为什么不继续玩玩呢?所以就创了一个项目, ...
- Makefile (2) gdb
gdb调试 1.用debug的方式编译 -g 2.打上断点 3.单步调试 step into 进入函数里面 step over 运行整个函数 step return 跳出当前函数 4.继续运行 5.打 ...
- POJ:3320-Jessica's Reading Problem(尺取法)
Jessica's Reading Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15224 Accepted: ...
- 3714: [PA2014]Kuglarz
3714: [PA2014]Kuglarz 链接 思路: 好题.对于每个点都需要确定它的值,那么一个点可以直接询问[i,i]来确定,或者已经知道了[i,j]和[i+1,j]推出来. 但是可能产生冲突, ...
- 常用数据库连接URL地址大全
1.Oracle8/8i/9i数据库(thin模式) Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); ...
- 成员变量和属性区别(@property那点事儿)
历史由来: 接触iOS的人都知道,@property声明的属性默认会生成一个_类型的成员变量,同时也会生成setter/getter方法. 但这只是在iOS5之后,苹果推出的一个新机制.看老代码时,经 ...
- lnmp一键安装环境中nginx开启pathinfo
问题及原理可参考:http://www.laruence.com/2009/11/13/1138.html 如果是用lnmp脚本一键安装的开发环境,可以通过如下方式开户pathinfo: 1.注释ng ...