On Nov 28, 2008, at 7:58 AM, Michael Bernardo wrote: I've followed the model used by sample code at "/Developer/Examples/Networking/Get Example" to get a single persistent connection working. I can verify that with single, serial requests, the connection is re-used.
This is a really old sample that predates the higher-level networking classes in Foundation. It still works, but these days if you want to use HTTP, you should almost certainly start with NSURLRequest / NSURLConnection, which do all the work for you, including managing multiple connections. These are the same classes that Safari uses. The trouble starts when I try to send simultaneous overlapping requests. It seems that CFNetwork only creates a single underlying network connection, no matter how many simultaneous requests are outstanding.
Well, the code you're using manages the connections directly. A CFStream corresponds directly to a single TCP socket, and this sample code only creates one of them. If you wanted to change it to open multiple connections, you'd have to create multiple CFStreams, probably by creating multiple GetExampleDelegate objects.
But I strongly recommend you stop using this code and use NSURLConnection instead. There is a lot of complexity to HTTP that this example doesn't get into, which you'd have to implement yourself — things like SSL support, pipelining, caching, GZIP compression…
—Jens
PS: Hey, Quinn — should that example be deprecated or something? It still serves a purpose in case someone absolutely has to implement their own HTTP support for some reason, but 99% of developers shouldn't be using it. Instead there should be some examples using NSURLConnection (I couldn't find any, either there or in /D/E/Foundation.) |