Le 12 nov. 2011 à 04:41, Conrad Shultz a écrit :

> 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).
> 

And if you find that CFStream does not fit your need (to high level for your 
purpose), you can just wrap you BSD socket into a CFSocketRef, and schedule it 
on the current run loop.


-- Jean-Daniel




_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to