4

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

So basically, I need to run the ping test to check if all the networks on the machine works well or not. With Ethernet port connected, it always run the ping on the Ethernet. I wanted to switch between different networks. I am able to do this on WiFi connections without Ethernet connected.

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
Abhinav
  • 37,684
  • 43
  • 191
  • 309
  • 1
    There is not enough information to answer your question. What do you mean "run a test on WiFi with Ethernet cable connected"? Connected how? Since you tagged this "iphone", that's even more critical than if you were on a Mac. Exactly what are you trying to test? – Joshua Nozzi Jan 29 '13 at 20:00
  • Hi Josh... I have updated my question and tagged it properly. Please see if you can help me out. – Abhinav Jan 29 '13 at 20:07
  • Ah. I've updated the title to clarify and posted an answer. – Joshua Nozzi Jan 29 '13 at 20:25

5 Answers5

1

You're going to have to drop down to using sockets (which you can do from Objective-C, it's just not as easy as, say, loading a resource with NSURL). As I recall, if you bind to the socket by IP address of the interface you want to use, using that socket means you're using that network connection.

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
  • Ok Josh. Any idea how to do this. Make a sample code could help me. – Abhinav Jan 29 '13 at 20:24
  • 2
    It would be better for you to research the use of sockets, try to write some code based on your findings, and post it with your question if you get stuck on something specific. Asking the community to write it for you without trying first is asking a bit too much. – Joshua Nozzi Jan 29 '13 at 20:28
  • There are sample projects for GCDAsyncSocket that allow socket binding to a specific interface. Links are in my answer below. – Alfonso Tesauro Oct 04 '19 at 03:34
0

From the Network in System Preferences you can Set Service Order...

tagyro
  • 1,638
  • 2
  • 21
  • 36
0

Another alternative you might want to consider is to make use of the System Configuration framework.

You can use functions like "SCNetworkSetSetCurrent" (to change network sets from one where only WiFi is enabled to another set where only Ethernet is enabled).

Also, if you're not going to release this app to the general public or through the app store, you can do Terminal/UNIX command line things like "sudo ifconfig" to take a specific interface (e.g. "en0") up or down between tests.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
0

You can try GCDAsyncSocket. You will need to change your connections to sockets. In the HTTPServer.m file in all samples, 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, like in the above example. I hope I will be helpful.

Alfonso Tesauro
  • 1,730
  • 13
  • 21
0

You need to write a privileged tool helper and then implement some function like this

func setNetworkLocation(newLocation : String,reply: @escaping (String) -> Void){
    var retVal : Bool = false
    var response = ""
    var prefs : SCPreferences
    prefs = SCPreferencesCreate (nil, "Softech" as CFString, nil)!
    var sets : CFArray
    sets = SCNetworkSetCopyAll (prefs)!
    
    
    let count = CFArrayGetCount(sets)
    var newSet : SCNetworkSet? = nil
    var bFound : Bool = false
    for nIndex  in 0..<count {
     let mySet = CFArrayGetValueAtIndex (sets, nIndex)
     let key = Unmanaged<SCNetworkSet>.fromOpaque(mySet!)
     newSet = key.takeUnretainedValue()
     let name : String = SCNetworkSetGetName(newSet!)! as String
     if(name  == newLocation){
      bFound = true
      break
     }
     print("Name: \(name) cerco \(newLocation)")
    }
    if(bFound){
        print("Trovata netwok location da impostare Name: \(newLocation)")
        retVal = SCNetworkSetSetCurrent (newSet!)
        print ("SCNetworkSetSetCurrent = \(retVal) ");
        retVal = SCPreferencesCommitChanges (prefs);
        print ("SCPreferencesCommitChanges = \(retVal) ")
        response = "setNetworkLocation: SCPreferencesCommitChanges = [\(retVal)] " + newLocation
    }else{
        response = "setNetworkLocation: Not found network location named [" + newLocation + "]"
    }
    reply(response)
}

Usage from application client

func setNetworkLocation(name: String){
    let xpcService = prepareXPC()?.remoteObjectProxyWithErrorHandler() { error -> Void in
        NSLog("XPC error: \(error)")
        } as? RemoteProcessProtocol

    xpcService?.setNetworkLocation(newLocation:name , reply: { [self]
        responseInfo in
        NSLog("\(name): FGLanSelectionHelper setNetworkLocation response => \(responseInfo)")
        
    })
}

@IBAction func btnSetNetworkLocation(_ sender: Any) { setNetworkLocation(name: "MyOffice") }