B4J Code Snippet Bind Jetty to desired network interface + get network interfaces

Hi,

Is there any way to make Jetty to use only WIFI interface or only ethernet interface?

Example: if I disconnect ethernet, and start Jetty, it will take IP from WiFi, but if I start it when both interfaces are up and connected it will use ethernet as default.

So, how can I give him the instructions which interface he should use.

Thanks!
 

Damjan

Member
Hi,

This is the way to set desired IP when you know it in advance.

How can I know which address is from WiFi interface? I mean in a dynamic way because server isn't always running in a same environment.

How can I make this totally automated without any config files or anything?

Maybe to use console input or is there a way to avoid it and make everything fully automated?

Thank you!!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code will return the network interfaces and their addresses:
B4X:
Sub Process_Globals
    Type NetworkInterface (Name As String, DisplayName As String, Address As String)
End Sub

Sub AppStart (Args() As String)
    Log("Hello world!!!")
    Log(GetNetworkInterfacesWithIp4Address)
End Sub

Private Sub GetNetworkInterfacesWithIp4Address As List
    Dim NetworkInterface As JavaObject
    NetworkInterface.InitializeStatic("java.net.NetworkInterface")
    Dim Collections As JavaObject
    Collections.InitializeStatic("java.util.Collections")
    Dim res As List
    res.Initialize
    Dim interfaces As List = Collections.RunMethod("list", Array(NetworkInterface.RunMethod("getNetworkInterfaces", Null)))
    For Each interface As JavaObject In interfaces
        Dim ni As NetworkInterface
        ni.Initialize
        ni.Name = interface.RunMethod("getName", Null)
        ni.DisplayName = interface.RunMethod("getDisplayName", Null)
        Dim Addresses As List = Collections.RunMethod("list", Array(interface.RunMethod("getInetAddresses", Null)))
        For Each add As JavaObject In Addresses
            If GetType(add) = "java.net.Inet4Address" Then
                ni.Address = add.RunMethod("getHostAddress", Null)
            End If
        Next
        If ni.Address <> "" Then res.Add(ni)
    Next
    Return res
End Sub
The name will start with wlan for wireless networks.
 
Top