Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone delayed response from server

As soon as, I send request to the server (via NSURLConnection sendSynchronousRequest method), the server receives it in about 2 seconds, it processes and sends back response in another 3-5 seconds. However, I only get back the response in 30-35 seconds. This delay makes our communication very slow.

Even the async APIs are getting a delayed response.

Earlier, everything was working fine, with client getting the response back within 10 seconds. Anyone else having this issue? What could be the reason?

EDIT here is a screenshot of Wireshark analysis:

Link to a better image

enter image description here

How should I see what packet is saying what?..and why is it getting delayed?

EDIT2 Here is the code:

 NSHTTPURLResponse *response=nil;

NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:nsURL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:180.0];
[theRequest setHTTPMethod:@"POST"];
[theRequest setTimeoutInterval:180.0];
[theRequest setHTTPBody:[[NSString stringWithFormat:@"%@",sdata] dataUsingEncoding:NSASCIIStringEncoding]];

NSError *error= nil;

NSData *result = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
if (error ) {
    NSLog(@"error sending synchronous request: %@", error);
}
NSLog(@"request completed with code:%d",response.statusCode);
like image 435
Nikita P Avatar asked Dec 20 '22 11:12

Nikita P


2 Answers

I'd avoid calling sendSynchronousRequest. Use the asynchronous version instead if you're not doing the call already on a background thread (you don't want to block the UI thread).

How do you know when the iOS response is received? An NSLog? A UI state change?

See also these questions:

NSURLConnection sendSynchronousRequest - background to foreground

NSURLConnection sendSynchronousRequest taking too much time to respond

Update

If you're a bit stuck, one strategy might be to rule out the use of NSURLConnection as the problem.

  • Strategy 1: try using NSURL's asychronous connection call instead of synchronous
  • Strategy 2: try using a different HTTP lib, such as AFNetworking

If you want to take a closer look at what is going on with the HTTP connection, you can use tools such as Charles, Fiddler or Wireshark to debug what data is being sent and received. To get the most benefit from this sort of analysis, you need to have some knowledge of the HTTP protocol(s). This is probably more time consuming than the previously mentioned strategies.

See also questions such as How to monitor network calls made from iOS Simulator.

Update

Are you accessing a webserver of your own, or is it someone else's?

Have you had a close look at the headers being sent to your webserver (and the ones being returned)? Pay attention to the content length, for example. Wrong content length can cause a delay, as explained here.

To see the request and returned headers, you could use Firebug, or something like wget or curl on the command line.

Also, double check that there's not a newline on the end of your URL, as described here.

like image 55
occulus Avatar answered Dec 31 '22 12:12

occulus


Solution that worked for me:

In the request headers, iOS sets "gzip" for "Accept-Encoding" by default. The gzip compression was taking a lot of time, and hence the delayed response. I did the following to solve the problem:

[theRequest setValue:@"" forHTTPHeaderField:@"Accept-Encoding"];

NOTE: Check your headers, for any response delay.

Thanks to @occulus for directing me to the request headers!

like image 43
Nikita P Avatar answered Dec 31 '22 14:12

Nikita P