尝试连接时取消curl_easy_perform [英] Cancel curl_easy_perform while it is trying to connect

查看:872
本文介绍了尝试连接时取消curl_easy_perform的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以取消C ++中的 curl_easy_perform ?

Is there a way that I can cancel a curl_easy_perform in C++??

我有一种情况,我试图连接到脱机的网络资源,而curl_easy_perform需要一定的时间才能返回 CURLE_COULDNT_CONNECT . 但是与此同时,用户已关闭希望连接的UI,我想立即切断curl连接,而不必等待它失败.

I have a scenario where I am trying to connect to network resource that is offline and the curl_easy_perform take a certain amount of time before it returns CURLE_COULDNT_CONNECT. But in the meantime the user has closed the UI that wishes to connect and I want to immediatley cut off the curl connection and not have to wait for it to fail to connect.

我尝试存储使用的curl指针:

I have tried storing the curl pointer that I use:

CURL *pEasy = curl_easy_init ();

然后在执行curl_easy_perform时,我打电话给

And then while it is doing curl_easy_perform I called

curl_easy_cleanup(pEasy);

试图释放或停止" curl连接,但它崩溃了,我从 curl_easy_perform 中得到了以下错误:

In an attempt to release or 'stop' the curl connection but it crashed and I got the following error from curl_easy_perform:

Unhandled exception at blah blah: Access violation reading location 0x00000004.

有没有办法可以取消连接尝试???? 我已阅读此链接:如何立即取消卷曲操作?,但似乎可以在成功连接后取消取消curl连接.

Is there a way I can cancel the connection attempt???? I have read this link: How can I immediately cancel a curl operation? but it seems to handle cancelling a curl connection after it has successfully connected.

编辑-解决方案

我发现如果我打电话:

curl_easy_reset( pEasy );

然后

curlResult = curl_easy_perform ( pEasy );

几乎立即返回结果 CURLE_OPERATION_TIMEDOUT .

我认为这就是我所需要的:)

I think this is what I need :)

推荐答案

您可以在线程中运行curl_easy_perform并保留CURL对象.

You can run curl_easy_perform in a thread and keep the CURL object.

要关闭连接时,在CURL对象上调用curl_easy_setopt(m_chatCurl, CURLOPT_TIMEOUT_MS, 1).

On the CURL object call curl_easy_setopt(m_chatCurl, CURLOPT_TIMEOUT_MS, 1) whenever you want to close the connection.

这样,连接将超时并且线程将能够完成.

This way the connection will timeout and the thread will be able to finish.

这篇关于尝试连接时取消curl_easy_perform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆