libcurl网络连接使用tcp/ip
2014-05-26 14:10
941 查看
转自:http://blog.csdn.net/zengraoli/article/details/11580565
不多说直接看代码:
[cpp] view
plaincopyprint?
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("连接超时。");
}
转载自:http://hi.baidu.com/baby_66_/item/24c3f0ce96263936e90f2ece
不多说直接看代码:
[cpp] view
plaincopyprint?
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("连接超时。");
}
转载自:http://hi.baidu.com/baby_66_/item/24c3f0ce96263936e90f2ece
相关文章推荐
- libcurl网络连接使用tcp/ip
- libcurl网络连接使用tcp/ip
- libcurl网络连接使用tcp/ip
- [转]linux下iftop工具的安装与使用详解(图文)——实时的网络流量,监控TCP/IP连接(单机)
- 到主机 的 TCP/IP 连接失败 (SQL2000数据库,使用的jar包是sqljdbc.jar->sql2005的包)
- 由tcpip.sys损坏引起的网络无法连接的问题
- C#如何使用GetTcpTable API获取TCP/IP连接信息
- [导入]用TCP/IP 网络连接过滤保护用户计算机
- 显示协议统计信息和当前 TCP/IP 网络连接。
- Java网络编程(一) TCP/IP,http,socket,长连接,短连接
- 数据连接linux网络编程之TCP/IP基础(四):TCP连接的建立和断开、滑动窗口
- sqlserver 2005没有1433端口|无法启动当使用tcp/ip连接时
- jsp,使用jdbc连接数据库出现“到主机的TCP/ip连接失败”的解决办法
- iPhone开发技巧之网络篇(5)--- 使用libcurl连接https服务器
- [导入]用TCP/IP 网络连接过滤保护用户计算机
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第四节 获取当前TCP连接情况
- Netstat用于显示与IP、TCP、UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况
- 修改tcpip.sys,破除winxp的10个网络连接限制(转)
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第四节 获取当前TCP连接情况
- 使用ZLG TCP/IP建立多个UDP连接的注意事项