views:

1920

answers:

2

Hi, Thanks a lot for reply. I am stuck here. Can you please help with this situation? Can CFHTTPStream will resolve this problem? Is there any other way to work with this problem ? Thanks.

Hi,

I am connecting to an HTTP url using NSURLConnection. I am supposed to get response after some time intervals (The response is not getting closed from server). Is it possible to read those responsed whenever they are written just like having an infinite while loop on input stream of http url connection in java. The didReceiveData method is not getting called.

Consider it as an http push where response will be written according to its availability on server.

I should be able to connect to server & remain connected till I don't close the connection.

Thanks for help.

+2  A: 

NSURLConnection is not keeping the connection alive forever; it has a timeout and if no data is received within that timeout, it closes the connection again.

The didReceiveData is only called when there is actually data available - if you send the request and nothing comes back from the server as there is no data currently available, it is expected that it is not called, isn't it?

Is the connectionDidFinishLoading method called? If it is called, I guess NSURLConnection was waiting for data a certain amount of time and as no data arrived, it stopped - as this method should always be called once NSURLConnection is done, no matter if it was successful or not.

A NSURLConnection bases on a NSURLRequest and when creating a NSURLRequest, you can manually set a timeout. Try setting a really huge timeout. Timeout is in seconds (fractions of seconds are allowed, it is a floating point number), so try setting 28800.0 as timeout, that is 8 hours and see if the connection now stays open long enough so didReciveData is ever called.

Please also note that NSURLConnection doesn't know where data starts and where data ends, so it might call didReceiveData multiple times for a single piece of data being sent and your app needs to re-assemble the received data itself and it must know if this is a full data block, ready for processing, or if it must wait for more data coming in another didReceiveData callback.

Update

NSURLConnection does not guarantee to deliver data as soon as the data arrived. If it receives some data and it still has room to cache more data, it will keep waiting for more data and aggregate data into a chunk and finally deliver the whole chunk at once. That is why your countdown timer does not work. It does deliver all countdown data, but it does so after the countdown is over, as all the small countdown updates perfectly fit into a single chunk and further the MIME type of the HTTP header announces that many small chunks are following. To avoid calling your method delegate more often than necessary, NSURLConnection caches reply content internally as long as it still has room for doing so.

I'm not sure if you can do what you are trying to do with a NSURLConnection. I personally would rather use a real socket connection for that - but I don't know if that is possible on the iPhone.

In case it is any solace to you, the URL you posted here works nicely in Firefox, but it does not work in Safari 4 (Safari 4 only shows the image at the end, not the countdown). So the behavior you are getting here is the same one you get in Safari.

Mecki
Hi,Thanks a lot for reply. I am stuck here.Can you please help with this situation? Can CFHTTPStream will resolve this problem? Is there any other way to work with this problem ?Thanks.
Prajakta
CFHTTP seems to be the same as the NS-Classes, just for Carbon (C instead of Obj-C), I doubt they will behave any different. You need to open a raw socket connection (CFSocket) and parse all HTTP and MIME headers yourself if you must have access to data as soon as it arrives. Do you have control over the server side? E.g. can you alter the way the server sends out the data? Or do you only have control over the client side?
Mecki
A: 

Hi Mecki, Thanks for reply. But I would like to clear few things ... I can see the data written from server but my didReceiveData method is not getting called. Also the connectionDidFinishLoading is not called. The delegate is set properly. I would like to give example. How can i read responses sent by this example(url).[ My application requires the same functionality.] http://www.servlets.com/jservlet2/examples/ch06/servlet/Countdown It sends responses like 10, 9, 8, 7, 6.....& then comes an image. We can see this is browser. But when i try to connect through code & get response, I get response only when the countdown finishes & that too the data is very large (I think image data). How can I read the previous small responses whenever they are sent ?

Thanks for help.

Prajakta
URL : http://www.servlets.com/jservlet2/examples/ch06/servlet/Countdown
Prajakta