cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: How to explicitly close underlying TCP connection in libcurl

From: ajil koshy <ajil.koshy_at_gmail.com>
Date: Mon, 28 Mar 2011 09:48:32 +0530

Hi,

Yes, I need to be able to explicitly connect and disconnect to the server.
The interval in which client should connect to the server is fixed- say once
in every 30 minutes. During each interval, following steps need to be
performed.

*On Startup:*
Initialize libcurl handle - This may result in client connecting once to the
server and disconnecting immediately. I'm set CURLOPT_CONNECT_ONLY and call
curl_easy_perform. Keep the CURL easy handle open.

*During each connect interval:*
1) Client opens a TCP connection to server
2) Client exchanges some data with server: I'm using curl_easy_send and
curl_easy_receive in order to achieve non-blocking, bi-directional exchange.
Chunked transfer is used here and both ends should be able to send data
simultaneously. Hence, not using CURLOPT_READFUNCTION &
CURL_OPTWRITEFUNCTION here.
3) Client (explicitly) disconnects from server *<=Q: How do I achieve
this???

**On client shutdown:*
Close the CURL easy handle and free all resources like SSL session cache,
etc.

Since I am using HTTPS and want to use SSL session caching, there is the
additional constraint that I keep the CURL easy handle alive throughout the
life-time of client process. Also, I have read that CURLOPT_FORBID_REUSE
cannot be used with CURLOPT_CONNECT_ONLY.

Thanks
Ajil

Date: Fri, 25 Mar 2011 16:12:13 +0100 (CET)
> From: Daniel Stenberg <daniel_at_haxx.se>
> To: libcurl development <curl-library_at_cool.haxx.se>
> Subject: Re: How to explicitly close underlying TCP connection in
> libcurl
> Message-ID: <alpine.DEB.2.00.1103251605470.16153_at_tvnag.unkk.fr>
> Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed
>
> On Fri, 25 Mar 2011, ajil koshy wrote:
>
> > I am writing an HTTPS client using libcurl/OpenSSL. The client is
> expected
> > to periodically connect to the server, exchange data and close the
> > connection i.e. not keep a persistent connection. What would be the best
> way
> > to explicitly close the underlying TCP connection from the client side
> > without having to close the libcurl handle? I want to keep the Libcurl
> > handle alive in order to leverage the SSL session cache. I currently see
> two
> > options.
>
> Are you really really sure you want to force a close of the connection? If
> you
> are to do another request within a reasonable time, it will be much less
> strain on the resources if you can reuse the connection.
>
> > a) Using undocumented function Curl_disconnect ()
>
> That's not only undocumented, it is internal and would be a very unwise to
> use.
>
> > b) Limit CURLOPT_MAXCONNECTS to 1 and use CURL_OPT_CONNECT_ONLY: To
> > disconnect, we can change the URL on the CURL handle and simply call call
> > curl_easy_perform ().
>
> Sorry, I don't see how that would have the desired effect.
>
> > Do you think either of the above is a nice way (probably not :-) ) to do
> it?
> > Can you suggest the right way to do this?
>
> Set CURLOPT_FORBID_REUSE on the specific handle you don't want to re-use,
> as
> it should make the connection get closed instead.
>
> --
>
> / daniel.haxx.se
>
>
> ------------------------------
>

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2011-03-28