Re: CFReadStream and keep-alive
Re: CFReadStream and keep-alive
- Subject: Re: CFReadStream and keep-alive
- From: Quinn <email@hidden>
- Date: Thu, 21 Jan 2010 10:17:11 +0000
At 15:38 -0800 20/1/10, John Michael Zorko wrote:
1. How to I keep the connection open once my callback gets called
with kCFStreamEventEndEncountered?
See below.
2. It is my understanding that every new HTTP request needs a new CFReadStream
That's correct.
so how do I tell the CFReadStream to _not_ initiate a new
connection, but rather, keep using the current one?
Set the kCFStreamPropertyHTTPAttemptPersistentConnection property.
However, there's more to it than that. See below.
3. Is setting the HTTP keep-alive header to the correct value all I
need to do, or do I need to do something else?
No, CFHTTPStream will take care of that level for you.
The exact requirements to get CFHTTPStream to reuse a connection are
not well documented. However, this has been discussed on the list
many times before. I've included the most relevant (IMO) snippets
below.
When reading this, remember that "Panther" is Mac OS X 10.3 and
"Tiger" is Mac OS X 10.4 (-:
If you're looking for more modern discussions of this, you can search
the list archives.
<http://www.lists.apple.com/macnetworkprog/>
S+E
--
Quinn "The Eskimo!" <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
At 10:47 -0500 15/6/04, Marc Epard wrote:
Date: Tue, 15 Jun 2004 10:47:20 -0500
From: Marc Epard <xxx@yyy>
Subject: kCFStreamPropertyHTTPAttemptPersistentConnection
To: macnetworkprog <email@hidden>
I'm using CFNetwork for HTTP and I'd like to have persistent connections
because I'm doing several transactions in a row. Before calling
CFReadStreamOpen, I do this:
bool isPersistent = CFReadStreamSetProperty(responseStream,
kCFStreamPropertyHTTPAttemptPersistentConnection,
kCFBooleanTrue);
I've verified that isPersistent is true, but CFNetwork is still closing the
TCP stream after each transaction.
I'm making sure to read the entire response. I've tried keeping a reference
to a stream around and not releasing it each time and I've tried not calling
CFReadStreamClose. Still, CFNetwork closes the stream.
I've used tcpdump and tcpflow to verify that both ends are including the
Connection: Keep-Alive header and determined that it is the CFNetwork end
that initiates the TCP close rather than the server. This is on 10.3.4.
Any suggestions?
Thanks.
-Marc
At 9:44 -0500 19/8/04, Marc Epard wrote:
Date: Thu, 19 Aug 2004 09:44:46 -0500
From: Marc Epard <xxx@yyy>
Subject: Re: kCFStreamPropertyHTTPAttemptPersistentConnection
To: macnetworkprog <email@hidden>
In case someone searches for this in the future:
I talked to some Apple engineers at WWDC and found out the trick to get
persistent HTTP connections to work in Panther:
You must call CFReadStreamOpen to issue a subsequent request before calling
CFReadStreamRead to read *any* of the response for the previous request.
Issuing the next request before calling CFReadStreamClose on the previous
response is not sufficient.
[...]
-Marc
At 10:05 -0700 19/8/04, Jeremy Wyld wrote:
Date: Thu, 19 Aug 2004 10:05:10 -0700
From: Jeremy Wyld <xxx@yyy>
Subject: Re: kCFStreamPropertyHTTPAttemptPersistentConnection
To: macnetworkprog Programming <email@hidden>
Let me fill in the details.
CFNetwork on Panther is very aggressive about reaping the HTTP
sockets. If another request is queued before all the bytes are
received on the previous request, the persistent connection will be
maintained. The problem is that the act of reading could be the
call to close down your persistent connection. This is why the
described method will work. The open will cause the queueing, and
the persistent connection is still open until the read.
CFNetwork on Tiger has been changed to be less aggressive. An
underlying, persistent connection is open as long as the last stream
on that connection. Calling open before the previous request's
close will get the new request enqueued on the persistent
connection. "GET Example" in /Developer/Examples/Networking/ has
been changed on the Tiger CD to use this model.
jeremy
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden