Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous network interface status check

Tags:

c++

c

objective-c

On Mac OS X, is there a way to get asynchronous notification when the network interface (en0, en1) status changes, such as network interface is active, inactive etc?

Instead of polling for the status, i would like to know is there a way to get asynchronous notification, so that whenever there is a change, notification is fired and app can handle it.

Is this possible?

Thanks,

like image 922
user998753 Avatar asked Oct 17 '11 12:10

user998753


1 Answers

It depends on exactly what changes in the network state you want to be informed of (e.g. do you just want to know when you gain or lose a network connection, or do you want to know things like if you've switched WiFi networks or some more esoteric detail like that), and if you provide more details about that I can provide a better answer, but generally speaking I reckon you want to use the SystemConfiguration framework, specifically probably the SystemConfiguration/SCNetworkReachability.h APIs (described in Apple docs here).

Even more specifically than that, I think SCNetworkReachabilityCallBack is the type of callback function you want to use. This here is the Apple documentation for it.

Finally, to set up that callback, I mean first you have to write your callback function, but to "register" it so SystemConfiguration will use it to call you back when your network status changes, use this guy: SCNetworkReachabilitySetCallback(_:_:_:) described in official Apple documentation here.

Hope this helps and if you want more specific help just add some more specifics to your question :)

like image 81
23r0c001 Avatar answered Nov 02 '22 22:11

23r0c001