vishnuvarthan
vishnuvarthan

Reputation: 512

NSURLConnection Fails with error codes -1001,-1005,1004 and also NSPOSIXErrorDomain Code=2

We have more number of devices(200+) connected to network to communicate with server, often the connection fails and does not reconnect as mentioned in the question I am getting the mentioned errors.

The code which we use to send request is

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

                NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://ipaddress"];

                NSMutableURLRequest *menuRequest=[[NSMutableURLRequest alloc]initWithURL:url];



                NSURLResponse *response;
                NSError *error=nil;

                NSData *data = [NSURLConnection sendSynchronousRequest:menuRequest returningResponse:&response error:&error];

                dispatch_async(dispatch_get_main_queue(), ^{

                    if(!error)
                    {

                        NSLog(@"Request Success");
                        NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData: data];
                        [xmlParser setDelegate:self];
                        [xmlParser parse];
                    }
                    else
                    {
                       NSLog(@"Failed with error-----%@",error);
                    }
                });
            });

This request is sent every minute.

It happens only in client environment we need to recover from this connection failure and reconnect(the next request does not get success even if the server is up).This happens in both iOS 7 & 8.I went through some similar post but I am not able to get the exact reason and also the solution for these problems.Help me out guys.

Upvotes: 3

Views: 15701

Answers (2)

rustyMagnet
rustyMagnet

Reputation: 4145

Like @quellish said, these errors tell you what happened. I googled a thousand times to investigate codes like -1004. But it is simpler to find them locally...

XCode / Window / Developer Documentation / URL Loading System Error Codes

enter image description here

Upvotes: 1

quellish
quellish

Reputation: 21254

"I went through some similar post but i am not able to get the exact reason and alos the solution for these problems.Help me out guys."

The errors you are getting are the reasons. Specifically, codes -1001, -1005, and -1004 in the NSURLErrorDomain are time outs, network connection lost, and cannot connect to host. You can look these error codes up in the NSURLErrors.h header. These are all problems with your network connectivity.

The code you have posted has no logic to reconnect, which is why it does not attempt to do so.

Upvotes: 6

Related Questions