PDA

View Full Version : Check for LAN/WAN connection?




Locker
Sep 17, 2010, 02:49 AM
Hello,

I want an app to check whether it has a connection to a local server (via host-name or IP)... and also check whether it has a connection to the Internet (this can be by checking google.com if needs be).

If anyone could point me towards the easiest way to do this it would be much appreciated :)



87vert
Sep 17, 2010, 06:30 AM
you should be able to do a simple applescript to check for that.

Locker
Sep 17, 2010, 06:45 AM
Okay, so say I have something like:

if (do shell script "ping host-name") contains "1 packets received" then...

How exactly would I integrate this into a cocoa app? So that the fact it was running that ping was unknown to the user. And... how would it pass the results of the ping back to the cocoa app?

Sorry, I'm new to this :o

ChOas
Sep 17, 2010, 06:53 AM
This might be something: http://stackoverflow.com/questions/798454/how-to-write-a-simple-ping-method-in-cocoa-objective-c

Loads more to be found: http://www.google.nl/search?q=cocoa+ping

Locker
Sep 20, 2010, 06:00 AM
I did do a search in Google. Unfortunately most examples use SCNetworkReachability methods which will return true even if the host isn't responding. Anyone know any alternatives to that? I imagined this would be pretty easy to do...

ChOas
Sep 20, 2010, 06:27 AM
I did do a search in Google. Unfortunately most examples use SCNetworkReachability methods which will return true even if the host isn't responding. Anyone know any alternatives to that? I imagined this would be pretty easy to do...

This one then ? : http://developer.apple.com/library/mac/#samplecode/SimplePing/Introduction/Intro.html

Locker
Sep 20, 2010, 07:06 AM
This one then ? : http://developer.apple.com/library/mac/#samplecode/SimplePing/Introduction/Intro.html
That looks promising - thanks :)

Krevnik
Sep 21, 2010, 04:13 PM
Yeah, you pretty much have to approach this with two layers. SCNetworkReachability is your first layer which tells you if it is even worth trying to hit the network. The second layer is to check the host itself, and the links provided is exactly what you need.

This works well for providing a status mechanism on a server without having to actually open a connection to it.

If you intend to open a connection anyways, you might as well make the connection once SCNetworkReachability says you've got a connection that will work. It's almost always wasted effort to ping a server immediately before opening the connection, as they have similar timeouts for Ping vs TCP connections.