|
|
How do I check for a network connection?
|
|
|
|
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
|
Forum Regular
Join Date: Nov 2002
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
|
Banned
Join Date: Apr 2002
Location: -
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: Feb 2001
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
|
Banned
Join Date: Apr 2002
Location: -
Status:
Offline
|
|
Whatever happened to Steven Frank's SmallSockets *sigh*
|
|
|
|
|
|
|
|
|
Forum Regular
Join Date: Feb 2002
Location: New York
Status:
Offline
|
|
|
|
|
|
|
|
|
|
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
Well, CFNetwork provides various networking things, hopefully they'll be exposed to cocoa at some point too.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|