cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: How to explicitly close underlying TCP connection in libcurl

From: Daniel Stenberg <daniel_at_haxx.se>
Date: Mon, 28 Mar 2011 09:33:36 +0200 (CEST)

On Mon, 28 Mar 2011, ajil koshy wrote:

Please: http://curl.haxx.se/mail/etiquette.html#Do_Not_Top_Post

Seriously, I will stop responding unless you start behaving.

> 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.

Already there at the first step you're not using libcurl as designed and
you're sort of on your own. I cannot recommend this approach.

When you're on your own, you explicitly deny yourself lots of libcurl services
and features and it seems you want at least some of them.

> *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.

That's another way of saying "I don't need libcurl's services and instead I'll
do it all myself". Again, this is not something I recommend. *Especially* so
if the protocol you're using is one of them libcurl knows.

> Chunked transfer is used here and both ends should be able to send data
> simultaneously.

Right. HTTP is a full duplex protocol so you can't truly support HTTP without
being able to send and receive at the same time.

> 3) Client (explicitly) disconnects from server *<=Q: How do I achieve
> this???

You call curl_easy_cleanup() on the easy handle.

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

When you're using the easy interface there's no such fine granuality. You
close the handle when you want to clean up/shutdown the client operations.

> 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.

I strongly suggest that you avoid CURLOPT_CONNECT_ONLY. I don't think you need
it.

I suggest you use the multi interface and that you set CURLOPT_FORBID_REUSE
for the connections that need to be closed as soon as possible after use.

-- 
  / 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