Default Network Interface

Controls like UDPSocket use the “default” network interface if it’s .NetworkInterface = nil.
Is there a way in Xojo to find which is the default Network Interface? I see that I can iterate through the interfaces, but which one would be the default? Index 0?

It’s the first one listed, i.e. System.NetworkInterface(0)

It would seem that its not always the first entry as it’s not on my PC here. The simplest way is to connect to a known address on the network you’re interested in and check what address its used to make the connection, that will be the default address.

So for checking which interface your computer uses to connect to the internet:

[code]Dim t As New TCPSocket
t.address = “google.com
t.port = 80
t.connect

For i As Integer = 0 To system.NetworkInterfaceCount - 1
If t.LocalAddress = system.NetworkInterface(i).IPAddress Then
system.DebugLog("Default Interface is " + str(i) + " with IP " + system.NetworkInterface(i).IPAddress)
End If
Next

t.close[/code]

23:26:42 : Default Interface is 1 with IP 192.168.0.84

Really? Oh brother!

So how does UDPSocket “figure out” which interface is the default?

In windows and linux, metrics, on mac, priorities.

If I had to guess, I’d assume that Xojo is just leveraging an underlying OS api call on both OS X and Windows, and it was easier in the documentation to say “it just uses the default” than to explain the intricacies of exactly how it works on each platform. It likely does just use the default interface as provided by the os API - but exactly what that default is, and how you can determine (beforehand) which interface that will be… I don’t know.

As for @JulianS 's example above - I’m not sure you’ll have to actually make the connection - if you just dim t as new TCPSocket (or UDPSocket) does t.LocalAddress get populated with anything before you try to make a connection?

Doesn’t it strike you as odd that the UDPSocket has an internal function to find the default network interface, but this function is not part of System.NetworkInterface itself?

It is what it is, I suppose.

Thanks for your help, gents!

Odd yes
Surprising no
Its probably been that way for a very long time

[quote=473042:@Norman Palardy]Its probably been that way for a very long time[/quote]I’m quite sure you’re right - I guess I must just be the first one who noticed.

Julian’s guessing code can be simplified as (at least on Mac):

Dim boundIP As String = TCPSocket(New TCPSocket).LocalAddress For i As Integer = 0 To system.NetworkInterfaceCount - 1 Dim s As String = "Interface "+str(i)+ " IP: "+system.NetworkInterface(i).IPAddress If boundIP = system.NetworkInterface(i).IPAddress Then s = s + " <<< Default" System.DebugLog s Next

Don’t trust any of this on Windows. If a hyper-v server has been installed the “default” address will be a private address i.e. 172.16.0.0 to 172.31.255.255 which is allocated to the default virtual adapter.

I’m not sure where the issue is, surely if you get an address on the default virtual adaptor then you’ve got the default network interface for that VM because I wouldn’t expect a VM to be handed the baremetal’s default adaptor if that’s what your saying is the issue? If the VM can get out of the box (even via NAT) on that private address then surely that’s the default adptor for that route?

You are correct. I wanted to find the network address of my network and using the default adapter doesn’t do it for me.