Re: How to determine if there is an internet connection available?
Re: How to determine if there is an internet connection available?
- Subject: Re: How to determine if there is an internet connection available?
- From: Conrad Shultz <email@hidden>
- Date: Fri, 11 Nov 2011 19:41:11 -0800
On 11/11/11 6:39 PM, Vojtěch Meluzín wrote:
> Hi,
>
> I'm using BSD sockets for some internet access, it works fine. But if there
> is no connection available, it waits for say 30 seconds completely stopping
> the application. Is there a way to determine if there is actually an
> internet connection, so that I can check before using sockets?
(This has been extensively discussed in the past in the context of iOS
network availability. Search the archives for more info.)
The only reliable way to verify connectivity is to attempt a connection.
Checking for physical connectivity might speed up failure detection
(assuming you don't care about connections on the loopback interface),
but there are a host (no pun intended) of reasons that you still might
not be able to connect.
However, there are other options.
For one, since you are using BSD sockets (and I have to ask: are you
sure that a higher level API wouldn't accomplish what you need?), you
can use setsockopt() to control various timeouts. One of the Darwin or
networking lists might be of more assistance at this level of the
application.
More worrying is your statement that the wait "completely [stops] the
application." Are you saying that you are blocking the main thread
while waiting on I/O? This is almost always a bad idea in a
user-interactive application. The BSD sockets API is thread-safe (when
accessed from a single thread), so you could spawn a separate network
thread. I believe there is also a non-blocking mode within the sockets
system, but people on the Darwin list would be more helpful here.
If you are able to use higher level APIs, life becomes somewhat easier.
One level up, using CFNetwork, Apple has detailed documentation about
how to handle this issue
(http://developer.apple.com/library/mac/#documentation/Networking/Conceptual/CFNetwork/CFStreamTasks/CFStreamTasks.html#//apple_ref/doc/uid/TP30001132-CH6-SW19).
--
Conrad Shultz
Synthetiq Solutions
www.synthetiqsolutions.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden