Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > How do I check for a network connection?

How do I check for a network connection?
Thread Tools
kman42
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Apr 8, 2003, 10:28 AM
 
Is there some really simple way to check for a net connection? My app gets data off the web, but I don't want to get an error if there is no net connection.

kman
     
ivi
Forum Regular
Join Date: Nov 2002
Status: Offline
Reply With Quote
Apr 8, 2003, 01:09 PM
 
The easiest is of course to open a socket and try to connect to a host with the data, if the connection can't be established then say that couldn't connect etc.
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Apr 8, 2003, 02:14 PM
 
Originally posted by ivi:
The easiest is of course to open a socket and try to connect to a host with the data, if the connection can't be established then say that couldn't connect etc.
I appreciate your response, but need a bit more explanation than that. I am a Cocoa newbie and couldn't find the appropriate class/methods for networking in the Cocoa docs. Could you point me in the right direction?

thanks,
kman
     
Rickster
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status: Offline
Reply With Quote
Apr 8, 2003, 04:58 PM
 
Foundation only provides ObjC API for the most simple of networking tasks... for many things you'll need to use C APIs.

The recommended way to test for network connections is to use the SCReachability functions in the SystemConfiguration framework -- they can tell you whether a given hostname or IP address can be resolved or connected to in the current state of your network connections. You might have some addresses reachable on your local Ethernet or AirPort network but need to open a PPP connection to the Internet to reach others, for example; SystemConfiguration helps with that, too.
Rick Roe
icons.cx | weblog
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Apr 8, 2003, 05:19 PM
 
Originally posted by Rickster:
Foundation only provides ObjC API for the most simple of networking tasks... for many things you'll need to use C APIs.

The recommended way to test for network connections is to use the SCReachability functions in the SystemConfiguration framework -- they can tell you whether a given hostname or IP address can be resolved or connected to in the current state of your network connections. You might have some addresses reachable on your local Ethernet or AirPort network but need to open a PPP connection to the Internet to reach others, for example; SystemConfiguration helps with that, too.
That sounds quite complicated. How about a shortcut? I'm currently using [NSString stringWithContentsOfURL:]. Is there a way to set a timeout or something on this method? I know it will return nil if it fails, but it seems to try for a really long time. Any idea how long it will attempt? I could just make sure the timer that calls this method is set to twice the timeout for the method or something. Thoughts?

kman
     
ambush
Banned
Join Date: Apr 2002
Location: -
Status: Offline
Reply With Quote
Apr 11, 2003, 09:00 AM
 
Originally posted by kman42:
That sounds quite complicated. How about a shortcut? I'm currently using [NSString stringWithContentsOfURL:]. Is there a way to set a timeout or something on this method? I know it will return nil if it fails, but it seems to try for a really long time. Any idea how long it will attempt? I could just make sure the timer that calls this method is set to twice the timeout for the method or something. Thoughts?

kman
Well the initWithContentOfURL: methods are quite bad, but, I admit easy to use.

They're not multithreaded and slow.

You could use curlhandle. curlhandle.sf.net , which is multithreaded (optional) and quite excellent..

you can use NSURLHandle something too.
     
Rickster
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status: Offline
Reply With Quote
Apr 11, 2003, 01:57 PM
 
If you're just looking to provide a timeout, you could use the asynchronous loading methods on NSURL -- set a timer and if your client's -URL:resourceDataDidBecomeAvailable: method isn't called by the time it fires, consider that your failure case.
Rick Roe
icons.cx | weblog
     
3.1416
Dedicated MacNNer
Join Date: Feb 2001
Status: Offline
Reply With Quote
Apr 11, 2003, 08:05 PM
 
Foundation only provides ObjC API for the most simple of networking tasks... for many things you'll need to use C APIs.

True and annoying. Apple really should add an NSTCPSocket class to Foundation, but in the meantime I've used the networking classes from http://www.mulle-kybernetik.com/software/EDFrameworks/ with good results.
     
ambush
Banned
Join Date: Apr 2002
Location: -
Status: Offline
Reply With Quote
Apr 11, 2003, 08:45 PM
 
Whatever happened to Steven Frank's SmallSockets *sigh*
     
ksuther
Forum Regular
Join Date: Feb 2002
Location: New York
Status: Offline
Reply With Quote
Apr 11, 2003, 10:42 PM
 
Take a look at this: http://cocoa.mamasam.com/COCOADEV/2002/03/2/29159.php

Hope that helps
     
Angus_D
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Apr 14, 2003, 05:40 PM
 
Well, CFNetwork provides various networking things, hopefully they'll be exposed to cocoa at some point too.
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 05:45 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,