Using libcurl multi interface for consecutive requests for same “easy” handle

拥有回忆 提交于 2019-12-10 20:29:21

问题


My development organization has its own wrapper implementation for threads and select(). The application needs to be enhanced to perform HTTPS requests, and I've decided to use libcurl. After some research, I see that the curl_easy_perform is a blocking call, so I've decided to use the curl_multi_perform approach for non-blocking calls to allow other work in the threads.

The HTTPS request will need to be performed periodically to the same URL. I know I can keep the same curl_easy handle and give that to the curl_multi handle. I would perform the curl_multi_perform to get the results, but I would later need to use curl_multi_perform to resend the request, say in 5 minutes. So, this would be a consecutive request using the same easy handle. However, I'm not sure how the curl_easy interface tells the multi interface as to when to resend the request after I have received results from the first request. How do I accomplish this?

(Maybe drop the easy handle from the multi handle, and re-add it to the multi handle when a request is needed again?)

I presume that whatever technique is used, that the outgoing request will be using the same outgoing port.


回答1:


(Maybe drop the easy handle from the multi handle, and re-add it to the multi handle when a request is needed again?)

Correct. From the libcurl documentation:

When a single transfer is completed, the easy handle is still left added to the multi stack. You need to first remove the easy handle with curl_multi_remove_handle and then close it with curl_easy_cleanup, or possibly set new options to it and add it again with curl_multi_add_handle to start another transfer.

.

I presume that whatever technique is used, that the outgoing request will be using the same outgoing port

This is not guaranteed. libcurl will attempt to re-use existing connections that are associated with the easy handle, but if the previous connection has already died then a new connection with an unpredictable local port will be established.



来源:https://stackoverflow.com/questions/54483937/using-libcurl-multi-interface-for-consecutive-requests-for-same-easy-handle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!