Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 372
vishnuvarthan Avatar asked Nov 20 '25 23:11

vishnuvarthan


2 Answers

"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.

like image 83
quellish Avatar answered Nov 24 '25 12:11

quellish


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

like image 40
rustyMagnet Avatar answered Nov 24 '25 10:11

rustyMagnet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!