Correct. The reachability API can only tell you if you "may" be able to communicate with another host. Whether you "can" is dependent on a whole host of issues including every cable/router/firewall between your host and the target and whether the application server is even running. The flags that are returned only tell you if a packet targeted for a remote host will be transmitted off of the local system (and nothing more).
As to your question about checking the full path between your host and a target system. Honestly, the only way to know for sure is to actually communicate with the application server. As you commented, ping's not really an option because the ICMP traffic is blocked by some networks. In other cases, the ping might be allowed but there's a firewall blocking your access to the application server. Lastly, even if you could ping the remote host there's no assurance that the server is even running.
- Allan
On May 1, 2006, at 11:22 AM, Gurmit Teotia wrote: I think I have misunderstood the reachablity API. These API can not detect whether the target computer is attached to network or not (in case if I temporary remove the network cable of same) unlike ping program. I have verified the above concept using the API SCNetworkCheckReachabliityByAddress. and return value in flag was always 131074( kSCNetworkFlagsReachable and kSCNetworkFlagsIsDirect) whether the network cable of target computer is plugged-in or not. But if I remove the cable of source computer(where this program is running) then value in flag is always 7 (kSCNetworFlagTransientConnection,kSCNetworkFlagsReachable,kSCNetworkFlagsConnectionRequired) Same was the behaviour of "SimpleReach" application downloaded from Apple site. I think reachability API checks the path of source computer rather then of target. Allan, can you please verify if my assumption is correct? If reachability API can not detect the connectivity of target computer then can you please suggest some alternative approach? ping is option but it is blocked in some networks? Regards, Gurmit
On 5/1/06, Gurmit Teotia <email@hidden> wrote: Hi Allan, I have double check the code. I'm always getting 2 in status whether target computer is connected to network or not. Most applications should be testing for :
reachable = ((flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired));
In fact I'm using the same _expression_ to check the connectity but when I got this behaviour I print the value of status and post it to list. Unless your code needs to work on a pre-Panther system I'd suggestion you look at the reachability APIs found in <SystemConfiguration/ SCNetworkReachability.h>. For synchronous requests the APIs will give you back the same results but you now have the option to setup an asynchronous notification when the reachability flags change. Using the notification you can skip having to poll if the network is available. That is a good idea. I'm targetting my application for tiger so above suggestion will work for me. I'll use the asynchronous notification approach and let you know the result. Allan, I have one more question. In my application target computer can be across a VPN. If I have to give the domain name for checking reachablity, how can I get the domain name of such computer by nam? Is there any such API? BTW in above issue both source and target exist in same network. Thanks for your quick response. Regards, On 4/29/06, Allan Nathanson <email@hidden> wrote: On Apr 29, 2006, at 8:42 AM, Gurmit Teotia wrote:
> Hi All, > In my application I will have to check at regular interval if a > computer is connected to network or not. To implement this > funtionality I was checking the behaviour of > SCNetworkCheckReachablityByName. > Below is the code snippet: > > SCNetworkConnectionFlags status; > BOOL result; > result=SCNetworkCheckReachabilityByName("XYZ",&status); > > NSLog(@"Result= %d status=%d",result,status); > > Output is always Result=1 status=0 whether "XYZ" computer is > connected to network or not. If I include domain name computer name > as follow:
result==1 (TRUE, the SCNetworkConnectionFlags are valid) status==0 (Not reachable, most likely because the specified hostname could not be resolved)
> SCNetworkConnectionFlags status; > BOOL result; > result=SCNetworkCheckReachabilityByName(" XYZ.MyDomain.com",&status); > > NSLog(@"Result= %d status=%d",result,status); > > Then output is always "Result=1 status=2" whether computer is > connected to network or not.
result==1 (TRUE, the SCNetworkConnectionFlags are valid) status==0 (Reachable)
Are you sure that you're getting a status=2 when the computer is not connected to the network. In many cases you'll get back a status=6 (kSCNetworkFlagsReachable|kSCNetworkFlagsConnectionRequired) which says "yes, you can (may) reach this host but you must first establish a connection using your Built-in modem). Most applications should be testing for :
reachable = ((flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired));
> Can someone explain this behaviour? Is that behaviour correct? > Please also suggest if I'm using the right approach to check if a > computer is connected to network or not.
Unless your code needs to work on a pre-Panther system I'd suggestion you look at the reachability APIs found in <SystemConfiguration/ SCNetworkReachability.h>. For synchronous requests the APIs will give you back the same results but you now have the option to setup an asynchronous notification when the reachability flags change. Using the notification you can skip having to poll if the network is available.
- Allan
|