AFNetworking: Using IP address, application cannot reach server
Asked Answered
G

1

7

When I try to use an ip address instead of a hostname to tell an ios application in a simulator to talk to a local server the app hangs on the loading screen, waiting for a response from the server that never comes.

I'm new with Objective C but the code is using the AFNetowrking framework that's handling the request and the SCNetworkReachabilityFlags property is set differently for the ip address on the AFHTTPClient object.

The following properties of the AFHTTPClient object for ip address and hostname were captured during a debug.

<SCNetworkReachability 0xd26d8a0 [0x53b9ec8]>
{address = 127.0.0.1, flags = 0x00010002, if_index = 1}

<SCNetworkReachability 0xcf283f0 [0x53b9ec8]>
{name = localhost (server query active), flags = 0x80000000, if_index = 0}

In the startMonitoringNetworkReachability method within the AFHTTPClient.m class there is the following if statement that is triggered for the ip address but I don't know if it is the problem:

/* Network reachability monitoring does not establish a baseline for IP addresses as
it does for hostnames, so if the base URL host is an IP address, the initial 
reachability callback is manually triggered.
 */
if (AFURLHostIsIPAddress(self.baseURL)) {
    SCNetworkReachabilityFlags flags;
    SCNetworkReachabilityGetFlags(self.networkReachability, &flags);
    dispatch_async(dispatch_get_main_queue(), ^{
        AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusForFlags(flags);
        callback(status);
    });
}

My question is what's causing the problem and how do I use an IP address as baseURL to connect to a server?

Gonfalon answered 12/11, 2013 at 0:17 Comment(3)
So, your server isn't responding? What's the question?Cracking
Hi Scott, that's correct the tornado server does not respond when an ip address is used. I think the AFHTTPClient never finds the server is reachable, even though when using a hostname it is. I've added a question to the original post. thanks.Gonfalon
Is there a way out to use ip address right row?Case
L
4

This is a common issue. There was a discussion about this on the AFNetworking github page: https://github.com/AFNetworking/AFNetworking/issues/2191

Domains and ip addresses are checked differently.

You can check an IP address using a manager created with managerForAddress: http://cocoadocs.org/docsets/AFNetworking/2.5.0/Classes/AFNetworkReachabilityManager.html#//api/name/managerForAddress:

I hope this helps! Let me know if you have any more questions.

EDIT

I think I misunderstood the question.

I've sent requests using the AFHTTPRequestOperationManager and AFURLSessionManager before and they both work using IP addresses in the url.

An example of how to use them can be found here: http://cocoadocs.org/docsets/AFNetworking/2.5.0/#usage

Logwood answered 4/12, 2014 at 21:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.