Cannot connect to localhost through LAN IP address

Hello!


HTTP requests to localhost using the LAN IP address return error POSIX 2 "No such file or directory". It happens with NSURLConnection and NSURLSessionDataTask. It started happening when I installed OSX El Capitan (final release) and only on iOS 9 and tvOS 9 simulators. iOS 8 works perfectly, iOS 9 and tvOS 9 devices work fine too.


Before El Capitan, with Yosemite, there was no problem on any simulator.


You can try to reproduce it with this code:


    NSError *error = nil;
    NSURLResponse *response = nil;

    NSURLRequest *requestLocalhost = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:9001"]]; // localhost
    NSURLRequest *request127IP = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://127.0.0.1:9001"]]; // localhost through LAN IP address
    NSURLRequest *requestLocalhostLANIP = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.0.59:9001"]]; // localhost through LAN IP address
    NSURLRequest *requestOtherLANIP = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.0.35:9001"]]; // localhost through LAN IP address

    [NSURLConnection sendSynchronousRequest:requestLocalhost returningResponse:&response error:&error];
    NSLog(@"Error through localhost: %@", error);

    error =nil;
    [NSURLConnection sendSynchronousRequest:request127IP returningResponse:&response error:&error];
    NSLog(@"Error through 127.0.0.1: %@", error);

    error =nil;
    [NSURLConnection sendSynchronousRequest:requestLocalhostLANIP returningResponse:&response error:&error];
    NSLog(@"Error through LAN IP address: %@", error);

    error =nil;
    [NSURLConnection sendSynchronousRequest:requestOtherLANIP returningResponse:&response error:&error];
    NSLog(@"Error to other LAN IP address: %@", error);


Just start an HTTP server somewhere in your computer (running "python -m SimpleHTTPServer 9001" in your Terminal) and also in another computer. Replace "192.168.0.59" with your machin IP address, and "192.168.0.35" with the other machin IP address.


This is the output I get:


Error through localhost: (null)
Error through 127.0.0.1: (null)
Error through LAN IP address: Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory" UserInfo={NSErrorFailingURLStringKey=http://192.168.0.59:9001/, NSErrorFailingURLKey=http://192.168.0.59:9001/, _kCFStreamErrorCodeKey=2, _kCFStreamErrorDomainKey=1}
Error to other LAN IP address: (null)


Again, the same happens with NSURLSessionDataTask. Any help is welcome!


Thank you in advance 🙂

Accepted Reply

I reported it to http://bugreport.apple.com and it was closed as duplicated. It also appears as a known issue in Xcode 7.1 beta 3 release notes, so I guess we can only wait for them to fix it 🙂


Thank you everyone!

Replies

Have you found the solution to this? I just updated to El Capitan and can't get the simulator to connect to localhost:9000.


Safari has no problems connecting to it though.

Having the same issue. I'm using Alamofire in my Swift App. Cannot connect to an API on my local server.


This is my output:


Request]: { URL: http://192.168.0.100:4000 }
[Response]: nil
[Data]: 0 bytes
[Result]: FAILURE: Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory" UserInfo={_kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=2}
Optional({ URL: http://192.168.0.100:4000 })
nil
Optional(<>)
FAILURE

A bump and a "me to". I'm seeing this in the non pre-release iOS 9 / XCode 7 as well. In case it's relevant it's an http url (no certificate) and I'm using NSAllowsArbitraryLoads = YES in my info plist.

I reported it to http://bugreport.apple.com and it was closed as duplicated. It also appears as a known issue in Xcode 7.1 beta 3 release notes, so I guess we can only wait for them to fix it 🙂


Thank you everyone!

We are now seeing this as well and its a show stopper. We are using the Amazon AWS3 AWSS3TransferUtility for file upload/download. We chose their implementation for its background transfer capability which wraps NSURLSessionUploadTask and NSURLSessionDownloadTask.


The uploadFile method produces:

Cannot read file at file:///var/mobile/Containers/Data/Application/F505C2A6-3CA3-47F4-87A1-3FD2CFDE932D/Documents/1098/1098.png


The uploadData method just writes the data to a local file and passes the fireURL to be uploaded and that produces.


Cannot read file at file:///private/var/mobile/Containers/Data/Application/652CAEA4-9174-4408-AFFD-760B275FAB5B/tmp/ea3d80e0cfabcedbe25cc9403ef3bca5/84B78171-E4CA-4F02-9297-F30756657B14-4889-00000AB32D416253.tmp


In both cases the underlying error is:

Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}


I'm at El Capitan 10.11.2 and Xcode 7.2


Has anyone any news? Does anyone know if there is a previous version of Xcode which will resolve this issue? Sounds like this issue has been around for a while. C'Mon Apple...