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("连接超时。");
}
libcurl网络连接使用tcp/ip
来源:个人博客 2016-04-01 10:59:23 人气: 我有话说( 0 人参与)
libcurl网络连接使用tcp ip,部分代码如下:CURL *curl;CURLcode res;const char *request = "GETas.xxxxE测试发送"; curl_socket_t sockfd; * socket * ...
部分代码如下:
本文源自互联网,采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可,
版权归原作者,如有问题请联系service@tsingfun.com (编辑:admin)
版权归原作者,如有问题请联系service@tsingfun.com (编辑:admin)
相关热点
- 1google mock分享(全网最全最好的gmock文...
- 2【解决】asan runtime does not come ...
- 3coinitialize失败,返回值是0x80010106 无...
- 4Reference to ' ' is ambiguous:符号定义重复
- 5hidden symbol ... is referenced by DSO 剖析
- 6浅析Linux段错误:SEGV_MAPERR、SEGV_ACCERR
- 7use of deleted function std::unique_...
- 8VS Addin插件基本开发入门
- 9【解决】munmap_chunk(): invalid pointer
本月排行
- 1C/C++ 如何向上取整? 160次浏览
- 2hidden symbol ... is referenced by DSO 剖析 100次浏览
- 3Linux C++ 单元测试与gcov代... 53次浏览
- 4【解决】double free or corruption (!prev) 46次浏览
- 5CMFCTabCtrl的使用、颜色样式调整 41次浏览
- 6解决:Run-Time Check Failur... 35次浏览
- 7CFileDialog用法及参数解释 30次浏览
- 8error C2220: 警告被视为错误... 27次浏览
- 9与复制构造函数相关的错误.例如... 27次浏览
- 10C++使用OLE/COM高速读写EXCEL的源码 26次浏览
评论排行
- 1C++ 读写xml方法整理(持续更新)3次评论
- 2Reference to ' ' is ambiguous:符号定义重复2次评论
- 3全民OS2次评论
- 4__attribute__2次评论
- 5海量数据相似度计算之simhash和海...2次评论
- 6C++使用OLE/COM高速读写EXCEL的源码2次评论
- 7AfxIsValidAddress 测试内存地址2次评论
- 8C++入门进阶最佳实战2次评论
- 9phpcms附件上传 Flash换成H52次评论
- 10解决xrdp登陆不上的问题:xrdp s...2次评论