Swift - Get XML content in LAN with error -1004

256 Views Asked by At

I create a function to get an xml content in LAN using Alamofire.

private func discover(from ip: String, completion: @escaping () -> Void) {
    let url = ip.url(file: "discover.xml")

    var request = URLRequest(url: url)
    request.httpMethod = "GET"
    request.timeoutInterval = 5.0 // !

    let manager = Alamofire.SessionManager.default
    //        manager.session.configuration.httpShouldUsePipelining = true
    manager.session.configuration.timeoutIntervalForRequest = 5.0
    manager.session.configuration.timeoutIntervalForResource = 5.0

    manager.request(request).responseData { [weak self] (response) in
        guard let strongSelf = self else { return }

        defer {
            completion()
        }

        switch response.result {
        case .success(_):
            print("success")

        case .failure(let error):
            print("[LANUser] failed with IP: \(ip) => \(error)")
        }
    }
}

extension String {
    func url(file: String) -> URL {
        return URL(string: "http://\(self)/\(file)?rand=\(arc4random())")! // rand to make sure cached content is not returned
    }
} 

And use it like below:

@IBAction func scan1(_ sender: Any) {
    let ip = "192.168.1.105:17171" // I'm sure my server already worked on port `17171`

    discover(from: ip) {
        print("done -> check!")
    }
}

However, I just can receive error -1004:

2018-10-05 18:07:34.393450+0700 LANScan[205:3504] Task <CCDD5C95-2C7E-408B-AD1B-BA54EFAA2F70>.<511> load failed with error Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={_kCFStreamErrorCodeKey=61, NSUnderlyingError=0x282b5eeb0 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <CCDD5C95-2C7E-408B-AD1B-BA54EFAA2F70>.<511>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <CCDD5C95-2C7E-408B-AD1B-BA54EFAA2F70>.<511>"
), NSLocalizedDescription=Could not connect to the server., NSErrorFailingURLStringKey=http://192.168.1.105:17171/discover.xml?rand=3209938829, NSErrorFailingURLKey=http://192.168.1.105:17171/discover.xml?rand=3209938829, _kCFStreamErrorDomainKey=1} [-1004]
[LANUser] failed with IP: 192.168.1.105:17171 => Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={_kCFStreamErrorCodeKey=61, NSUnderlyingError=0x282b5eeb0 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <CCDD5C95-2C7E-408B-AD1B-BA54EFAA2F70>.<511>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <CCDD5C95-2C7E-408B-AD1B-BA54EFAA2F70>.<511>"
), NSLocalizedDescription=Could not connect to the server., NSErrorFailingURLStringKey=http://192.168.1.105:17171/discover.xml?rand=3209938829, NSErrorFailingURLKey=http://192.168.1.105:17171/discover.xml?rand=3209938829, _kCFStreamErrorDomainKey=1}
done -> check!

I also Allow Arbitrary Loads and Arbitrary Loads in Web Content (below) but that still does not help:

App Transport Security Settings

What blocks me from getting the xml content from 192.168.1.105? Is there any other settings that I should enable to get it working?

Thanks in advance,

EDIT: Add port 17171 as @wottle has pointed out

1

There are 1 best solutions below

2
On

It does not appear to be ATS blocking this connection. The first step to troubleshooting this is to attempt to load the URL in Safari on the device. I suspect you can access the URL from your development machine, but the iOS device is blocked from accessing the server.

Basically, load the http://192.168.1.105:17171/discover.xml?rand=00000 in Safari and see if you get an error.

If it can't load, make sure your iOS device is on the same network as the 192.168.1.105 device. Also, make sure there are no firewall rules that would block traffic. There are a lot of reasons you could not be able to check, so you'd need to provide more details i it turns out you cannot connect (I suspect you cannot). One thing you could check is that you have Wifi Assist turned off. If your Wifi signal is weak, it could be using cellular instead.

If it does load successfully, there could be something wrong with your code. You seem to have a difference in the code running and the code you show. Note that your code shows the port for the server as 17171, but the networking error appears to be using the default port (80), since it does not specify the port in the error (see: NSErrorFailingURLStringKey=http://192.168.1.105/discover.xml?rand=3209938829) Did you change the code in between posting and running it. Make sure that matches. It seems as though your code doesn't do anything to strip the port off, but double check the url that is actually being loaded matches the url that you were able to successfully load in Safari.