(iphone) reachability test for specific ip/port?

2019-01-19 08:14发布

I've looked at Reachability sample from apple and another sample from "Iphone developer's cook book"

Here are steps to test ip/port reachability

  1. construct sockaddr_in variable with given ip/port.
  2. call SCNetworkReachabilityCreateWithAddress with the address
  3. call SCNetworkReachabilityGetFlags and see if kSCNetworkFlagsReachable is set.

I find that whatever valid(valid-range) ip/port I put to test, my code says it's reachable even though they are not actually reachable when i ping or telnet test.
(I have seen other SO posts where reachability test to specific ip always succeed) Reachability reachabilityWithAddress does not work

is SCNetworkReachability.. call only checks the validity of argument(ip address or hostname)? ie, do they actually send a packet(or do connect) to the given address to test reachability?

I can implement the async connect with timeout myself, but it's subtle matter how long the timeout should be not to be rejected by apple reviewers.(I am worried if it takes too long, apple reviewers might think my app is not handling network reachability at all)

Thank you

1条回答
甜甜的少女心
2楼-- · 2019-01-19 08:50

To answer your question based on the comments above, according to the SCNetworkReachability Reference

A remote host is considered reachable when a data packet, sent by an application into the network stack, can leave the local device. Reachability does not guarantee that the data packet will actually be received by the host.

This will not be able to check the status of your server but just make sure it can get a data packet out.

Apple wants for your app to elegantly fail if there are any network related issue, checking to see if internet is available using Reachability is the first step. If Reachability fails you can assume that you wont be able a server and alert the user immediately. The next steps are to properly handle errors returned by your server such as a 400 bad request, 404 file not found, setting a reasonable timeout, and not blocking the UI during long downloads while showing some sort of status indicator.

查看更多
登录 后发表回答