How to choose a network interface to use in my Cocoa application

I am trying to run a test on different networks. I can switch between two WiFi connections, but I need to know how I can run a test on WiFi with an Ethernet cable connected.

So basically, I need to run the ping test to check if all networks are working on the machine or not. When an Ethernet port is connected, it always pings on Ethernet. I wanted to switch between different networks. I can do this on WiFi connections without an Ethernet connection.

+4
source share
4 answers

You will need to get down to using sockets (what you can do from Objective-C, it's not as simple as, say, loading a resource using NSURL). As far as I remember, if you bind to a socket by the IP address of the interface you want to use, using this socket means that you are using this network connection.

+1
source

From the network in the system settings, you can set the service order ...

0
source

Another alternative that you might want to consider is to use the system configuration infrastructure.

You can use features such as " SCNetworkSetSetCurrent " (to change network sets from one, where only WiFi is turned on for another set, where only Ethernet is allowed).

In addition, if you are not going to release this application to the general public or through the application store, you can use the Terminal / UNIX command line, for example, " sudo ifconfig " , to run a specific interface (for example, " en0 ") up or down between tests.

0
source

You can try GCDAsyncSocket . You will need to change your socket connections. In the file HTTPServer.m in all examples you will find:

 // By default bind on all available interfaces, en1, wifi etc interface = @"192.168.2.1"; // Use a default port of 0 // This will allow the kernel to automatically pick an open port for us port = 40000; 

You can set the interface to either en0, en1, etc., or directly, using the IP address of the interface, as in the above example. I hope I will be helpful.

0
source

All Articles