libcurl网络连接使用tcp/ip
2015-11-02 09:40
721 查看
cc
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("连接超时。");
}
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协议认识
- 【读书笔记】iOS网络-解析响应负载
- 【读书笔记】iOS网络-解析响应负载
- P2P对等网络技术原理整合
- 如何对付网络爬虫 - JavaEye和网络爬虫斗争之路
- 全球网络安全专业实力最强十所大学
- 原生网络请求:同步请求、异步请求、GET请求、POST请求
- 网络音乐平台:如何拥有独家“好声音”?
- HTTP通信过程底层实现原理
- AFN网络请求封装(MRC模式, 若是ARC模式 需添加 -fno-objc-arc )
- 网络通信(Telematik)-传输层协议(Transportprotokoll)2
- org.apache.catalina.connector.RequestFacade cannot be cast to MultipartHttpServletRequest 异常处理
- ZigBee多机双向通信编程方法【星形网络】
- 图解HTTP -- 目录
- 编写爬虫(spider)的预备知识:用java发送HTTP请求
- Windows 下使用 SNMP 来管理网络
- 网络字节序与主机字节序 和它们之间的相互转换函数
- apache httpd学习笔记二 - 命令
- Java+MySQL实现网络爬虫程序
- linux笔记:网络命令ping,traceroute,ifconfig,netstat;挂载和卸载命令mount,umount